0

Established Fact: application does not need to be platform independent.

I have been sitting here for a while and I don't know why this is causing me so much of an issue. What I want to do is this:

1) check to see if a file exists
2) if it doesn't exist, create it and then let me know
3) if it does exist, don't try to write over it anyway, just do nothing and let me know

String pathToChange = "C:/Program Files/WOTA".replace("/", "\\");
    JOptionPane.showMessageDialog(rootPane, pathToChange);
    File file = new File(pathToChange);
    if (!file.exists()) {
        file.mkdirs();
        if (file.mkdir()) {JOptionPane.showMessageDialog(rootPane, "C:/Program            Files/WOTA was created."); }
        else { JOptionPane.showMessageDialog(rootPane, "Did not create.");
    }

    }

I don't know why but this is giving me a lot of trouble but it is. Oh, and you'll notice that I am having a JOptionPanel (Dialog) pop up with the file name that it is trying to create so that I know what is getting handed off is correct.

Can anyone kindly point out why this is not working and what I will need to do to make it work. More importantly, since I am a prideful bastard and I don't like others doing my work for me, please tell me why it wouldn't work.

Btw, I am building all of this in NetBeans.

Thank you!

7
  • 2
    Kindly share your code... Commented Sep 27, 2013 at 21:36
  • Crap! Sorry. +1 for that, though. :) Commented Sep 27, 2013 at 21:46
  • 1
    The piece of code is working fine on my machine. However what is rootPane? and are you sure you have permission to write files on C drive? Commented Sep 27, 2013 at 21:53
  • 1
    File#mkdirs will return true/false itself. If you then try and call File#mkdir, it is likely to return false because the directory may already exist. Also, beware, under Windows 7, the UAC/security model can restrict you from making directories in protected folders, like Program Files... Commented Sep 27, 2013 at 22:13
  • 1
    You can easily check weather you can write into specified folder by using File#canWrite() Commented Sep 27, 2013 at 22:14

2 Answers 2

2

The line file.mkdirs(); will create the folder that you are trying to create. Then in your if(file.mkdir()) statement, it is attempting to create the file again. The way the code is written, you will always get the "Did not create" but the folder should still appear.

Sign up to request clarification or add additional context in comments.

2 Comments

I disagree. If file.exists() returns anything but true, it will run the else code. If file.exists() returns true and if the folder is created, it will notify via JOptionPane (Dialog). The embedded if statement is to let me know when that condition is met, if it creates the folder or not.
@Rincewind: I don't know why you disagree, but what jhnewkirk says is true. If you look at the Javadoc of File (docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdir%28%29), mkdir() returns true ONLY if the directory is created, but it won't because you already thid with mkdirs().
0

File#mkdirs will return false on it's own accord.

A better approach might be to use something more like...

if (!file.exists() && !file.mkdirs()) {
    // Can not make the directory
} else {
    // Directories exists or was created
}

Under Windows 7, the UAC and updated security model, you may not be able to write to certain locations on the disk, including Program Files (we've had this issue at work :P).

Even worse, under Java 6, File#canWrite can return a false positive (ie, return true when you can't write to the specified location). The really bizarre thing we found was that you could even try and write to a file with it raising an exception...

What we've done in the past is use File#canWrite, if returns true, we actually write a file to the specified location, check to see if it exists and check the contents of the file.

If this works, only then do we trust the result.

As I understand it, this may have being fixed in Java 7...Thank you Windows :P

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.