| FileNotFound [message #1010174] |
Sat, 16 February 2013 14:34  |
Sally Budack Messages: 22 Registered: February 2013 |
Junior Member |
|
|
I am trying to access a file in the source folder. When I use another editor with the same code, it works. I cannot find a reason it does not in Eclipse. All of my other code works. I have tried other files without success. I unistalled & reinstalled Eclipse. It still does not work.
Any Suggestions?
Exception in thread "main" java.io.FileNotFoundException: mobydick.txt (The syst
em cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at HWo5Main.main(HWo5Main.java:17)
Exception in thread "main" java.io.FileNotFoundException: Numbers.txt (The syste
m cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at WrapperClass.readFile(WrapperClass.java:32)
at CH10Main.main(CH10Main.java:22)
Any help would be appreciated.
|
|
|
| Re: FileNotFound [message #1010248 is a reply to message #1010174] |
Sat, 16 February 2013 20:00   |
Russell Bateman Messages: 2711 Registered: July 2009 Location: Provo, Utah, USA |
Senior Member |

|
|
On 02/16/2013 02:37 PM, Sally Budack wrote:
> I am trying to access a file in the source folder. When I use another
> editor with the same code, it works. I cannot find a reason it does not
> in Eclipse. All of my other code works. I have tried other files
> without success. I unistalled & reinstalled Eclipse. It still does not
> work. Any Suggestions?
>
> Exception in thread "main" java.io.FileNotFoundException: mobydick.txt
> (The syst
> em cannot find the file specified)
> at java.io.FileInputStream.open(Native Method)
> at java.io.FileInputStream.<init>(Unknown Source)
> at java.util.Scanner.<init>(Unknown Source)
> at HWo5Main.main(HWo5Main.java:17)
>
>
> Exception in thread "main" java.io.FileNotFoundException: Numbers.txt
> (The syste
> m cannot find the file specified)
> at java.io.FileInputStream.open(Native Method)
> at java.io.FileInputStream.<init>(Unknown Source)
> at java.util.Scanner.<init>(Unknown Source)
> at WrapperClass.readFile(WrapperClass.java:32)
> at CH10Main.main(CH10Main.java:22)
>
> Any help would be appreciated.
Your question is a bit misleading. In fact, you're not trying to open a
file with an editor, but from code you've written in Java, no? The fact
that you can use an editor to open the file successfully is neither here
nor there (apples and oranges).
This is really more a Java--not Eclipse--question. As such, you should
ask it in a Java forum like javaranch.com, jguru.com or
stackoverflow.com. Or Google for the answer.
What you're missing, however, is an understanding of how the path works
relative to, in this case an Eclipse project, but it's a similar problem
no matter what IDE you're using (or even with no IDE at all).
For many reasons, Java is a lot harder to use for file I/O than what
other languages like C offer. If you were to move mobydick.txt to
C:\eclipse (or wherever you've installed Eclipse) or to the root of your
project, you'd probably find that your code works. Otherwise, you'll
have to do some serious study on how Java I/O works.
Many, many years ago, when I was in Java short pants, I wrote this note
to myself:
http://www.javahotchocolate.com/topics/pfileio.html
Here are some good (probably better) links to read on this topic:
http://stackoverflow.com/questions/437382/how-do-relative-file-paths-work-in-eclipse
http://docs.oracle.com/javase/tutorial/essential/io/path.html
http://www.mkyong.com/java/how-to-construct-a-file-path-in-java/
http://www.avajava.com/tutorials/lessons/whats-the-difference-between-a-files-path-absolute-path-and-canonical-path.html
I hope some of this helps.
Best of luck.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Re: FileNotFound [message #1010502 is a reply to message #1010315] |
Sun, 17 February 2013 08:45  |
David Wegener Messages: 1157 Registered: July 2009 |
Senior Member |
|
|
On 02/16/2013 11:28 PM, Sally Budack wrote:
> Thanks for the input. The error of my ways was importing the text file
> from the file menu rather than from the package explorer. Simple but
> huge difference.
No, that has nothing to do with it. As Russell said, this involves path
resolution at runtime. You need to spend some time understanding things
like current/running directory. Programs run in a specific directory.
That has to be consistent with the relative paths to files that your
program looks for.
|
|
|
Powered by
FUDForum. Page generated in 0.06976 seconds