writing to a text file, urgent help needed
807589Jul 18 2007 — edited Jun 25 2008Hi,
I am trying to write to a text file, but I need certain behavior that I'm not sure how to get. Look at the code below, it is fairly simple and straight forward.
------------------------------------------------------------
FileOutputStream FOStream = null;
PrintStream POStream = null;
try
{
FOStream = new FileOutputStream("text.txt");
POStream = new PrintStream(FOStream);
for (int i=10; i<20; i++){
POStream.print("hello" + i +"\r\n");
}
}catch(Exception e){
e.printStackTrace();
}
------------------------------------------------------------
This pretty much just iterates thru the for loop and then displays everything in the text file.
However, what I want is for each iteration to overwrite the previous iteration. So, for each iteration, I want the result to just overwrite what was already there in the text file instead of just appending it. I know I can create the FileOutputStream inside the loop itself to create a text file every iteration but I would like to avoid this, as it causes some memory and cpu usage that I wish to avoid. Please help. I would be ever so grateful.
Thanks