0

I have reviewed other answers to questions about this problem and tried what they suggested, but with no success. In my program, I have these five lines all in succession:

    String curDir = System.getProperty("user.dir");
    System.out.println(curDir);

    File f = new File("/home/brian/workspace/Color Sampler/src/Data.txt");
    if(f.exists() && !f.isDirectory()) { System.out.println("success"); }

    Scanner input = new Scanner(new File("/home/brian/workspace/Color Sampler/src/Data.txt"));

The first four lines result in the output:

/home/brian/workspace/Color Sampler
success

The file, to my knowledge, is located in MULTIPLE locations:

/home/brian/workspace/Color Sampler
/home/brian/workspace/Color Sampler/src
/home/brian/workspace/Color Sampler/bin

Despite all this, I'm still getting the exception. I've tried it with all three of the above filepaths. The file also is not open anywhere so it should be readable. I'm ripping my hair out at this point as I've spent over an hour on a simple filename specification.

Here's the exception:

Exception in thread "AWT-EventQueue-1" java.lang.Error: Unresolved compilation problem: 
Unhandled exception type FileNotFoundException

at WindowDestroyer.windowOpened(WindowDestroyer.java:46)
at java.awt.Window.processWindowEvent(Window.java:1972)
at javax.swing.JFrame.processWindowEvent(JFrame.java:290)
at java.awt.Window.processEvent(Window.java:1933)
at java.awt.Component.dispatchEventImpl(Component.java:4649)
at java.awt.Container.dispatchEventImpl(Container.java:2103)
at java.awt.Window.dispatchEventImpl(Window.java:2588)
at java.awt.Component.dispatchEvent(Component.java:4475)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:675)
at java.awt.EventQueue.access$300(EventQueue.java:96)
at java.awt.EventQueue$2.run(EventQueue.java:634)
at java.awt.EventQueue$2.run(EventQueue.java:632)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:108)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:119)
at java.awt.EventQueue$3.run(EventQueue.java:648)
at java.awt.EventQueue$3.run(EventQueue.java:646)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:108)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:645)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
Bobazonski
  • 1,515
  • 5
  • 26
  • 43
  • What do you want to do with the file? – user432 Dec 03 '14 at 15:29
  • does Data.txt really have a captital D? Also, what happens when you just create a new File, without feeding it into the scanner first? Can you get some file information, or is that already failing? It would also be good if you could show the exception you're getting. – SirRichie Dec 03 '14 at 15:32
  • It does have a capital D. And I revised my question to include the eception. And what do you mean by just create a new File? I'm new to Java and very unfamiliar with its file-io. – Bobazonski Dec 03 '14 at 15:38

3 Answers3

3

Your problem is not, that the file cannot be found, your problem is, that you're not handling a possible FileNotFoundException.

Either change your code to:

try (Scanner input = new Scanner(new File("/home/brian/workspace/Color Sampler/src/Data.txt"))) {
    // read file and all
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

or add a throws clause to your method:

public void method() throws FileNotFoundException {

}
Tom
  • 16,842
  • 17
  • 45
  • 54
0

I believe that the key to your problem is the "unresolved compilation error". Is your project generated in Eclipse? If so, According to this discussion, Eclipse can generate code that should no compile hence gives an error at run time.

All open operations may generate exceptions. Check the documentation and correct your code, surrounding it with an apropriate try-catch clause.

Try this:

try {
 Scanner input = new Scanner(new File("/home/brian/workspace/Color Sampler/src/Data.txt"));
} catch (Exception ex) {
}

If it works, then this is direction.

Addendum: further information on this issue can be found at this discussion.

Community
  • 1
  • 1
rlinden
  • 2,053
  • 1
  • 12
  • 13
-1

You probably have insufficient permission to access the file.

Try to call exists() on File object first, it should return true.

Milan Baran
  • 4,133
  • 2
  • 32
  • 49
  • I tried that, returned true. I revised my question to include this. Perhaps my computer is trying to prank me? – Bobazonski Dec 03 '14 at 15:45