2

Possible Duplicate:
How to append text to an existing file in Java

How can I add a string to the end of a .txt file?

Community
  • 1
  • 1
astring
  • 39
  • 1
  • 1
  • 5
  • 1
    To the filename? Or to the actual file contents? Would you be able to provide some more context about what you're trying to do? – Noel M Jul 15 '10 at 08:27
  • 2
    Could you give an example of what you try to achieve ??? What have you done so far ??? – pgras Jul 15 '10 at 08:27
  • 1
    @pgras: your question mark key seems to be stuck! – Joachim Sauer Jul 15 '10 at 08:53
  • @Joachim_Sauer That's a pretty big leap and assumption for your edit. How does 'the title' lead you to believe that this question is about the content rather than the filename? You've created a question where there really wasn't a clear one without input from the OP. – Lazarus Jul 15 '10 at 11:33
  • @Lazarus: I was simply assuming the more common operation (as did the people who answered before my edit, by the way). – Joachim Sauer Jul 15 '10 at 15:05
  • 1
    @Joachim_Sauer That's fair enough but if you read back to the original... could the OP be asking about renaming the file? Making assumptions, even, or especially, based on crowd responses might not always be the best approach. That's really all I was saying, the question was significantly ambiguous and vague to need further input from the OP. The fact that once edited it became a dupe is for another day. – Lazarus Jul 15 '10 at 15:36

2 Answers2

5

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
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 3
    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 Jul 15 '10 at 08:30
  • ...new FileWriter("checkbook.dat", true) --> the second parameter "true" means "append to the file"... – pgras Jul 15 '10 at 08:34
  • 3
    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 Jul 15 '10 at 08:35
  • @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 Jul 15 '10 at 08:47
  • @Joachim Sauer - fair point. I added an update to warn about it. – Bozho Jul 15 '10 at 08:49
  • @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 Jul 15 '10 at 08:50
  • hah, yes, I omitted all the arguments, but shouldn't have omitted this one. – Bozho Jul 15 '10 at 08:51
  • @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 Jul 15 '10 at 11:30
  • @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 Jul 15 '10 at 11:33
  • @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 Jul 15 '10 at 12:19
2

English term to look for: "append"

You need to perform the following steps:

  1. open the file.
  2. append the string.
  3. close the file.

Read about the FileOutputStream class.

Anax
  • 9,122
  • 5
  • 34
  • 68
  • 1
    `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 Jul 15 '10 at 08:31