Can not write to file in Java
This program is supposed to read a file of the format:
asdf,asdf,sadf And write a column into a file called var.txt, but it creates the file, and does not write anything to it. No exception occurs Code:
import java.io.*; |
Have you considered that maybe "vs" has zero elements?
|
[QUOTEHave you considered that maybe "vs" has zero elements?[/QUOTE]
this is not possible as I intentionally valid data to it, If I go like: Code:
for(int i=0;i<vs.size;i++) |
Can you try bw.flush() after bw.write()?
|
yes ppanyam that worked. thanks!
but when you write shudnt the JVM itself handle the flushing, instead of calling it explicity. |
Your program is supposed to do an explicit "close()". Which, in turn, does an implicit "flush()".
|
The BufferedWriter "write()"s to buffer, as the name suggests. It will write to disk as per a predefined policy like if buffer is full or if x amount of time has lapsed. If you want it to write immediately to disk, you invoke flush() on the BufferedWriter. Alternately, if you close() the stream, it will clean up and will write to disk.
|
ppanyam is right.
A "BufferedWriter" (by definition!) buffers its data (in the name of I/O efficiency). If you don't like this behavior, you can either flush whenever you wish to, or choose an unbuffered I/O class. But in any case - you must absolutely "close()" any file you open. Doing otherwise (e.g. depending on the OS or JVM to "clean up after you") is, frankly, just sloppy programming. IMHO .. PSM |
All times are GMT -5. The time now is 09:14 PM. |