如果文件不存在,Java的FileWriter对象是否会尝试创建文件?

时间:2015-03-13 17:36:01

标签: java io filewriter java-api

我想澄清Java API文档中有关FileWriter类的内容。文档说明如下:

constructor:
Constructs a FileWriter object given a file name.

public FileWriter(String fileName)
    throws IOException


fileName - String The system-dependent filename.

IOException - if the named file exists but is a directory rather than a
regular file, does not exist but cannot be created, or cannot be opened
or any other reason

我不清楚FileWriter对象是否会尝试创建fileName String指定的文件,尽管很明显该对象将检查文件是否已创建,如果可以,则抛出异常不被创造。

1 个答案:

答案 0 :(得分:1)

是的,它将被创建(如果它尚不存在。)如果无法创建文件,则抛出IOException。