Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.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
我的类应用程序中的FileNotFoundException java.io.FileNotFoundException:Monsters.txt(系统找不到指定的文件) 在java.io.FileInputStream.open(本机方法) 位于java.io.FileInputStream。(FileInputStream.java:138) 位于java.io.FileInputStream。(FileInputStream.java:93) 位于java.io.FileReader。(FileReader.java:58) 位于MonsterApp.main(MonsterApp.java:28)_Java_Exception - Fatal编程技术网

我的类应用程序中的FileNotFoundException java.io.FileNotFoundException:Monsters.txt(系统找不到指定的文件) 在java.io.FileInputStream.open(本机方法) 位于java.io.FileInputStream。(FileInputStream.java:138) 位于java.io.FileInputStream。(FileInputStream.java:93) 位于java.io.FileReader。(FileReader.java:58) 位于MonsterApp.main(MonsterApp.java:28)

我的类应用程序中的FileNotFoundException java.io.FileNotFoundException:Monsters.txt(系统找不到指定的文件) 在java.io.FileInputStream.open(本机方法) 位于java.io.FileInputStream。(FileInputStream.java:138) 位于java.io.FileInputStream。(FileInputStream.java:93) 位于java.io.FileReader。(FileReader.java:58) 位于MonsterApp.main(MonsterApp.java:28),java,exception,Java,Exception,这是我在运行应用程序时收到的错误。。我似乎无法修复代码。从这个角度来看,最有可能发生的错误是什么 这是我正在运行的应用程序中的代码 __ 公共类应用程序{ java.io.FileNotFoundException: Monsters.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.&

这是我在运行应用程序时收到的错误。。我似乎无法修复代码。从这个角度来看,最有可能发生的错误是什么


这是我正在运行的应用程序中的代码

__ 公共类应用程序{

java.io.FileNotFoundException: Monsters.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.FileInputStream.<init>(FileInputStream.java:93)
    at java.io.FileReader.<init>(FileReader.java:58)

at MonsterApp.main(MonsterApp.java:28)
//读取的所有怪物的列表。
私有静态ArrayList monsters=新ArrayList();
/**
*主要方法
*@param args
*/
公共静态void main(字符串[]args){
//Try for IOExceptions--在找不到文件时调用
试一试{
/*要求:文件输入*/
//连接到monsters.txt文件
BufferedReader br=新的BufferedReader(新文件阅读器(“Monsters.txt”);
字符串行=”;
//读每一行
而((line=br.readLine())!=null){
//分道扬镳
String[]parts=line.split(“,”);
//尝试从文件详细信息创建怪物
试一试{
//它是哪种怪物
开关(部件[0]){
案例“奇美拉”:
/*要求:多态性*/
//将奇美拉添加到怪物列表中
monsters.add(新的Chimera(Monster.SIZE.indexOf(parts[1])、Integer.parseInt(parts[2])、Integer.parseInt(parts[3]),
部分[4],整数.parseInt(部分[5]);
打破
案例“Hydra”:
/*要求:多态性*/
//将九头蛇添加到怪物列表中
添加(新Hydra(Monster.SIZE.indexOf(parts[1])、Integer.parseInt(parts[2])、Integer.parseInt(parts[3]),
部分[4],Integer.parseInt(部分[5]),新玩具(部分[6],部分[7]);
打破
}
}捕获(怪物例外){
//怪物有问题,请输出消息
System.out.println(例如getMessage());
}
}
//环游所有的怪物
对于(怪物mon:怪物){
//输出每个怪物的toString结果
System.out.println(mon.toString());
//调用scare方法
mon.scare();
//调用sleep方法
mon.sleep();
}
//关闭文件
br.close();
//打开runcount文件
BufferedReader br2=新的BufferedReader(新文件读取器(“runcount.txt”);
//读取运行次数
int count=Integer.parseInt(br2.readLine());
//关闭文件
br2.close();
//获取文件
File File=新文件(“runcount.txt”);
//开放写作
PrintWriter pw=新的PrintWriter(文件);
//增加计数
计数++;
/*要求:文件输出*/
//输出到文件
pw.println(计数);
//关闭文件
关闭();
//输出运行计数信息
System.out.println(“\n此程序已运行”+count+“次”);
}捕获(IOE异常){
//发生文件读取/写入错误。
e、 printStackTrace();
}   
}

}

将代码
新文件读取器(名称)
更改为
新文件读取器(新文件(名称).getAbsoluteFile())
,您的异常将告诉您在何处尝试查找文件。它很可能不是您认为的地方。

您能分享代码吗?FileNotFoundException通常发生在您指向不存在的文件或无法访问该文件时。这似乎是因为您提供的文件名没有路径。所以我建议提供正确的路径,比如c:/mydocs/Monster.txt。顺便说一句,没有任何源代码就很难帮助你,试着提供你的源代码来帮助你。这实际上意味着它找不到你的文件。最可能的问题是你在文件路径上犯了错误。尝试传递绝对文件路径。我想我已经找到了异常问题的答案。但是现在我在运行应用程序时遇到了一个问题,当它的计数为++时,增量为;MonsterApp.java:91:错误:';'期望计数++;^1错误这是im接收的错误听起来像是编译错误,而不是运行时错误。您上面的代码看起来是正确的,是您试图编译的代码吗?哪一个是第91行。
//A list of all monsters read in.
private static ArrayList<Monster> monsters = new ArrayList<Monster>();

/**
 * The main method
 * @param args
 */
public static void main(String[] args) {
    //Try for IOExceptions -- called when file cannot be found
    try {
        /* REQUIREMENT : FILE INPUT */
        //Connect to the monsters.txt file
        BufferedReader br = new BufferedReader(new FileReader("Monsters.txt"));

        String line = "";

        //Read in each line
        while((line = br.readLine()) != null) {
            //Split the line
            String[] parts = line.split(",");

            //Try to create monsters from the file details
            try {
                //Swtich which kind of monster it is
                switch(parts[0]) {
                    case "Chimera":
                        /* REQUIREMENT : POLYMORPHISM */
                        //Add chimera to monster list
                        monsters.add(new Chimera(Monster.SIZE.indexOf(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3]), 
                                parts[4], Integer.parseInt(parts[5])));
                        break;
                    case "Hydra":
                        /* REQUIREMENT : POLYMORPHISM */
                        //Add hydra to monster list
                        monsters.add(new Hydra(Monster.SIZE.indexOf(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3]), 
                                parts[4], Integer.parseInt(parts[5]), new Toy(parts[6], parts[7])));
                        break;
                }
            } catch (MonsterException ex) {
                //Something is wrong with the monster, output the message
                System.out.println(ex.getMessage());
            }
        }

        //Loop through all the monsters
        for(Monster mon : monsters) {
            //Output the toString result of each monster
            System.out.println(mon.toString());

            //Call the scare method
            mon.scare();

            //Call the sleep method
            mon.sleep();
        }

        //Close the file
        br.close();

        //Open the runcount file
        BufferedReader br2 = new BufferedReader(new FileReader("runcount.txt"));

        //Read the number of runs
        int count = Integer.parseInt(br2.readLine());

        //Close the file
        br2.close();

        //Get the file
        File file = new File("runcount.txt");

        //Open for writing
        PrintWriter pw = new PrintWriter(file);

        //Increment the count
        count++;

        /* REQUIREMENT : FILE OUTPUT */
        //Output to the file
        pw.println(count);

        //Close the file
        pw.close();

        //Output the run count information
        System.out.println("\nThis program has been run " + count + " times");
    } catch (IOException e) {
        //A file reading/writing error has occurred.
        e.printStackTrace();
    }   
}