一个java创建文件的问题
File file = new File("c:/temp/a.txt");if (!file.exists()) {
// if its parent directories doesn't exist, then create its parent directories
if (!(new File(file.getParent()).exists()))
(new File(file.getParent())).mkdirs();
file.createNewFile();
}
很简单的程序,运行应该不会有什么问题。
但是前两天,我不知道有mkdirs(),而是用mkdir()。于是问题就来了,如果temp这个文件夹不存在,系统就会报错。所以想问一下高手,如果不用mkdirs(),这个问题改如何解决。我当初考虑用嵌套,看文件夹是不是存在,如果不存在,再看上一级的文件夹存不存在,结果自己也就糊涂了。。。 原帖由 ─壶酒 于 2007-11-22 15:49 发表 http://www.dolc.de/forum/images/common/back.gif
这行
file.createNewFile();
没有try/catch IOException
$汗$
假如把try catch放在方法上,就没什么问题啊 不明白你要干什么, file.createNewFile()有啥用,你要写什么东西直接往里写就是了,没必要建个空文件. 如果只是临时用的可以用static createTempFile(String prefix, String suffix)
Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
或者用System.get...大概是tmp.dir的值吧,取得系统临时文件夹位置,在里面存就是了. 原帖由 renzaijianghu 于 2007-11-24 11:32 发表 http://www.dolc.de/forum/images/common/back.gif
不明白你要干什么, file.createNewFile()有啥用,你要写什么东西直接往里写就是了,没必要建个空文件. 如果只是临时用的可以用static createTempFile(String prefix, String suffix)
Creates an empt ...
如果文件不存在,那么必须首先创建这个文件。
页:
[1]