Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取FileNotFoundException,即使文件存在且拼写正确_Java_Filenotfoundexception - Fatal编程技术网

Java 获取FileNotFoundException,即使文件存在且拼写正确

Java 获取FileNotFoundException,即使文件存在且拼写正确,java,filenotfoundexception,Java,Filenotfoundexception,我正在创建一个小程序,它将读取一个文本文件,其中包含大量随机生成的数字,并生成统计数据,如平均值、中值和模式。我已经创建了文本文件,并确保在声明为新文件时名称完全相同 是,该文件与类文件位于同一文件夹中 public class GradeStats { public static void main(String[] args){ ListCreator lc = new ListCreator(); //create ListCreator object lc.getGrad

我正在创建一个小程序,它将读取一个文本文件,其中包含大量随机生成的数字,并生成统计数据,如平均值、中值和模式。我已经创建了文本文件,并确保在声明为新文件时名称完全相同

是,该文件与类文件位于同一文件夹中

public class GradeStats {
public static void main(String[] args){
    ListCreator lc = new ListCreator(); //create ListCreator object
    lc.getGrades(); //start the grade listing process
    try{
        File gradeList = new File("C:/Users/Casi/IdeaProjects/GradeStats/GradeList");
        FileReader fr = new FileReader(gradeList); 

        BufferedReader bf = new BufferedReader(fr);       

        String line;

        while ((line = bf.readLine()) != null){
            System.out.println(line);
        }
        bf.close();
    }catch(Exception ex){
        ex.printStackTrace();


    }
}
}

错误行内容如下:

java.io.FileNotFoundException: GradeList.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileReader.<init>(FileReader.java:72)
    at ListCreator.getGrades(ListCreator.java:17)
    at GradeStats.main(GradeStats.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
java.io.FileNotFoundException:GradeList.txt(系统找不到指定的文件)
在java.io.FileInputStream.open(本机方法)
位于java.io.FileInputStream。(FileInputStream.java:138)
位于java.io.FileReader。(FileReader.java:72)
在ListCreator.getGrades(ListCreator.java:17)
位于GradeStats.main(GradeStats.java:11)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:601)
位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
如何添加:

String curDir = System.getProperty("user.dir");
把这个打印出来。它将告诉您当前工作目录是什么。然后您应该能够看到它找不到文件的原因

如果找不到文件,您可以检查以允许自己执行某些操作,而不是允许代码抛出:

File GradeList = new File("GradeList.txt");
if(!GradeList.exists()) {
    System.out.println("Failed to find file");
   //do something
}
请运行以下命令并粘贴输出:

String curDir = System.getProperty("user.dir");
File GradeList = new File("GradeList.txt");
System.out.println("Current sys dir: " + curDir);
System.out.println("Current abs dir: " + GradeList.getAbsolutePath());

问题是您只指定了相对文件路径,不知道java应用程序的“当前目录”是什么

添加此代码,一切都将变得清晰:

File gradeList = new File("GradeList.txt");
if (!gradeList.exists()) {
    throw new FileNotFoundException("Failed to find file: " + 
        gradeList.getAbsolutePath());
}
通过检查绝对路径,您将发现文件不是当前目录

另一种方法是在创建文件对象时指定绝对文件路径:

File gradeList = new File("/somedir/somesubdir/GradeList.txt");

顺便说一句,试着坚持命名约定:用小写字母开头命名变量,即
gradeList
而不是
gradeList

请包含IDE和目录结构的信息好吗?该文件可能与类文件位于同一目录中,但通常不是当前目录。尝试执行system.out.println(GradeList.getAbsolutePath()),然后使用文件的完整路径名。很明显,您找错了文件的位置。文件的“.txt”扩展名怎么了?双重检查、三重检查、四重检查,确保所有内容的拼写和大写与磁盘上的完全相同。您的代码显示为“GradeList”,异常消息显示为“GradeList.txt”。因此,您没有运行该代码。我尝试了该代码,但它无法编译,仍然提供FileNotFoundException。您打印了curDir吗?它说了什么?成绩表在哪里?我在答案的底部添加了四行代码。请在此处运行并粘贴输出打印出的curDir结果为C:\Users\Casi\IdeaProjects\GradeStats。GradeStats文件位于\GradeStats\src,但当使用system.out.println(GradeStats.getAbsolutePath())时,它会打印与curDir.Right相同的路径,因此如果您放置:file GradeList=新文件(“C:\Users\Casi\IdeaProjects\GradeStats\GradeList.txt”);它应该找到它。这就是windows资源管理器显示它的位置吗?