Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read from file in eclipse

I'm trying to read from a text file to input data to my java program. However, eclipse continuosly gives me a Source not found error no matter where I put the file.

I've made an additional sources folder in the project directory, the file in question is in both it and the bin file for the project and it still can't find it.

I even put a copy of it on my desktop and tried pointing eclipse there when it asked me to browse for the source lookup path.

No matter what I do it can't find the file.

here's my code in case it's pertinent:

System.out.println(System.getProperty("user.dir"));
    File file = new File("file.txt");


    Scanner scanner = new Scanner(file);

in addition, it says the user directory is the project directory and there is a copy there too.

I have no clue what to do.

Thanks, Alex

after attempting the suggestion below and refreshing again, I was greeted by a host of errors.

FileNotFoundException(Throwable).<init>(String) line: 195   
FileNotFoundException(Exception).<init>(String) line: not available 
FileNotFoundException(IOException).<init>(String) line: not available   
FileNotFoundException.<init>(String) line: not available    
URLClassPath$JarLoader.getJarFile(URL) line: not available  
URLClassPath$JarLoader.access$600(URLClassPath$JarLoader, URL) line: not available  
URLClassPath$JarLoader$1.run() line: not available  
AccessController.doPrivileged(PrivilegedExceptionAction<T>) line: not available [native method] 
URLClassPath$JarLoader.ensureOpen() line: not available 
URLClassPath$JarLoader.<init>(URL, URLStreamHandler, HashMap) line: not available   
URLClassPath$3.run() line: not available    
AccessController.doPrivileged(PrivilegedExceptionAction<T>) line: not available [native method] 
URLClassPath.getLoader(URL) line: not available 
URLClassPath.getLoader(int) line: not available 
URLClassPath.access$000(URLClassPath, int) line: not available  
URLClassPath$2.next() line: not available   
URLClassPath$2.hasMoreElements() line: not available    
ClassLoader$2.hasMoreElements() line: not available 
CompoundEnumeration<E>.next() line: not available   
CompoundEnumeration<E>.hasMoreElements() line: not available    
ServiceLoader$LazyIterator.hasNext() line: not available    
ServiceLoader$1.hasNext() line: not available   
LocaleServiceProviderPool$1.run() line: not available   
AccessController.doPrivileged(PrivilegedExceptionAction<T>) line: not available [native method] 
LocaleServiceProviderPool.<init>(Class<LocaleServiceProvider>) line: not available  
LocaleServiceProviderPool.getPool(Class<LocaleServiceProvider>) line: not available 
NumberFormat.getInstance(Locale, int) line: not available   
NumberFormat.getNumberInstance(Locale) line: not available  
Scanner.useLocale(Locale) line: not available   
Scanner.<init>(Readable, Pattern) line: not available   
Scanner.<init>(ReadableByteChannel) line: not available 
Scanner.<init>(File) line: not available    

code used:

System.out.println(System.getProperty("user.dir"));
    File file = new File(System.getProperty("user.dir") + "/file.txt");


    Scanner scanner = new Scanner(file);
like image 698
Buzkie Avatar asked Mar 25 '09 10:03

Buzkie


People also ask

Where do I put the read Java file in Eclipse?

Put the file in the folder from where you run your Java application (your current/working folder). If you're using the default settings of Eclipse to run your application, you should put the file directly inside the Eclipse project folder.

How can I read from a file in Java?

There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader, or Scanner to read a text file. Every utility provides something special e.g. BufferedReader provides buffering of data for fast reading, and Scanner provides parsing ability.

How do I open a text file in Eclipse?

Right-click on your project in the Package Explorer and select Import.... This will start up the Import wizard. In the import window's filter bar (the text field that will, by default says "type filter text") type "file" and then select File System.


4 Answers

Did you try refreshing (right click -> refresh) the project folder after copying the file in there? That will SYNC your file system with Eclipse's internal file system.

When you run Eclipse projects, the CWD (current working directory) is project's root directory. Not bin's directory. Not src's directory, but the root dir.

Also, if you're in Linux, remember that its file systems are usually case sensitive.

like image 132
Pablo Santa Cruz Avatar answered Sep 20 '22 02:09

Pablo Santa Cruz


Have you tried using an absolute path:

File file = new File(System.getProperty("user.dir") + "/file.txt");
like image 44
kgiannakakis Avatar answered Sep 24 '22 02:09

kgiannakakis


You are searching/reading the file "fiel.txt" in the execution directory (where the class are stored, i think).

If you whish to read the file in a given directory, you have to says so :

File file = new File(System.getProperty("user.dir")+"/"+"file.txt");

You could also give the directory with a relative path, eg "./images/photo.gif) for a subdirecory for example.

Note that there is also a property for the separator (hard-coded to "/" in my exemple)

regards Guillaume

like image 37
PATRY Guillaume Avatar answered Sep 24 '22 02:09

PATRY Guillaume


I am using eclipse and I was stuck on not being able to read files because of a "file not found exception". What I did to solve this problem was I moved the file to the root of my project. Hope this helps.

like image 45
Electron Avatar answered Sep 22 '22 02:09

Electron