How to append a string to a file at any stage

D

DX

I need to append a string to a file in this way:

/////////////////////////////////////////////
String text;

while(some condition){
text = new value;
Append "text" to file named "OutputFile.txt";
}
Append "text" to file named "OutputFile.txt" again;
Other codes;
Append "text" to file named "OutputFile.txt" again;
......
end of program;
//////////////////////////////////////////

I have tried to use:

////
BufferedWriter out = new BufferedWriter(new FileWriter("OutputFile.txt",
true));
while(some condition){
text = new value;
out.write(text);
}
other codes;
out.write(text);
out.close();
////

but it doesn't behave the way I expected.

What I want is: as the program runns at different stage, I need to have the
capability of appending a string to a specific file at any point.
Is there such a method that can allow me to do so in Java?

Thank you for your help!
-New Java user
 
R

Roedy Green

while(some condition){
text = new value;
Append "text" to file named "OutputFile.txt";
}
Append "text" to file named "OutputFile.txt" again;
Other codes;
Append "text" to file named "OutputFile.txt" again;
.....
end of program;
///////////////////////
see http://mindprod.com/jgloss/fileio.html

Just open with append, write, keep writing, or close and open with
append.
 
S

Steve Horsley

DX said:
I need to append a string to a file in this way:

/////////////////////////////////////////////
String text;

while(some condition){
text = new value;
Append "text" to file named "OutputFile.txt";
}
Append "text" to file named "OutputFile.txt" again;
Other codes;
Append "text" to file named "OutputFile.txt" again;
.....
end of program;
//////////////////////////////////////////

I have tried to use:

////
BufferedWriter out = new BufferedWriter(new FileWriter("OutputFile.txt",
true));
while(some condition){
text = new value;
out.write(text);
}
other codes;
out.write(text);
out.close();
////

but it doesn't behave the way I expected.

Looks OK to me - that's what I would do. In what way doesn't it do
what you expect?

You may want to do:

out.write(text);
out.newLine();

Steve
 
D

DX

DX said:
I need to append a string to a file in this way:

/////////////////////////////////////////////
String text;

while(some condition){
text = new value;
Append "text" to file named "OutputFile.txt";
}
Append "text" to file named "OutputFile.txt" again;
Other codes;
Append "text" to file named "OutputFile.txt" again;
.....
end of program;
//////////////////////////////////////////

I have tried to use:

////
BufferedWriter out = new BufferedWriter(new FileWriter("OutputFile.txt",
true));
while(some condition){
text = new value;
out.write(text);
}
other codes;
out.write(text);
out.close();
////

but it doesn't behave the way I expected.

What I want is: as the program runns at different stage, I need to have the
capability of appending a string to a specific file at any point.
Is there such a method that can allow me to do so in Java?

Thank you for your help!
-New Java user



Thanks for replies!
Sorry I didn't specify what was not "expected".
It looks like to me that I need to defined more than one "out" instance in
order to use "write(text)" each time.
If I only use one "out" instance, I can see my output file empty.
Any idea what I was missing?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top