Java FileReader FileNotFoundException

时间:2018-05-06 23:56:10

标签: java file-io

我只是想读取一个文件,我的类文件存在于与我尝试阅读的文件完全相同的目录中。我尝试阅读的文件称为profiles.txt。我之前做过完全相同的方法在极其相似的情况下它起作用(并且仍然有效),我不知道为什么这不起作用。如果有人能解释我会非常感激。

public static void readProfiles(BST tree) {
        try {
            BufferedReader getData = new BufferedReader(
                    new FileReader(
                            new File("profiles.txt")));

            String data = getData.readLine();

            while(data != null) {
                String[] profileData = data.split(",");
                String[] interests = profileData[7].split(";");
                tree.insertProfile(new Profile(
                        profileData[0],
                        new int[] {Integer.parseInt(profileData[1]), Integer.parseInt(profileData[2]), Integer.parseInt(profileData[3])},
                        profileData[4],
                        profileData[5],
                        profileData[6],
                        interests
                ));
                data = getData.readLine();
            }
            getData.close();
        }
        catch(FileNotFoundException e) {
            System.out.println("File not found");
            System.exit(0);
        }
        catch(IOException e) {
            System.out.println("IO error occured");
            System.exit(0);
        }
    }

2 个答案:

答案 0 :(得分:0)

文件名是相对的,不包含任何目录,因此需要在当前的工作目录中。

类文件没有任何关系。

答案 1 :(得分:-2)

尝试相对于程序主运行文件的路径。