2015-03-13 489 views
-4

我正在寻求澄清Java API文档对FileWriter类的说明。该文件规定如下:如果文件不存在,Java的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对象是否会尝试创建由文件名字符串指定的文件,但很显然,该对象将检查,看看是否如果无法创建文件,则会创建文件并引发异常。

+2

是的,它的确如此。提示未来:如果你不确定,测试这样的东西并不需要很长时间... – 2015-03-13 17:37:54

+1

我想这个问答有助于你。 http://stackoverflow.com/questions/8630484/why-new-filewriterabc-txt-creates-a-new-file-and-new-fileabc-txt-does-no – jungyh0218 2015-03-13 17:38:42

+0

该文档说:*抛出IOException如果命名文件[...]不存在但无法创建*。这并不意味着该方法尝试创建文件,如果它不存在? – 2015-03-13 17:40:19

回答

1

是的,它会被创建(如果它不存在的话)。如果文件不能创建,抛出一个IOException。

相关问题