为什么FileWriter不创建新文件?FileNotFoundException

see*_*ker 6 java file-io filewriter

所以我有一个如下的代码片段。我试图找出为什么它抛出FileNotFoundException。

File file= new File (WORKSPACE_PATH+fname);
FileWriter fw;
if (file.exists())
{
     fw = new FileWriter(file,true);//if file exists append to file. Works fine.
}
else
{
     fw = new FileWriter(file);// If file does not exist. Create it. This throws a FileNotFoundException. Why? 
}
Run Code Online (Sandbox Code Playgroud)

nbz*_*nbz 6

创建文件时使用连接不会添加必要的路径分隔符。

File file = new File(WORKSPACE_PATH, fname);
Run Code Online (Sandbox Code Playgroud)

  • 他确实提到打开追加工作正常吗? (3认同)