my dog learned polymorphism*
The moose likes Beginning Java and the fly likes append by just PrintWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "append by just PrintWriter " Watch "append by just PrintWriter " New topic
Author

append by just PrintWriter

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 606
Hello!

Say I have a file contents text, I want append new text only by PrintWriter. How can I do that?
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 373
    
    1

Does this do what you want?

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 606


PrintWriter deletes Ya ali string.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13872
    
  10

That's not because of the PrintWriter, but because you're not opening the file correctly. You need to open it for appending.

Look closely at line 20 of Stevens' code. Note that there's an extra parameter which is true. This indicates that the file should be opened for appending.

Look at the API documentation of class FileWriter. It also has a constructor that takes a parameter to indicate that you want to append to the file. You'll have to add that to line 11 of your code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 606


BE advised that there is ali.txt in main direct of the program. When I open in directory of program, There is no text in that file. Why?
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 606

I fixed it myself
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: append by just PrintWriter
 
Similar Threads
writing a newline char into a text file
Line Feed Carriage Return problem
How to append a Text file?
PrintWriter append
RandomAccessFile newLine in notepad