Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

append by just PrintWriter

 
abalfazl hossein
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?
 
Stevens Miller
Bartender
Posts: 1229
23
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15288
39
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic