• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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: 4321
59
  • 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.
 
Straws are for suckers. Now suck on this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic