[java] 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);

This question is related to java eclipse file-io

The answer is


you just need to get the absolute-path of the file, since the file you are looking for doesnt exist in the eclipse's runtime workspace you can use - getProperty() or getLocationURI() methods to get the absolute-path of the file


Have you tried using an absolute path:

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

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.


Sometimes, even when the file is in the right directory, there is still the "file not found" exception. One thing you could do is to drop the text file inside eclipse, where your classes are, on the left side. It is going to ask you if you want to copy, click yes. Sometimes it helps.


There's nothing wrong with your code, the following works fine for me when I have the file.txt in the user.dir directory.

import java.io.File;
import java.util.Scanner;

public class testme {
    public static void main(String[] args) {
        System.out.println(System.getProperty("user.dir"));
        File file = new File("file.txt");
        try {
            Scanner scanner = new Scanner(file);
        } catch (Exception e) {
        System.out.println(e);
        }
    }
}

Don't trust Eclipse with where it says the file is. Go out to the actual filesystem with Windows Explorer or equivalent and check.

Based on your edit, I think we need to see your import statements as well.


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


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to eclipse

How do I get the command-line for an Eclipse run configuration? My eclipse won't open, i download the bundle pack it keeps saying error log strange error in my Animation Drawable How to uninstall Eclipse? How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Class has been compiled by a more recent version of the Java Environment Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 "The POM for ... is missing, no dependency information available" even though it exists in Maven Repository The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat

Examples related to file-io

Python, Pandas : write content of DataFrame into text File Saving response from Requests to file How to while loop until the end of a file in Python without checking for empty line? Getting "java.nio.file.AccessDeniedException" when trying to write to a folder How do I add a resources folder to my Java project in Eclipse Read and write a String from text file Python Pandas: How to read only first n rows of CSV files in? Open files in 'rt' and 'wt' modes How to write to a file without overwriting current contents? Write objects into file with Node.js