wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes CreateNewFile() not working, canRead() & canWrite() both true Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "CreateNewFile() not working, canRead() & canWrite() both true" Watch "CreateNewFile() not working, canRead() & canWrite() both true" New topic
Author

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

Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 63
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!


Life is 10% what happens and 90% how you react to it.
Attitude is everything.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

So... if "database.exists()" returns true, you would see the output you are seeing.
Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 63
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
jQuery in Action, 2nd edition
 
subject: CreateNewFile() not working, canRead() & canWrite() both true
 
Similar Threads
Pulling up File Permissions using Java API ???
File object creation
btSendFile
Does anyone know what these error means?
Ensuring that a file is not currently being written to