File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java IO Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java IO Problem" Watch "Java IO Problem" New topic
Author

Java IO Problem

Adeiza Yusuf
Greenhorn

Joined: Oct 06, 2010
Posts: 24
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
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Adeiza Yusuf
Greenhorn

Joined: Oct 06, 2010
Posts: 24
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

Did you check the constructors ? Copy/paste them here.
Adeiza Yusuf
Greenhorn

Joined: Oct 06, 2010
Posts: 24
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.
Eduardo Yañez Parareda
Ranch Hand

Joined: Oct 09, 2008
Posts: 92

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.


http://serfj.sourceforge.net - Simplest Ever REST Framework for Java
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Do you see anything interesting in that list ? Something which may help you append data to a file ?
Adeiza Yusuf
Greenhorn

Joined: Oct 06, 2010
Posts: 24
Thanks for pointing that out.
Adeiza Yusuf
Greenhorn

Joined: Oct 06, 2010
Posts: 24
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Oct 06, 2010
Posts: 24
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.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Java IO Problem