Java 如何为给定的路径创建文件——包括文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4040624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:50:50  来源:igfitidea点击:

How to create a file -- including folders -- for a given path?

javaandroidfile

提问by Srinivas

Am downloading a zip file from web. It contain folders and files. Uncompressing them using ZipInputstreamand ZipEntry. Zipentry.getNamegives the name of file as htm/css/aaa.htm.

我正在从网上下载一个 zip 文件。它包含文件夹和文件。使用ZipInputstream和解压缩它们ZipEntryZipentry.getName给出文件的名称为htm/css/aaa.htm.

So I am creating new File(zipentry.getName);

所以我正在创造新的 File(zipentry.getName);

But problem it is throwing an exception: File not found. I got that it is creating subfolders htmand css.

但问题是抛出异常:File not found. 我知道它正在创建子文件夹htmcss.

My question is: how to create a file including its sub directories, by passing above path?

我的问题是:如何通过传递上面的路径来创建一个包含其子目录的文件?

采纳答案by Sean Patrick Floyd

Use this:

用这个:

File targetFile = new File("foo/bar/phleem.css");
File parent = targetFile.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
    throw new IllegalStateException("Couldn't create dir: " + parent);
}

While you can just do file.getParentFile().mkdirs()without checking the result, it's considered a best practice to check for the return value of the operation. Hence the check for an existing directory first and then the check for successful creation (if it didn't exist yet).

虽然您可以file.getParentFile().mkdirs()不检查结果,但检查操作的返回值被认为是最佳实践。因此,首先检查现有目录,然后检查是否成功创建(如果尚不存在)。

Reference:

参考:

回答by dogbane

You need to create subdirectories if necessary, as you loop through the entries in the zip file.

如有必要,您需要创建子目录,因为您循环浏览 zip 文件中的条目。

ZipFile zipFile = new ZipFile(myZipFile);
Enumeration e = zipFile.entries();
while(e.hasMoreElements()){
    ZipEntry entry = (ZipEntry)e.nextElement();
    File destinationFilePath = new File(entry.getName());
    destinationFilePath.getParentFile().mkdirs();
    if(!entry.isDirectory()){
        //code to uncompress the file 
    }
}

回答by Andrejs

You can use Google's guavalibrary to do it in a couple of lines with Filesclass:

您可以使用 Google 的番石榴库通过Files类在几行中完成此操作:

Files.createParentDirs(file);
Files.touch(file);

https://code.google.com/p/guava-libraries/

https://code.google.com/p/guava-libraries/

回答by parag.rane

Looks at the file you use the .mkdirs()method on a Fileobject: http://www.roseindia.net/java/beginners/java-create-directory.shtml

查看您.mkdirs()File对象上使用该方法的文件:http: //www.roseindia.net/java/beginners/java-create-directory.shtml

        isDirectoryCreated = (new File("../path_for_Directory/Directory_Name")).mkdirs();
        if (!isDirectoryCreated) 
        {
            // Directory creation failed
        }

回答by Gubatron

This is how I do it

这就是我的做法

static void ensureFoldersExist(File folder) {
    if (!folder.exists()) {
        if (!folder.mkdirs()) {
            ensureFoldersExist(folder.getParentFile());
        }
    }
}