• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Text is not being written to file

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, it worked.
 
reply
    Bookmark Topic Watch Topic
  • New Topic