请优化以下代码

public static void main(String[] args) throws IOException {
    File file = new File("some file path");
    List list1 = new ArrayList();
    FileInputStream fileInputStream = new FileInputStream(file);
    byte[] bytes = new byte[1024];
    int i = fileInputStream.read(bytes);
    if(i > 0) {
        do {
            i = fileInputStream.read(bytes);
            String s = new String(bytes, 0 , i);
            list1.add(s);
        } while(i != -1);
    }
    i = fileInputStream.read(bytes);
    String start = new String(bytes, 0, i);
    list1.add(start);
    for (int i1 = 0; i1 < list1.size(); i1++) {
        String s = (String) list1.get(i1);
        if(s.contains("1")) {
            s = s.replace("1", "a");
        } else if(s.contains("2")) {
            s = s.replace("2", "b");
        } else if(s.contains("3")) {
            s = s.replace("3", "c");
        } else if(s.contains("4")) {
            s = s.replace("4", "d");
        }
        list1.remove(i1);
        list1.add(i1, s);
    }
    FileOutputStream fos = new FileOutputStream(file);
    for (Object o : list1) {
        fos.write(o.toString().getBytes());
    }
}
全部评论
马住慢慢看
点赞 回复 分享
发布于 2023-03-04 12:15 河北
这不致远互联吗
点赞 回复 分享
发布于 2023-02-28 19:37 黑龙江

相关推荐

03-26 13:44
南华大学 Java
在看面经的花生米很野蛮:这种情况下你当然要回答,你也是吗!!!!我超喜欢他的XXXXX
点赞 评论 收藏
分享
程序员鼓励师阿欢:哈哈哈哈哈笑死我了😂
点赞 评论 收藏
分享
评论
1
2
分享

创作者周榜

更多
牛客网
牛客企业服务