繁体   English   中英

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

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

文本文件如下:

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

以下代码使用 stringbuilder 附加行。 下面的代码分割上面的文本文件并在找到 .I 时创建多个文件

但问题是当我运行代码时,它不会为最后一个创建文件。我在文件中有 1400 .I。 所以应该有1400个文本文件。 但它会产生 1399 个文本文件。 有什么问题 ? 我找不到问题所在。

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();
          }
    }
}

在使用 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 ****
    }
}

建议你简化一下逻辑和代码:

  • 甚至不使用 StringBuilder,因为在当前情况下使用它没有任何优势。 只需创建一个字符串。
  • 确保在 while 循环中和编写文本之前提取和使用所有文本。

当您遇到以“.I”开头的行时,您想关闭现有文件(如果有)并启动一个新文件。 其他所有内容都会附加到当前打开的文件中。 确保最后检查是否有打开的文件并关闭它。

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