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

Does Java's FileWriter object attempt to create a file if file does not exist?

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

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 字符串指定的文件,尽管很明显该对象会检查文件是否已创建,如果存在则抛出异常无法创建。

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