java 当文件在文件夹中时,Netbeans FileReader FileNotFound 异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7091898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:35:33  来源:igfitidea点击:

Netbeans FileReader FileNotFound Exception when the file is in folder?

javaexceptionnetbeansidefilereader

提问by Ben Mattaini

so the problem is that I am having exception thrown each time I try to load the code below on NetBeans or Eclips, but when I try to run it thru TextMate everything works fine!

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

I tried to put the absolute address, changed the text file etc.. didn't help!

我试图把绝对地址,改变文本文件等。没有帮助!

Can someone help me or tell why it won't run with IDE?

有人可以帮助我或告诉为什么它不能与 IDE 一起运行吗?

Thanks

谢谢

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

Debug log: 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 }

调试日志: 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 }

回答by Vineet Menon

In netbeans the default working directory is always the root folder, i mean the folder which contains the folders which name "src", "build" etc. Place the file along with these folders and it will do the trick.

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

回答by Java Scholar

Here is step by Step procedure in NetBeans IDE 7.0.1

这是 NetBeans IDE 7.0.1 中的分步过程

  1. Click on File menu.
  2. Click on Project Properties.
  3. In the categories, select Run.
  4. In main class you select your current java file.
  5. In Arguments select the file you want to read for e.g. abc.txt or abc.java
  6. And in Working Directory write down the path of folder in which this abc.txt or abc.java lies.
  7. Click OK to close Project Properties.
  8. While running your program don't forget to select your project as Main Project.
  9. Then click F^ on keyboard. i.e. You have to raun your main project instead of just running your current java file. That's it....enjoy!!!!
  1. 单击文件菜单。
  2. 单击项目属性。
  3. 在类别中,选择运行。
  4. 在主类中,您选择当前的 java 文件。
  5. 在参数中选择您要读取的文件,例如 abc.txt 或 abc.java
  6. 并在工作目录中记下此 abc.txt 或 abc.java 所在文件夹的路径。
  7. 单击确定关闭项目属性。
  8. 在运行你的程序时不要忘记选择你的项目作为主项目。
  9. 然后单击键盘上的 F^。即您必须运行您的主项目,而不仅仅是运行您当前的 java 文件。就是这样……享受!!!!

回答by Ben Mattaini

Finally found the solution

终于找到解决办法

In eclipse you should put the target file in project folder. Guess same applies to NetBeans.

在 Eclipse 中,您应该将目标文件放在项目文件夹中。猜测同样适用于 NetBeans。

I had my target file in "src" folder (where the actual code files were). In fact i had to just change it to upper folder where the project folder is.

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

Easy and simple.

简单易行。

回答by YasirAzgar

right click on your text file select properties and copy the path and paste it in the place where you have entered your file name

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

回答by osamakhair

lets say you wanna add test.txt in netbeans

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

if your project in C:\myProject put the text file inside C:\myProject file directly notin the C:\myProject\src . then use:

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

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

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

Scanner in = new Scanner(file);

扫描仪输入 = 新扫描仪(文件);

OR

或者

Scanner input = new Scanner(new File("test.txt"));

Scanner input = new Scanner(new File("test.txt"));

回答by dacwe

Probably you have different "working directories"in you different setups. You can check which directory you are in by printing it like this:

您可能在不同的设置中有不同的“工作目录”。您可以通过如下打印来检查您所在的目录:

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

In eclipse you can set set up the working directory in the run configurations, arguments tab.

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

回答by sealz

Try BufferedReader?

试试 BufferedReader?

EDIT: Edited to show example more closely to your code. I got excepption when using File Reader. But was able to .printlnwith BufferedReader. I did not use Scanner.

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

EDIT2: I was also able to get your code to work. With Scanner etc(When using full path) (Example: FileReader read = new FileReader(""C:\\myfolder\\folder\\a.txt". So hmmm.

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);     
        }