PrintStream Only Printing out the Last Line of Input File

后端 未结 2 1747
星月不相逢
星月不相逢 2021-01-29 07:34

I have this code

public class program {
    public static void main(String[] args) {
        try {
            String filePath = (args[0]);
            String st         


        
相关标签:
2条回答
  • 2021-01-29 08:26

    Don't create a new PrintStream in each loop. Instead create the PrintStream prior to the while loop:

    PrintStream out = new PrintStream( ... );
    while ((strLine = br.readLine()) != null) {
      out.print(strLine);
    }            
    
    0 讨论(0)
  • 2021-01-29 08:34

    Because you are overwriting to the same file within the loop. Create the PrintStream outside of the loop not inside the loop and it should write all lines!

    0 讨论(0)
提交回复
热议问题