This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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: 635
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: 523
    
    3

Does this do what you want?

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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: 635


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: 635

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