0

I was reading similar topic to mine at How to append text to an existing file in Java and have tried solutions there, unfortunately non of them answer my specific case.

I will be creating a lot of changes to a file so I thought that I will create method which will return PrintWriter object on which I could do changes by executing writer.prinln("text");

private static PrintWriter WriteToFile() {

     PrintWriter out = null;
      BufferedWriter bw = null;
      FileWriter fw = null;
      try{
         fw = new FileWriter("smrud.txt", true);
         bw = new BufferedWriter(fw);
         out = new PrintWriter(bw);
//           out.println("the text");
         return out;
      }
      catch( IOException e ){
          return null;
      }
      finally{
         try{
            if( out != null ){
               out.close(); // Will close bw and fw too
            }
            else if( bw != null ){
               bw.close(); // Will close fw too
            }
            else if( fw != null ){
               fw.close();
            }
            else{
               // Oh boy did it fail hard! :3
            }
         }
         catch( IOException e ){
            // Closing the file writers failed for some obscure reason
         }
      }
}

So in my main method I'm calling this method

PrintWriter writer = WriteToFile();

Then I'm making changes

 writer.println("the text2");

and I'm closing writer to save changes to disk:

 writer.close();

Unfortunately I don't see any changes. When I put changes inside WriteToFile() method then I see the changes:

private static void WriteToFile() {

     PrintWriter out = null;
      BufferedWriter bw = null;
      FileWriter fw = null;
      try{
         fw = new FileWriter("smrud.txt", true);
         bw = new BufferedWriter(fw);
         out = new PrintWriter(bw);
         out.println("the text");
      }
      catch( IOException e ){
         // File writing/opening failed at some stage.
      }
      finally{
         try{
            if( out != null ){
               out.close(); // Will close bw and fw too
            }
            else if( bw != null ){
               bw.close(); // Will close fw too
            }
            else if( fw != null ){
               fw.close();
            }
            else{
               // Oh boy did it fail hard! :3
            }
         }
         catch( IOException e ){
            // Closing the file writers failed for some obscure reason
         }
      }




}

But this method open FileWriter, BufferedWriter and PrintWriter every time it will be executed and I wanted to avoid that by returning PrintWriter to main method and then executing writer.println("text"); and after some time close it by writer.close(); but this don't work.

Any suggestions will be appreciated.

J-Alex
  • 6,881
  • 10
  • 46
  • 64
J. Doe
  • 1

2 Answers2

0

All the writers are closed in WriteToFile so when the PrintWriter object is returned it will not write anything.

You will have to increase the scope of the PrintWriter and manage it outside the WriteToFile method.

Someting like:

public static void main(String[] args) {
    try (PrintWriter printWriter = createWriter()) {
        printWriter.println("Line 1");
        printWriter.println("Line 2");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static PrintWriter createWriter() throws IOException {
    return new PrintWriter(new FileWriter("smrud.txt", true));
}
neta
  • 61
  • 5
0

Not sure I get your point.

Anyhow, why dont you create your specialized "writer" object? In its constructor you would open {File; Buffered; Print}Writer, then your object will need a println(String text) that will forward the string parameter to the appropriate writer. Finelly you'll need a close() method that will call close on all your writer objects.

You could also extend this making your object a singleton and synchronizing the print method, allowying your software to print on the same file from different classes.

Viç
  • 356
  • 2
  • 5