简体   繁体   English

File.mkdir无法正常工作,我不明白为什么

[英]File.mkdir is not working and I can't understand why

I've this brief snippet: 我有一个简短的摘要:

String target = baseFolder.toString() + entryName;
                target = target.substring(0, target.length() - 1);
                File targetdir = new File(target);
                if (!targetdir.mkdirs()) {
                    throw new Exception("Errore nell'estrazione del file zip");
                }

doesn't mattere if I leave the last char (that is usually a slash). 是否保留最后一个字符并不重要(通常是一个斜杠)。 It's done this way to work on both unix and windows. 这样可以在Unix和Windows上工作。 The path is actually obtained from the URI of the base folder. 该路径实际上是从基本文件夹的URI获得的。 As you can see from baseFolder.toString() (baseFolder is of type URI and is correct). 从baseFolder.toString()中可以看到(baseFolder是URI类型,并且是正确的)。 The base folder actually exists. 基本文件夹实际上存在。 I can't debug this because all I get is true or false from mkdir, no other explanations.The weird thing is that baseFolder is created as well with mkdir and in that case it works. 我无法调试它,因为从mkdir获得的所有信息都是对还是错,没有其他解释。奇怪的是baseFolder也可以通过mkdir创建,并且在这种情况下可以正常工作。

Now I'm under windows. 现在我在窗户下。


the value of target just before the creation of targetdir is "file:/C:/Users/dario/jCommesse/jCommesseDB" if I cut and paste it (without the last entry) in windows explore it works... 如果我在Windows探索中将其剪切并粘贴(没有最后一个条目),则在创建targetdir之前,target的值是“ file:/ C:/ Users / dario / jCommesse / jCommesseDB”。

The path you provide is not a file path, but a URI. 您提供的路径不是文件路径,而是URI。 I suggest you try the following : 我建议您尝试以下方法:

URI uri = new URI("file://c:/foo/bar");
File f = new File(uri).

在我看来,似乎开头的“文件:/”是问题所在...尝试使用getAbsolutePath()而不是toString()。

The File constructor taking a String expects a path name. 带有StringFile构造函数需要一个路径名。 A path name is not an URI. 路径名不是 URI。

Remove the file:/ from the front of the String (or better yet, use getPath() instead of toString() ) to get to the path you need. 从String的file:/删除file:/ (或者更好的是,使用getPath()而不是toString() )到达所需的路径。

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

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