Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Eclipse, won't read text file.  RSS feed

 
John Reese
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a text file (TwoColumns.txt) that looks like this (the allignment might be messed up, but you get the idea):
A B
1.0 2.0
3.0 4.0
5.0 6.0

The below code works. The main point I'm after is that it properly opens the above text file. However, when I switch editors and use eclipse it does not work. I put the TwoColumns.txt file in with the eclipse classes directory. I get the following error:
opening filejava.io.FileNotFoundException: TwoColumns.txt (The system cannot find the file specified).



Please help. I'm getting desperate to finish projects, study etc. and weekend is slipping away. I can't even start until I figure this out.
TIA
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using an IDE, your best bet is not to copy the files to where you think they should go, but let the IDE know that it needs a resource to run, and let the IDE do the copying. This is because IDE's can do a lot of funky things with where resources may come from, what the 'active' directory for an application actually is, and what the classpath for execution is (versus the classpath for compilation or your system classpath).

So, instead of copying the file to your classes directory:
1. Put the file someplace outside the Eclipse managed directory structure.
2. Right click on the Project you want to be able to access it
3. Choose the Import... command.
4. In the Import Source tree structure, choose General/File System
5. Press Next >
6. Use the Browse... button to navigate to where the file is currently located, and select the directory it is in
7. On the left panel will be the directory structure, on the right will be the list of files you want to import as resources. Put a check-mark file you want access to
8. Leave the Into Folder field as the default, unless you want to make the file available in a sub-directory rather than from the root directory
9. Press Finish and the file will be imported into the appropriate place in the application structure, and more importantly it will be maintained as a resource in the root directory of your Runtime Classpath.

This can probably be shortened by dragging the file directly from the File System onto the Project that needs access to (but not any sub-directory, /src path, /bin path etc... thereof).
 
John Reese
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve Luke,
I did exactly as you wrote, but I'm still getting the same error.

<edit> I'll survive by using another editor. I do like eclipse though.</edit>
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,

Try putting it in the src directory in your project. Then, select the project folder and hit the F5 key or right-click the project folder and select refresh.

John.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just don't put the file into any Eclipse directory. Put it into some other directory and provide a full path to the file. Using a relative path is a dubious strategy -- as you have already found out.
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With Eclipse default settings and your current code, your files should be outside the src folder right under the project folder (same level with src folder).
 
Ron Savoy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am "taking" the CS106a course posted online by Stanford, doing the assignment to create a Hangman game, and am having the same problem. The text file shows up in my project but I get a FileNotFound exception.

I tried moving the file to various other locations, importing it as suggested here, putting in the entire file path, nothing seems to work. Also, I don't have src or bin directories in my project or eclipse files.

Google searches have proved fruitless - it feels as though I'm missing something obvious, but can't figure it out. Any thoughts, guidance?? Thanks!

Ron

 
Gene Hilpert
Ranch Hand
Posts: 51
Eclipse IDE Hibernate Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When in doute - do a test.
try writing a little file out using the same parameters and then see where it put it.
 
Ron Savoy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great idea - I wrote the following quicky program:



In Eclipse "new FileWriter("whereami.txt"));" is underlined in red and when hovered over it says "Unhandled exception type IOException".

Is something missing in my setup, an import or something?
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron Savoy wrote:Great idea - I wrote the following quicky program:



In Eclipse "new FileWriter("whereami.txt"));" is underlined in red and when hovered over it says "Unhandled exception type IOException".

Is something missing in my setup, an import or something?


The constructor of PrintWriter(FileWriter fileWriter) throws FileNotFoundException which is a subclass of IOException and so its a checked exception. so you have to put that code inside a try block followed by either catch block or finally or both. If you don't want to catch exceptions, then simply add throws IOException clause to your run() method



or
 
Ron Savoy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for that - that works! I must have missed the part of the lecture where he says you NEED to handle those exceptions, I thought it was good style but assumed it would run and simply give an error if the file didn't exist. I knew it had to be something basic I was missing, thanks for helping.

Ron
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. Please don't hesitate to post any java related question on this forum.we offer solutions for virtually everything
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!