如何创建一个文件,其中包括一个给定的路径文件夹? [英] How to create a file , including folders for a given path?

查看:194
本文介绍了如何创建一个文件,其中包括一个给定的路径文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是从网上下载的ZipFile。它包含文件夹和文件。 Uncom $ P $使用zipInputstream和ZipEntry的pssing他们。 zipentry.getName给文件名为HTM / CSS / aaa.htm。

Am downloading a ZipFile from web. where it contain folders and files. Uncompressing them using zipInputstream and zipentry . zipentry.getName giving name of file as "htm/css/aaa.htm".

所以我创建新的文件(zipentry.getName);

so i am creating New File(zipentry.getName);

但问题就抛出一个异常文件未找到。我得到了它正在创建子文件夹的htm和CSS。我的问题是,如何创建包括其子目录中的文件,通过将上述路径?

But problem it throwing an exception File not found. i got that it is creating subfolders htm and css. My question is that , how to create a file including its sub directories , by passing above path ?

谁能理清这个问题,请要感谢你。

Can anyone sort out this problem , please am thankful to you .

感谢你, 西SRINIVAS

Thanking you, Sai Srinivas

推荐答案

使用这样的:

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()不检查的结果,它被认为以检查操作的返回值的最佳实践。因此,检查现有的目录,然后再检查成功创建(如果不存在它尚未)。

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).

参考:

  • <一个href="http://download.oracle.com/javase/6/docs/api/java/io/File.html#getParentFile%28%29">File.getParentFile()
  • <一个href="http://download.oracle.com/javase/6/docs/api/java/io/File.html#exists%28%29">File.exists()
  • <一个href="http://download.oracle.com/javase/6/docs/api/java/io/File.html#mkdir%28%29">File.mkdir()
  • <一个href="http://download.oracle.com/javase/6/docs/api/java/io/File.html#mkdirs%28%29">File.mkdirs()
  • File.getParentFile()
  • File.exists()
  • File.mkdir()
  • File.mkdirs()

这篇关于如何创建一个文件,其中包括一个给定的路径文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆