简体   繁体   English

为什么下面的代码没有打印.I之后的最后一部分

[英]why the following code does not print the last part after .I

The text file is as follows :文本文件如下:

.I 1
some text
.I 2
some text
.I 3
some text
........

The following code use stringbuilder to append the lines.以下代码使用 stringbuilder 附加行。 The following code split the above text file and create multiple files when it finds .I下面的代码分割上面的文本文件并在找到 .I 时创建多个文件

but the problem is when I run the code , it does not create file for the last .I it has 1400 .I in the file.但问题是当我运行代码时,它不会为最后一个创建文件。我在文件中有 1400 .I。 So there should be 1400 text files.所以应该有1400个文本文件。 But it produces 1399 text files.但它会产生 1399 个文本文件。 whats the problem ?有什么问题 ? I could not find the problem.我找不到问题所在。

public class Test {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String inputFile="C:\\logs\\test.txt"; 
    BufferedReader br = new BufferedReader(new FileReader(new File(inputFile)));
         String line=null;
         StringBuilder sb = new StringBuilder();
         int count=1;
        try {
            while((line = br.readLine()) != null){
                if(line.startsWith(".I")){
                    if(sb.length()!=0){
                        File file = new File("C:\\logs\\DOC_ID_"+count+".txt");
                        PrintWriter writer = new PrintWriter(file, "UTF-8");
                        writer.println(sb.toString());
                        writer.close();
                        sb.delete(0, sb.length());
                        count++;
                    }
                    continue;
                }
                sb.append(line);
            }

           } catch (Exception ex) {
             ex.printStackTrace();
        }
           finally {
                  br.close();
          }
    }
}

You're appending to the StringBuilder at the bottom of your loop, after you've written with the FileWriter, and this means that the last line appended to the StringBuilder will not be written to the file:在使用 FileWriter 写入之后,您将附加到循环底部的 StringBuilder,这意味着附加到 StringBuilder 的最后一行将不会写入文件:

try {
    while((line = br.readLine()) != null){
        if(line.startsWith(".I")){
            if(sb.length()!=0){
                File file = new File("C:\\logs\\DOC_ID_"+count+".txt");
                PrintWriter writer = new PrintWriter(file, "UTF-8");
                writer.println(sb.toString());
                writer.close();
                sb.delete(0, sb.length());
                count++;
            }
            continue;
        }
        sb.append(line);  // here ****
    }
}

I suggest that you simplify the logic and code:建议你简化一下逻辑和代码:

  • Not even use a StringBuilder as there's no advantage to it's use in the current situation.甚至不使用 StringBuilder,因为在当前情况下使用它没有任何优势。 Just create a String.只需创建一个字符串。
  • Be sure to extract and use all text within the while loop and before writing the text.确保在 while 循环中和编写文本之前提取和使用所有文本。

When you encounter a line starting with ".I" you want to close the existing file (if any) and start a new file.当您遇到以“.I”开头的行时,您想关闭现有文件(如果有)并启动一个新文件。 Everything else just gets appended to the currently open file.其他所有内容都会附加到当前打开的文件中。 Make sure at the end, you check if you have an open file and close it.确保最后检查是否有打开的文件并关闭它。

public static void main(String[] args) throws IOException {
    String inputFile="C:\\logs\\test.txt"; 
    BufferedReader br = new BufferedReader(new FileReader(new File(inputFile)));
    String line=null;
    int count=1;
    try {
        PrintWriter writer = null;
        while((line = br.readLine()) != null){
            if(line.startsWith(".I")){
                if(writer != null) writer.close();
                writer = new PrintWriter(file, "UTF-8");
            }
            if(writer != null){
                writer.println(line);
            }
        }
        if(writer != null){
            writer.close;
        }
    } catch (Exception ex) {
         ex.printStackTrace();
    } finally {
        br.close();
    }
}

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

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