开发者

Netbeans FileReader FileNotFound Exception when the file is in folder?

开发者 https://www.devze.com 2023-03-28 21:35 出处:网络
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!

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!

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?

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/Jav开发者_高级运维a/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 }


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.


Here is step by Step procedure in 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!!!!


Finally found the solution

In eclipse you should put the target file in project folder. Guess same applies to 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.

Easy and simple.


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.


Try BufferedReader?

EDIT: Edited to show example more closely to your code. I got excepption when using File Reader. But was able to .println with BufferedReader. I did not use 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.

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


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


lets say you wanna add test.txt in netbeans

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

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

Scanner in = new Scanner(file);

OR

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

0

精彩评论

暂无评论...
验证码 换一张
取 消