This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

append by just PrintWriter  RSS feed

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

Say I have a file contents text, I want append new text only by PrintWriter. How can I do that?
 
Bartender
Posts: 1460
31
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this do what you want?

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


PrintWriter deletes Ya ali string.
 
Java Cowboy
Sheriff
Posts: 16082
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I fixed it myself
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!