简体   繁体   English

为什么Java mkdirs不能正常工作?

[英]Why isnt Java mkdirs working properly?

I wrote some code to create folders in a Java app running under Windows. 我写了一些代码来在Windows下运行的Java应用程序中创建文件夹。 It's not creating the folders but not going into exception though. 它不是创建文件夹但是不会进入异常。

public static String createFolders(String client) {
    File folder = new File("/Users/Juan Manuel/Clientes/"+client);

    if (!folder.exists()) {

        try {
            folder.mkdirs();
            new File(folder.getPath()+"/IMSS").mkdir();
            new File(folder.getPath()+"/SAT").mkdir();
            new File(folder.getPath()+"/Finanzas").mkdir();
            new File(folder.getPath()+"/Otros").mkdir();
        }
        catch (Exception e) {
            return e.toString();
        }

    }
    return "";
}

I use the return value to check for any error but I get "" returned, so it's at least going through. 我使用返回值检查是否有任何错误,但我得到""返回,所以它至少会通过。

At Explorer's intended root path it's showing as C:\\Users\\Juan Manuel\\Clientes . 在Explorer的预期根路径中,它显示为C:\\Users\\Juan Manuel\\Clientes

According to the documentation, you are going to see an exception only when SecurityException is thrown. 根据文档,只有在抛出SecurityException时才会看到异常。 If the code is unable to create a folder for any other reason, your code is not going to detect it, because it ignores the return code. 如果代码由于任何其他原因无法创建文件夹,则代码不会检测到它,因为它会忽略返回代码。

Change your method as follows to pay attention to the error: 更改您的方法如下,以注意错误:

try {
     if (!folder.mkdirs()) {
         return "root";
     }
     if (!(new File(folder.getPath()+"/IMSS").mkdir())) {
         return "IMSS";
     }
     if (!(new File(folder.getPath()+"/SAT").mkdir())) {
         return "SAT";
     }
     if (!(new File(folder.getPath()+"/Finanzas").mkdir())) {
         return "Finanzas";
     }
     if (!(new File(folder.getPath()+"/Otros").mkdir())) {
         return "Otros";
     }
} catch (...)

I didn't run your code in MS Window OS, but I'm not sure if c:\\Users~ is equivalent to /Users~ in Java. 我没有在MS Window OS中运行你的代码,但我不确定c:\\ Users~是否等同于Java中的/ Users~。

If you sure it's not go into your exception handling part, that means Java create file somewhere in your disk, not under C:\\Users\\Juan Manuel\\Clientes though. 如果你确定它没有进入你的异常处理部分,那就意味着Java创建文件在磁盘的某个地方,而不是在C:\\ Users \\ Juan Manuel \\ Clientes下。 Try to print what's absolute path of your "folder" file's path is and take a look if it's already exist or not. 尝试打印“文件夹”文件路径的绝对路径,看看它是否已经存在。

So in catch statement. 所以在catch语句中。 do e.printStackTrace(); 做e.printStackTrace(); first instead of returning error message if your program calling createFolders() doesn't print return strings. 如果您的程序调用createFolders()不打印返回字符串,则首先而不是返回错误消息。 Checking e.toString() is sometimes not perfect to figure out root cause. 检查e.toString()有时不能完全找出根本原因。

Also, try to use proper File create as Boris mentioned, I know that your calling would work ( since I was using it as well ), but using File( path, filename ); 另外,尝试使用正确的文件创建,如Boris所提到的,我知道你的调用会起作用(因为我也使用它),但是使用File(path,filename); is more explicit and less error prone. 更明确,更不容易出错。

Lastly, check File class' JavaDoc http://docs.oracle.com/javase/7/docs/api/java/io/File.html#File(java.lang.String) 最后,检查File类'JavaDoc http://docs.oracle.com/javase/7/docs/api/java/io/File.html#File(java.lang.String)

It will return NPE when given path is null, but it won't return exception for other cases. 当给定路径为空时,它将返回NPE,但是对于其他情况,它不会返回异常。

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

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