【问题标题】:Netbeans FileReader FileNotFound Exception when the file is in folder?Netbeans FileReader FileNotFound 文件在文件夹中时出现异常?
【发布时间】:2011-08-17 11:14:20
【问题描述】:

所以问题是每次我尝试在 NetBeans 或 Eclips 上加载下面的代码时都会抛出异常,但是当我尝试通过 TextMate 运行它时,一切正常!

我尝试输入绝对地址,更改了文本文件等。没有帮助!

谁能帮助我或告诉我为什么它不能在 IDE 中运行?

谢谢

void loadFile() {
    try {
        list = new LinkedList<Patient>();

        FileReader read = new FileReader("a.txt");
        Scanner scan = new Scanner(read);

        while (scan.hasNextLine()) {
            String Line = scan.nextLine();
            String[] subArray = new String[5];
            subArray = Line.split(",");
            int a = Integer.parseInt(subArray[4]);

            list.add(new Patient(Integer.parseInt(subArray[0]), subArray[1], subArray[2], subArray[3], a));
        }
    } catch (FileNotFoundException e) {
        JOptionPane.showMessageDialog(null, "The file does not exist!" + "\nProgram is terminating.", "File Not Found", JOptionPane.INFORMATION_MESSAGE);
        System.exit(0);
    }
    cap = list.size();
    search_names = new int[cap];
    for (int i = 0; i < list.size(); i++) {
        search_names[i] = i;
    }
    setNames(search_names);
}//end loadFile

调试日志: Have no file for /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jsfd.jar Have no file for /System/Library/Frameworks/JavaVM.framework/Frameworks/JavaRuntimeSupport.framework/Resources/Java/JavaRuntimeSupport.jar Have no file for /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/laf.jar Have no file for /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/sunrsasign.jar }

【问题讨论】:

  • 我尝试了相同的代码,但它不起作用。我认为您需要提及完整的文件路径。

标签: java exception netbeans ide filereader


【解决方案1】:

在 netbeans 中,默认工作目录始终是根文件夹,我的意思是包含名称为“src”、“build”等文件夹的文件夹。将文件与这些文件夹一起放置就可以了。

【讨论】:

  • a.txt 文件位于“根”文件夹中,其中包含调用它的 .class 文件!示例:Users/ME/NetBeansProjects/Project1/src所有文件都在这个文件夹中!
  • 试试这个路径..Users/ME/NetBeansProjects/Project1/a.txt
  • 其他选择,您可以定义项目的相对路径:new FileReader("src/package/file.txt");
【解决方案2】:

这是 NetBeans IDE 7.0.1 中的分步过程

  1. 点击文件菜单。
  2. 单击项目属性。
  3. 在类别中,选择运行。
  4. 在主类中选择当前的 java 文件。
  5. 在参数中选择要读取的文件,例如abc.txt 或 abc.java
  6. 并在工作目录中记下此 abc.txt 或 abc.java 所在的文件夹路径。
  7. 单击“确定”关闭项目属性。
  8. 运行程序时不要忘记选择您的项目作为主项目。
  9. 然后单击键盘上的 F^。即您必须运行您的主项目,而不仅仅是运行您当前的 java 文件。 就是这样......享受!!!!

【讨论】:

    【解决方案3】:

    终于找到解决办法了

    在eclipse中你应该把目标文件放在项目文件夹中。猜想同样适用于 NetBeans。

    我的目标文件位于“src”文件夹(实际代码文件所在的位置)。事实上,我只需要将其更改为项目文件夹所在的上层文件夹即可。

    简单易行。

    【讨论】:

    • 在 Eclipse 中,如果要将文件放在其他位置,请使用:new FileReader("src/package/file.txt");
    【解决方案4】:

    可能您在不同的设置中有不同的“工作目录”。你可以这样打印来检查你所在的目录:

    System.out.println(new File(".").getAbsoluteFile());
    

    在 Eclipse 中,您可以在运行配置、参数选项卡中设置工作目录。

    【讨论】:

    • 已更新(针对 Eclipse)(以编程方式您可以执行 System.out.println(new File(".").getAbsoluteFile());
    【解决方案5】:

    试试 BufferedReader?

    编辑:已编辑以更贴近您的代码显示示例。使用文件阅读器时出现异常。但能够使用 BufferedReader .println。我没有使用Scanner

    EDIT2:我还能够让您的代码正常工作。使用扫描仪等(使用完整路径时)(例如:FileReader read = new FileReader(""C:\\myfolder\\folder\\a.txt"。所以嗯。

      try {
      list = new LinkedList<Patient>();
      BufferedReader scan = new BufferedReader(new FileReader("C:\\a.txt"));
      String lines;
                try {
                    // Scanner scan = new Scanner(read);
                    while ((lines = scan.readLine()) != null) {
                     //I just printed lines you will do your stuff here
                        System.out.println(lines);
                        }
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            catch (FileNotFoundException e) {
          JOptionPane.showMessageDialog(null, "The file does not exist!" + "\nProgram is terminating.", "File Not Found", JOptionPane.INFORMATION_MESSAGE);
          System.exit(0);     
            }
    

    【讨论】:

    • 不确定......也许它会这样工作,但我必须使用 LinkedList,因为它是“要求”:) - BufferedReader 不喜欢 LinkedList
    • @Ben Mattaini 我进行了编辑以展示我能够工作的内容。我注释掉了你的一堆代码等。但是一旦我在缓冲阅读器中指定了确切的路径,它就起作用了。
    【解决方案6】:

    右键单击您的文本文件选择属性并复制路径并将其粘贴到您输入文件名的位置

    【讨论】:

      【解决方案7】:

      假设你想在 netbeans 中添加 test.txt

      如果您在 C:\myProject 中的项目将文本文件直接放在 C:\myProject 文件中 not 在 C:\myProject\src 中。然后使用:

      文件file = new File("test.txt");

      Scanner in = new Scanner(file);

      扫描仪输入 = new Scanner(new File("test.txt"));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-02
        • 2020-04-15
        • 1970-01-01
        • 1970-01-01
        • 2018-07-07
        • 1970-01-01
        • 2016-10-02
        相关资源
        最近更新 更多