wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Text is not being written to file 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 "Text is not being written to file" Watch "Text is not being written to file" New topic
Author

Text is not being written to file

Gulshan Singh
Ranch Hand

Joined: Apr 22, 2009
Posts: 41
Basically, this part of my program is like a text editor. You enter in a file name, it loads the file into a text area, you can make changes, and when you click save it writes the new text to the file. This is the code,


It loads the file fine, but when I write it to the file, it simply erases everything in the file. This is only part of the code, if you can't tell whats wrong from here I can post all of it, but its kinda long... Just let me know. Thanks.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2988
    
    9

This has two big problems. One is the empty catch block - if anything goes wrong, this is hiding information from you. You need to make sure that if an error occurs, you get some information about it. The simplest way to do this is to print a stack trace:

Later you can learn how to use a logger like log4j. But this is good enough to start.

The second problem, probably the one that's preventing the file frombeing written correctly, is that you never close the writer. It's probably sufficient to just add buffin.close() after the write. To do it right, though, you really should get in the habit of putting the close in a finally block:
Gulshan Singh
Ranch Hand

Joined: Apr 22, 2009
Posts: 41
Thank you, it worked.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Text is not being written to file
 
Similar Threads
filereader
GUI: Cut/Copy/Paste/Etc.
Weird actionPerformed method.
Help with something Simple
Which class should I use to write into a text file.