简体   繁体   中英

Only prints out last line to the text file and not the rest in java

I am trying to print a file to the text file. Although I have managed to make it work, it only prints out the last line that is printed on the console. Eg My console has around 8000 lines but it only prints out the last line in the text file, and I want to print all the lines into the text file. This is the code:

       try {
        BufferedReader reader = new BufferedReader(new FileReader("JDT.txt"));
        Writer output = null;
        File file = new File("output.txt");
        output = new BufferedWriter(new FileWriter(file));


        String line = reader.readLine();
        int count=0;
        while(line !=null)
        {
            for(int i = 0 ; i<faults.length;i++){
                if(line.contains(faults[i]))
                    System.out.println(line);
                count++; 
                output.write(line +"Total: "+count);
                //System.out.println("File Written");
        }
            line=reader.readLine();
            }                  
        System.out.println("Printed Lines =" +count); 
                   output.close();     
        }
    catch (Exception ex) {
        System.out.println(ex.getMessage());
        }       
}
    }

Thank you in advanced.

You are re-creating and overwriting your file in each loop. Create your file and your BufferedWriter before your loops, and close it after your loops finish.

You should place these lines before the beginning of the while loop.

Writer output = null;
File file = new File("output.txt");
output = new BufferedWriter(new FileWriter(file));

And close the Writer after while loop exits.

output.close();

Also the statement output.write(line +"Total: "+count); is trying to write every line at the end of another, resulting in one big line of output. Replace it with:

output.write(line + "Total: " + count + "\n");

This should result in each line getting printed on a new line.

Not sure if it's expected but in your code output.write might get executed multiple times for the same line depending on the length of your faults variable. This will result in same line getting printed more than once.

If the length of the faults is n the write will be called n times for the same line .

I think you need this:

while (line != null) {
    for (int i = 0; i < faults.length; i++) {
        if (line.contains(faults[i])) {
            count++;
            output.write(line + "Total: " + count + "\n");
            System.out.println(line);
            break;
        }
    }
    // System.out.println("File Written");
    line = reader.readLine();
}

I thought I'd put this here for reference. It's just a general approach for copying lines from an input file to an output file.

final class Sample
{
   private static final String inputFile = "input_file.txt";
   private static final String outputFile = "output_file.txt";

   public void start()
   {

      BufferedReader bufferedReader;
      BufferedWriter bufferedWriter;
      try
      {
         bufferedReader = new BufferedReader(new FileReader(inputFile));
         bufferedWriter = new BufferedWriter(new FileWriter(outputFile));
         copyContents(bufferedReader, bufferedWriter);
         bufferedReader.close();
         bufferedWriter.close();
      }
      catch (final IOException e)
      {
         e.printStackTrace();
      }
   }

   private void copyContents(final BufferedReader bufferedReader, final BufferedWriter bufferedWriter)
   {
      try
      {
         String line = bufferedReader.readLine();
         while (null != line)
         {
            bufferedWriter.write(line + '\n');
            line = bufferedReader.readLine();
         }
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
   }
}

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