Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing data to a text file

 
mary caldwell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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!

 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • 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
  • 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).
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15281
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic