This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CreateNewFile() not working, canRead() & canWrite() both true

 
Derek Boring
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I've been having this issue for a week and no amount of Google searches has helped me figure it out. My interface is written in Swing, but I use the AWT FileDialog to have the user choose a file to save their data to. (I chose this method because it uses the native file chooser since the JFileChooser is absolutely horrid and completely unusable on Mac) Once the user chooses the file, I get the File from the FileDialog object and then check if it exists. If it does not, I use the File class's createNewFile() method. However, the method returns false and does not create a new file. canRead() and canWrite() both return true. I have tried choosing several different locations to save to, (even the root directory) but I get exactly the same result no matter where I choose. Is there something I am missing?
Here is my code:



The System.out.println() displays this result:

file created: false Read/Write Permissions: true/true

and, of course, no file is actually created.
Just in case it makes a difference, here is my development environment info:

Intel based MacBook Pro, OS X 10.9.2
Eclipse, Version: Indigo Service Release 2, Build id: 20120216-1857
Java, Version 7, Build 51 (build 1.7.0_51-b13)

Can anyone please help me figure out why it's not creating a file?

Let me know if you need any other information. Thank you in advance, you guys here are all freaking awesome!
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So... if "database.exists()" returns true, you would see the output you are seeing.
 
Derek Boring
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an exclamation point in front of database.exists(), thus if it returns false, the code following the if statement would be executed.
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, let me put that more clearly. If "database.exists()" returned true then the code controlled by the if-statement would not be executed, and then the output you observe would be the output of that code. So my hypothesis is that "database.exists()" returns true. That's a testable hypothesis.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic