给定正确的文件路径时,createNewFile() 抛出 FileNotFoundException
createNewFile() Throws FileNotFoundException When Given the Correct FilePath
下面是我的代码:
public void playerNaming() throws IOException {
Scanner pickName = new Scanner(System.in);
System.out.println("What do you want your username to be?");
String playerName = pickName.nextLine();
userName = playerName;
File file1 = new File("PlayerFiles\" + playerName + ".txt");
File file2 = new File(file1.getAbsolutePath());
System.out.println(file2);
file2.createNewFile();
BufferedWriter file3 = new BufferedWriter(new FileWriter(file2));
}
在线file2.createNewFile();
它抛出
java.io.FileNotFoundException: (在此插入正确的文件路径)系统找不到指定的路径
怎么了?根据我阅读的所有文章和其他 Whosebug 问题,这应该有效。
检查你的文件路径:
public static void main(String args[])
{
try {
// Get the file
File f = new File("F:\program1.txt");
// Create new file
// if it does not exist
if (f.createNewFile())
System.out.println("File created");
else
System.out.println("File already exists");
}
catch (Exception e) {
System.err.println(e);
}
注意:文件“F:\program.txt”是F:目录中的现有文件。
下面是我的代码:
public void playerNaming() throws IOException {
Scanner pickName = new Scanner(System.in);
System.out.println("What do you want your username to be?");
String playerName = pickName.nextLine();
userName = playerName;
File file1 = new File("PlayerFiles\" + playerName + ".txt");
File file2 = new File(file1.getAbsolutePath());
System.out.println(file2);
file2.createNewFile();
BufferedWriter file3 = new BufferedWriter(new FileWriter(file2));
}
在线file2.createNewFile();
它抛出
java.io.FileNotFoundException: (在此插入正确的文件路径)系统找不到指定的路径
怎么了?根据我阅读的所有文章和其他 Whosebug 问题,这应该有效。
检查你的文件路径:
public static void main(String args[])
{
try {
// Get the file
File f = new File("F:\program1.txt");
// Create new file
// if it does not exist
if (f.createNewFile())
System.out.println("File created");
else
System.out.println("File already exists");
}
catch (Exception e) {
System.err.println(e);
}
注意:文件“F:\program.txt”是F:目录中的现有文件。