How can I add a string to the end of a .txt file?
+1
A:
From here
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("checkbook.txt", true));
bw.write("400:08311998:Inprise Corporation:249.95");
bw.newLine();
bw.flush();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally { // always close the file
if (bw != null) {
try {
bw.close();
} catch (IOException ioe2) {
// just ignore it
}
}
}
As advised by Joachim Sauer, instead of using FileWriter
, you can use
new OutputStreamWriter(new FileOutputStream(..), ecnoding)
if you want to specify the encoding of the file. FileWriter
uses the default encoding, which changes from installation to installation.
Bozho
2010-07-15 08:27:04
Are you going to edit that to at least use a txt file so that it doesn't look so much like you just trawling for any rep you can get, even if the question doesn't make a whole lot of sense.
Lazarus
2010-07-15 08:30:48
...new FileWriter("checkbook.dat", true) --> the second parameter "true" means "append to the file"...
pgras
2010-07-15 08:34:22
Using a `FileWriter` means that you're pretty much ignoring the entire [encoding problem](http://www.joelonsoftware.com/articles/Unicode.html], which is a dangerous thing to do!
Joachim Sauer
2010-07-15 08:35:34
@Lazarus well, the extension of the file doesn't matter at all, if it is a text one. And the remark about the rep - you'd better spare those.
Bozho
2010-07-15 08:47:03
@Joachim Sauer - fair point. I added an update to warn about it.
Bozho
2010-07-15 08:49:27
@Bozho: actually, the alternative you presented **also** uses the default encoding of the file ;-) You'll need to provide a second argument to the `OutputStreamWriter` constructor.
Joachim Sauer
2010-07-15 08:50:12
hah, yes, I omitted all the arguments, but shouldn't have omitted this one.
Bozho
2010-07-15 08:51:23
@Bozho While it doesn't matter technically, your response doesn't speak to the OP's question specifically. What your answer looks like is a cut and paste without any real thought or explanation, that is until you edited the answer following some of the comments. Given all that I really does look like a 'Quick, get an answer, any answer, online even though the question is unclear so that I have max opportunity to get some rep'. I have no intention of sparing my opinion about your motives when you make such little effort, threats or not threats.
Lazarus
2010-07-15 11:30:55
@Lazarus - threats? eh.. my motives are to provide answers to what I know. Instead of writing this code, I copied it - after I verified it is what I would've written. You can check the tags I'm answering in, and you'll get a picture of how reputation-oriented my answers are (apart from the 'java' tag, the rest are not quite upvoted, apart from 2-3 enthusiasts). And this question was clear, albeit short.
Bozho
2010-07-15 11:33:53
@Bozho "And the remark about the rep - you'd better spare those" <- What would you call that other than a veiled threat? If your motives are good, then your conscience clear, then good for you. I applaud such people. If you really found the original question clear (it hadn't been edited when you posted your answer) then I'm astonished, you are obviously a much better person than I.
Lazarus
2010-07-15 12:19:25
+2
A:
English term to look for: "append"
You need to perform the following steps:
- open the file.
- append the string.
- close the file.
Read about the FileOutputStream class.
Anax
2010-07-15 08:28:59
`FileOutputStream` alone is the wrong tool when you want to write text to a .txt file. You will want to use a `Writer`. In this case you want to use a `OutputStreamWriter` wrapped around a `FileOutputStream` (there's also the `FileWriter`, but it's broken since it doesn't support [specifying an encoding](http://www.joelonsoftware.com/articles/Unicode.html)).
Joachim Sauer
2010-07-15 08:31:31