调用File#createNewFile是安全的,假设路径有效并且您对其具有写入权限。如果同名的文件已经存在,它只会返回 false:File f = new File("myfile.txt");if (f.createNewFile()) { // If there wasn't a file there beforehand, there is one now.} else { // If there was, no harm, no foul}// And now you can use it.