• Post Reply Bookmark Topic Watch Topic
  • New Topic

PrintWriter not writing to a created file.

 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code below creates the 2 files successfully, but it is not able to write the sample data into the newly created file. I can't figure out the reason why.

Another strange thing is that when I tried inserting System.out.println calls for debugging, nothing prints out.

Any guidance is appreciated.

 
Tony Docherty
Saloon Keeper
Posts: 3142
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you get no output from your print statements then the File.notExists(..) must be returning false. Are you sure you shouldn't be checking for the file existing.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you going through all that business with trying to create the file if it doesn't already exist, when the FileWriter is already doing that for you?

In fact, that's why you are confused. The FileWriter creates the file, and then your code which checks whether the file exists tells you that it does, and the code inside that if-block is not executed.
 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tip, Paul Clapham. I didn't realise that FileWriter does create an empty file if it is not existent.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going to refer you to the API documentation for FileWriter, but I read it first and was surprised to see that it doesn't actually say that the class will (try to) create a file if it doesn't already exist. Some of the documentation sort of implies that, but none of the documentation explicitly says so. However, as you see, it does (try to) create files which don't exist.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!