首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java FileReader FileNotFoundException

Java FileReader FileNotFoundException
EN

Stack Overflow用户
提问于 2018-05-07 07:56:10
回答 2查看 3.2K关注 0票数 0

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

代码语言:javascript
复制
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);
        }
    }
EN

回答 2

Stack Overflow用户

发布于 2018-05-07 08:02:19

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

类文件所在的位置与此无关。

票数 0
EN

Stack Overflow用户

发布于 2018-05-07 08:03:12

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

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50205551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档