By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,650 Members | 753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,650 IT Pros & Developers. It's quick & easy.

string out to file

P: n/a
hello one and all,

i am still new to java but becoming ever so more efficient. i need some
advice and guidance on big strings. i am debugging a special counter
that i created in java and i am iterating to check the count sequence.
i am then outputting the sequence to a jTextPane. however, the
iterations are too large for the jTextPane object and i get an "out of
memory" error while debugging.

so i can output to a file. what is the best way to output short
strings, 3 to 4 characters long followed by a line feed. there may be
700,000 lines or more in the file when done. so what is the best way to
write to an ascii text file in java?

thanx in advance.

lucas

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> so what is the best way to
write to an ascii text file in java?


Check the API documentation for FileOutputStream and BufferedWriter.
These are classes that you may need to accomplish your tasks.

Create a FileOutputStream and specify the filename and path in its
constructor, then create the BufferedWriter (for enhanced performance)
specifying the FileOutputStream in its constructor. There may be some
examples on the Internet on how to use BufferedWriter.

Note that 700K lines of 5 characters each (newline counts as a
character) will be not much of a problem, unless you try to open it in
Micro$oft's NotePad.

Regards,

Joost
Jul 17 '05 #2

P: n/a
yes, that worked quite well, and it was fast without filling up the RAM.

this is what i ended up with:

try {
BufferedWriter oBuffer = new BufferedWriter(new OutputStreamWriter(new
FileOutputStream("/home/lucas/inc_chr_java.txt"),"UTF8"));
int ll = 4/*jTextField1.getText()*/;
String cc = iqs.padl("",ll);
String ee = iqs.padlchr("",'~',ll);
do {
cc = iqs.incChr(cc,ll);
oBuffer.write(cc); oBuffer.newLine();
}
while (!cc.equals(ee));
oBuffer.flush(); oBuffer.close();
}
catch (IOException except) {
return;
}

thanx again for the help.

lucas
remove .nospam wrote:
so what is the best way to write to an ascii text file in java?

Check the API documentation for FileOutputStream and BufferedWriter.
These are classes that you may need to accomplish your tasks.

Create a FileOutputStream and specify the filename and path in its
constructor, then create the BufferedWriter (for enhanced performance)
specifying the FileOutputStream in its constructor. There may be some
examples on the Internet on how to use BufferedWriter.

Note that 700K lines of 5 characters each (newline counts as a
character) will be not much of a problem, unless you try to open it in
Micro$oft's NotePad.

Regards,

Joost


Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.