简体   繁体   中英

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. The following code split the above text file and create multiple files when it finds .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. So there should be 1400 text files. But it produces 1399 text files. 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:

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. Just create a String.
  • Be sure to extract and use all text within the while loop and before writing the text.

When you encounter a line starting with ".I" you want to close the existing file (if any) and start a new file. 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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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