如何创build一个文件 – 包括文件夹 – 给定的path?
我正在从网上下载一个zip文件。 它包含文件夹和文件。 使用ZipInputstream和ZipEntry解压缩它们。  Zipentry.getName将文件的名称作为htm/css/aaa.htm 。 
 所以我正在创build新的File(zipentry.getName); 
 但问题是抛出一个exception: File not found 。 我知道它是创build子文件夹htm和css 。 
我的问题是:如何创build一个文件,包括其子目录,通过上面的path?
用这个:
 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); } 
 虽然可以在不检查结果的情况下执行file.getParentFile().mkdirs()但检查操作的返回值被认为是最佳做法。 因此,首先检查现有的目录,然后检查是否成功创build(如果尚不存在)。 
参考:
- File.getParentFile()
- 文件已存在()
- File.mkdir()
- File.mkdirs()
您可以使用Google的番石榴图书馆用几个文件类行:
 Files.createParentDirs(file); Files.touch(file); 
如果需要的话,您需要创build子目录,当您循环浏览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 } } 
  File在File对象上使用.mkdirs()方法的File : http : .mkdirs() 
         isDirectoryCreated =(new File(“../ path_for_Directory / Directory_Name”))。mkdirs();
        如果(!isDirectoryCreated) 
         {
             //目录创build失败
         }
这是我如何做到的
 static void ensureFoldersExist(File folder) { if (!folder.exists()) { if (!folder.mkdirs()) { ensureFoldersExist(folder.getParentFile()); } } }