• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Text is not being written to file

 
Gulshan Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, it worked.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic