Java.IO.Filenotfoundexception错误,找不到C中存在的文件:

Java.IO.Filenotfoundexception错误,找不到C中存在的文件:,java,file,backslash,Java,File,Backslash,我最近开始编程,无法让我的程序找到一个文件,然后从中读取输入。表示该文件不存在。这是我的密码 At line Scanner inputfile=new Scannerf;。出现上面提到的错误。另外,当提示在程序中键入文件名时,我会输入C:/Games.txt…..但是当我得到要打印的文件名时,文件名被注册为C:\Games.txt…..为什么正斜杠会变成反斜杠。谢谢你抽出时间来帮助我 为什么正斜杠变成了反斜杠 因为您在Windows上,目录本机由一个\ 接下来,您似乎没有使用PrintWrit

我最近开始编程,无法让我的程序找到一个文件,然后从中读取输入。表示该文件不存在。这是我的密码

At line Scanner inputfile=new Scannerf;。出现上面提到的错误。另外,当提示在程序中键入文件名时,我会输入C:/Games.txt…..但是当我得到要打印的文件名时,文件名被注册为C:\Games.txt…..为什么正斜杠会变成反斜杠。谢谢你抽出时间来帮助我

为什么正斜杠变成了反斜杠

因为您在Windows上,目录本机由一个\

接下来,您似乎没有使用PrintWriter进行写作。如果要检查存在的文件,请调用


确保名为file的文件夹存在,以便创建文件。如果它不在那里,它可能会抛出错误。为了阅读,你需要有适当的权利

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Answer {

    public static void main(String args[]) throws IOException, FileNotFoundException {

        // Have to throw a FileNotFoundException just in case an error occurs the compiler needs to know how to process the error.

        PrintWriter pw = new PrintWriter("C:/file/Summary.txt");
        Scanner k = new Scanner(System.in);
        String filename;


        System.out
                .println("--------------------------------\nBowsers Nuclear Weapons Inventory\n"
                        + "---------------------------------");
        System.out.print("Please enter the name of the file: ");
        filename = k.nextLine(); //Input for strings

        System.out.println(filename);
        File f = new File("C:/file/"+filename+".txt"); //Must have a location for your files
        f.createNewFile(); //The file's pathname is the only thing that you can supply when you instantiate the object
        //you actually have to invoke the createNewFile method upon the object.
        if(f.exists()) { //Don't be afraid to check your code this is a must for every programmer.
            System.out.println("Good! The File Exists");
        }

        Scanner inputFile = new Scanner(f);

        String Game1 = inputFile.nextLine();
        System.out.println(Game1);

        inputFile.close();


    }
}

创建文件时,始终必须抛出FileNotFoundException,如果不这样做,编译器将不知道如果发生错误该怎么办。指定文件目录时使用/

\通常用作转义序列,当您键入此\\基本上告诉它转义自身时,此代码在其他情况下有用,但在本例中不有用

您不能通过启动对象来创建新文件。您必须始终在对象上调用createNewFile方法,以便创建新文件。这是因为没有构造函数在类中自动调用createNewFile方法。您可能想知道参数中的单词是什么,它们只是用于命名文件目录。如果您想查看创建文件,我发现了一个有用的链接。只需查看“构造函数”选项卡下的内容

一定要!要经常检查代码,不管你是多么优秀的程序员。你总是要检查错误,如果你做了一个游戏,你不知道错误在数百万行代码中的什么地方。你会过得很开心的


最后,我不确定在if语句之后您想做什么,但是在if语句之后您会收到一个错误,因此如果您想问我如何帮助您,请键入我文章中的注释。

在Windows上,/和\在文件名上应该是等效的,所以这没什么好担心的。我猜,许多Windows程序接受/作为\的替代品,因为Linux用户更习惯于/。无论如何,您确定该文件确实存在于C:中的顶级文件夹中吗?试着打开一个命令提示符窗口并键入dir C:\Games.txt。我试过你说的,但什么都没有出现……我假设它认为该文件不存在。但是Games.txt文件在我的C驱动器中。@BrandonNolan则该文件不存在,如果存在,则上面会打印它。感谢您的帮助……该文件实际上名为Games.txt……并且是一个txt文件。所以我必须输入Games.txt.txt,我觉得stupidI删除了这一行,错误仍然会出现。哈阿,我被难住了
File f = new File(filename);
if (f.exists()) {
  System.out.println(f);
  Scanner inputFile = new Scanner(f);
  while (inputFile.hasNextLine()) {
    System.out.println(inputFile.nextLine());
  }
} else {
  System.out.println(f.getPath() + " does not exist");
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Answer {

    public static void main(String args[]) throws IOException, FileNotFoundException {

        // Have to throw a FileNotFoundException just in case an error occurs the compiler needs to know how to process the error.

        PrintWriter pw = new PrintWriter("C:/file/Summary.txt");
        Scanner k = new Scanner(System.in);
        String filename;


        System.out
                .println("--------------------------------\nBowsers Nuclear Weapons Inventory\n"
                        + "---------------------------------");
        System.out.print("Please enter the name of the file: ");
        filename = k.nextLine(); //Input for strings

        System.out.println(filename);
        File f = new File("C:/file/"+filename+".txt"); //Must have a location for your files
        f.createNewFile(); //The file's pathname is the only thing that you can supply when you instantiate the object
        //you actually have to invoke the createNewFile method upon the object.
        if(f.exists()) { //Don't be afraid to check your code this is a must for every programmer.
            System.out.println("Good! The File Exists");
        }

        Scanner inputFile = new Scanner(f);

        String Game1 = inputFile.nextLine();
        System.out.println(Game1);

        inputFile.close();


    }