Opening an existing file in Java and closing it.
Asked Answered
C

4

5

Is it possible to open a file a in java append data and close numerous times. For example:

 //---- psuedocode
      //class variable declaration 
      FileWriter writer1 = new FileWriter(filename);

      fn1:

         writer.append(data - title);

      fn2:
       while(incomingdata == true){
         writer.append(data)
         writer.flush();
         writer.close()
        }

The problem lies in the while loop. The file is closed and cant be opened again. Any one can help me in this?

Couthie answered 20/3, 2012 at 7:16 Comment(7)
You could reopen it each time?Kazim
You cannot access the stream once it is closed.Pricket
So don't close it? What's the problem here?Vadim
The file that i created has to be saved. For which im supposed to close it so the timestamp is updated. Thats when the file is synced live.Couthie
That's what writer.flush() will do.Amersfoort
@Siddhartan It might be a caching problem, i e the operating system is not re-reading the file and updating it's timestamp or file size for every flush, but the file is actually updated. This certainly is the case in Windows looking in explorer or using the dir command.Linden
Reference for why to close(): coderanch.com/t/550351/java/close-BufferedWriter-finish-writingGrizzle
D
3

The answers that advise against closing and re-opening the file each time are quite correct.

However, if you absolutely have to do this (and it's not clear to me that you do), then you can create a new FileWriter each time. Pass true as the second argument when you construct a FileWriter, to get one that appends to the file instead of replacing it. Like

FileWriter writer1 = new FileWriter(filename, true); 
Demakis answered 20/3, 2012 at 7:21 Comment(2)
So can I assume that the file won't be replaced each time by a new file?Couthie
That's what the second argument gives you. Check the javadoc for FileWriter if you need more detail.Demakis
P
3

Once a stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect.

 while(incomingdata == true){
         writer.write(data)
 }
 writer.close()

You don't need to flush each time. as calling close() will first flush data before closing the stream.

Updated for

The file that i created has to be saved. For which im supposed to close it so the timestamp is updated. Thats when the file is synced live.

Use it like this

while(incomingdata == true){
             writer.append(data);
             writer.flush();
}
writer.close();
Pricket answered 20/3, 2012 at 7:22 Comment(2)
What if I run into the loop again? is it possible to open the file again? Bcos I have closed it at the end of the loopCouthie
It will throw IOException. close the stream only after writing all the data into the stream.Pricket
D
1

I don't recommend trying to close your file and then reopening it again. Opening a file is an expensive operation and the fewer times you do this, the better it is for your code to run quickly.

Open it once, and close the file once you're done writing to it. This would be outside your loop.

Demodulate answered 20/3, 2012 at 7:20 Comment(0)
I
1

Once that the file is closed you will need to re-open it. Calling writer.flush() should flush the stream. So basically, you will then remove writer.close() from within the while loop. This will allow you to close the file once that you will have finished with it.

So you have two options, either remove writer.close() from within the while loop or else create a new FileWriter instance at the beginning of your loop.

Inconceivable answered 20/3, 2012 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.