• 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

Writing data to a text file

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,
I'm having trouble writing data to a text file. The code I have right now compiles and even gives me System.out.println messages that suggest it is working, but it doesn't seem to be actually creating the text file (or at least I can't find it). Any help with this would be awesome. Thanks in advance!

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's with putting quotes in the filename? In Windows I'm not allowed to create files with quotes in their names. You're probably using Windows too, and even if you aren't, that probably isn't a valid filename.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably best to print rather more details from the catch; a stack trace might give useful information.
 
mary caldwell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm actually on a mac, but I was just trying the quotes out to see if it made a difference (it didn't). My catch block is not catching anything, either (when I run, it prints the "working" from within the try block).
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, change the content of the catch block as follows:

This way, you will get more useful information than just "not working" if an IOException is thrown.

Second, after the file.close(); statement, put a System.out.println statement to print "done", and see if it gets printed - there might be a runtime exception happening that you don't see.

Also, don't do this: new File("\"" + filename + "\"")

It will create a file with literally quotes in the name. That's most likely not what you want. Just do this: new File(filename)

Finally, call file.flush() before file.close() to make sure that BufferedWriter writes its cache to the file.
 
mary caldwell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you everyone for the replies. It apparently is creating the text file correctly now (it works when I call my load file method), but I still can't find where it is saving to.... hmm... oh well! At least it works, if mysteriously. Thanks again!
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change the first line of the try block to
This will print out the filename you supply and the absolute path of your file that Java uses. These may or may not be the same. If they are different and you don't understand why, come back and someone should be able to explain.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic