简体   繁体   English

写入多个文件时文件为空

[英]files are empty when writing multiple files

I'm trying to read one file and write several files after modifying a small point. 我试图在修改一点后读取一个文件并写入多个文件。

My code works while writing the first file, but the other files are empty files. 我的代码在写入第一个文件时有效,但其他文件为空文件。 :( :(

I think there is a problem when I use bufferedwriter and filewriter , but can't find what the problem is though I followed an advice to use flush from stackoverflow. 我认为当我使用bufferedwriterfilewriter时出现问题,但是尽管我遵循了从stackoverflow使用flush的建议,却找不到问题所在。

What is the problem in my code? 我的代码有什么问题?

        FileReader fr = new FileReader(FileDir);
        BufferedReader br = new BufferedReader(fr);
        for (String mc: matchedContents){
            FileWriter fw = new FileWriter(saveFileDir+String.valueOf(matchedContents.indexOf(mc)+1)+".xml", false);
            BufferedWriter bw = new BufferedWriter(fw);
            while ((s = br.readLine())!=null){
                // check if s has matched contents
                if (s.contains(mc)){
                    String replacedString="";
                    if (mc.contains("NV"))
                        replacedString = s.replace(mc, "NV("+anyItem(edgeNames)+")");
                    else if (mc.contains("AW"))
                        replacedString = s.replace(mc, "AW("+anyItem(edgeNames)+")");
                    bw.write(replacedString);
                    bw.newLine();
                }
                else {
                    bw.write(s);
                    bw.newLine();
                }
            }
            System.out.println(mc+" end");
            bw.flush();
            bw.close();
            fw.close();
        }
        br.close();
        fr.close();

Its because after the first file your bufferedReader comes to the end. 这是因为在第一个文件之后,您的bufferedReader结束了。 To write again you need to reload the file to bufferedReader . 要再次写入,您需要将文件重新加载到bufferedReader So what you need to do is make the bufferedReader and the FileReader inside the for loop 因此,您需要做的是在for循环内创建bufferedReaderFileReader

    for (String mc: matchedContents){
        FileReader fr = new FileReader(FileDir);
        BufferedReader br = new BufferedReader(fr);
        FileWriter fw = new FileWriter(saveFileDir+String.valueOf(matchedContents.indexOf(mc)+1)+".xml", false);
        BufferedWriter bw = new BufferedWriter(fw);
        while ((s = br.readLine())!=null){
            // check if s has matched contents
            if (s.contains(mc)){
                String replacedString="";
                if (mc.contains("NV"))
                    replacedString = s.replace(mc, "NV("+anyItem(edgeNames)+")");
                else if (mc.contains("AW"))
                    replacedString = s.replace(mc, "AW("+anyItem(edgeNames)+")");
                bw.write(replacedString);
                bw.newLine();
            }
            else {
                bw.write(s);
                bw.newLine();
            }
        }
        System.out.println(mc+" end");
        bw.flush();
        bw.close();
        fw.close();
        br.close();
    }

    fr.close();

I would try debugging the code line by line and check what is exactly being written to the second, third etc. file (maybe the file writing is OK, but empty strings are being written?). 我将尝试逐行调试代码,并检查第二个,第三个等文件中到底写入了什么(也许文件写入正常,但是正在写入空字符串?)。

Also check: 还要检查:
Debugging Java code line by line 逐行调试Java代码
Java read in single file and write out multiple files Java读取单个文件并写出多个文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM