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?

6
  • 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? Commented Jul 15, 2010 at 8:27
  • 2
    Could you give an example of what you try to achieve ??? What have you done so far ??? Commented Jul 15, 2010 at 8:27
  • 1
    @pgras: your question mark key seems to be stuck! Commented Jul 15, 2010 at 8: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. Commented Jul 15, 2010 at 11:33
  • 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. Commented Jul 15, 2010 at 15:36

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

10 Comments

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.
...new FileWriter("checkbook.dat", true) --> the second parameter "true" means "append to the file"...
Using a FileWriter means that you're pretty much ignoring the entire [encoding problem](joelonsoftware.com/articles/Unicode.html], which is a dangerous thing to do!
@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.
@Joachim Sauer - fair point. I added an update to warn about it.
|
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.

1 Comment

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).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.