简体   繁体   English

使用Java在Windows中创建新文件夹时遇到问题

[英]Trouble with creating a new folder in Windows using Java

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 1)检查文件是否存在
2) if it doesn't exist, create it and then let me know 2)如果它不存在,创建它然后让我知道
3) if it does exist, don't try to write over it anyway, just do nothing and let me know 3)如果它确实存在,不要试图写它,只是什么也不做,让我知道

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. 哦,你会注意到我正在弹出一个JOptionPanel(Dialog),其中包含它正在尝试创建的文件名,以便我知道传递的内容是正确的。

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. 顺便说一句,我正在NetBeans中构建所有这些。

Thank you! 谢谢!

The line file.mkdirs(); 行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. 然后在if(file.mkdir())语句中,它尝试再次创建该文件。 The way the code is written, you will always get the "Did not create" but the folder should still appear. 编写代码的方式,您将始终获得“未创建”但仍应显示该文件夹。

File#mkdirs will return false on it's own accord. File#mkdirs将自行返回false

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). 在Windows 7,UAC和更新的安全模型下,您可能无法写入磁盘上的某些位置,包括Program Files (我们在工作中遇到过这个问题: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). 更糟糕的是,在Java 6下, File#canWrite可以返回false (即,当您无法写入指定位置时返回true )。 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. 我们过去所做的是使用File#canWrite ,如果返回true ,我们实际上将文件写入指定的位置,检查它是否存在并检查文件的内容。

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 据我了解,这可能已在Java 7中得到修复...谢谢Windows:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM