2015-03-13 32 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。

相關問題