This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Java IO Problem  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this might be a piece of cake for some but its proving a little difficult for me. i am designing a test project that will be saving user information to a text file and updates the file anytime the user returns. My problem is that each time i run the application, it keeps creating a new file this overwriting the existing one and writing only the new information in the text file. I will just write a little code to demonstrate my issues.

* //This one goes well but if i run the program again with same user information which means the file already exists just make changes but it only writes the new information.





Thanks
 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I know this might be a piece of cake for some


A piece of cake for anybody who checks the API of the FileWriter class. Hint: look at the various contructors of that class.
 
Adeiza Yusuf
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried everything. each time i run the program it create a new file file instead of updating the existing one. I use an if statement to test if file exist yet it doesn't work.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you check the constructors ? Copy/paste them here.
 
Adeiza Yusuf
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Christophe Verré wrote:Did you check the constructors ? Copy/paste them here.



As you Requested.


Constructor Summary
FileWriter(File file)
Constructs a FileWriter object given a File object.
FileWriter(File file, boolean append)
Constructs a FileWriter object given a File object.
FileWriter(FileDescriptor fd)
Constructs a FileWriter object associated with a file descriptor.
FileWriter(String fileName)
Constructs a FileWriter object given a file name.
FileWriter(String fileName, boolean append)
Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.
 
Ranch Hand
Posts: 93
IntelliJ IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then...


Constructor Summary
FileWriter(File file)
Constructs a FileWriter object given a File object.
FileWriter(File file, boolean append)
Constructs a FileWriter object given a File object.
FileWriter(FileDescriptor fd)
Constructs a FileWriter object associated with a file descriptor.
FileWriter(String fileName)
Constructs a FileWriter object given a file name.
FileWriter(String fileName, boolean append)
Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you see anything interesting in that list ? Something which may help you append data to a file ?
 
Adeiza Yusuf
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for pointing that out.
 
Adeiza Yusuf
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes! it worked but i've got another problem, when i try to save a string to a file, it does not recognize the new line "\n" so everything is saved in the text file in one line. any alternative to the newline? how do i keep the string format same when it is saved in the text file.

thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PrintWriter has some delicious methods for you. You are using write(), but there are alternatives. Can you find them in the PrintWriter API ?
 
Adeiza Yusuf
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes I have tried using the write also, the closest to getting what i want is using the println(string) method that way i have to write the each line.




when i pass the message as an argument to the println() method, its prints everything in a straight line in the text file ignoring the "\n"

so i had to use a more cumbersome way which i don't think its the best.


leaving out the message variable.

 
Marshal
Posts: 60068
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are methods in the java.util.Formatter class whihc may make that easier, but I have never found a way to make a Formatter append rather than overwrite.
Never use an empty catch block; if an exception occurs, you would not know about it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!