Java, reading a file from current directory?

Java

Java Problem Overview


I want a java program that reads a user specified filename from the current directory (the same directory where the .class file is run).

In other words, if the user specifies the file name to be "myFile.txt", and that file is already in the current directory:

reader = new BufferedReader(new FileReader("myFile.txt"));

does not work. Why?

I'm running it in windows.

Java Solutions


Solution 1 - Java

Try

System.getProperty("user.dir")

It returns the current working directory.

Solution 2 - Java

The current directory is not (necessarily) the directory the .class file is in. It's working directory of the process. (ie: the directory you were in when you started the JVM)

You can load files from the same directory* as the .class file with http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)">getResourceAsStream()</a>;. That'll give you an InputStream which you can convert to a Reader with InputStreamReader.


*Note that this "directory" may actually be a jar file, depending on where the class was loaded from.

Solution 3 - Java

None of the above answer works for me. Here is what works for me.

Let's say your class name is Foo.java, to access to the myFile.txt in the same folder as Foo.java, use this code:

URL path = Foo.class.getResource("myFile.txt");
File f = new File(path.getFile());
reader = new BufferedReader(new FileReader(f));

Solution 4 - Java

Files in your project are available to you relative to your src folder. if you know which package or folder myfile.txt will be in, say it is in

----src
--------package1
------------myfile.txt
------------Prog.java

you can specify its path as "src/package1/myfile.txt" from Prog.java

Solution 5 - Java

If you know your file will live where your classes are, that directory will be on your classpath. In that case, you can be sure that this solution will solve your problem:

URL path = ClassLoader.getSystemResource("myFile.txt");
if(path==null) {
     //The file was not found, insert error handling here
}
File f = new File(path.toURI());

reader = new BufferedReader(new FileReader(f));

Solution 6 - Java

Thanks @Laurence Gonsalves your answer helped me a lot. your current directory will working directory of proccess so you have to give full path start from your src directory like mentioned below:

public class Run {
public static void main(String[] args) {
	File inputFile = new File("./src/main/java/input.txt");
	try {
		Scanner reader = new Scanner(inputFile);
		while (reader.hasNextLine()) {
			String data = reader.nextLine();
			System.out.println(data);
			
		}
		reader.close();
	} catch (FileNotFoundException e) {
		System.out.println("scanner error");
		e.printStackTrace();
	}
}

}

While my input.txt file is in same directory.

enter image description here

Solution 7 - Java

Try this:

BufferedReader br = new BufferedReader(new FileReader("java_module_name/src/file_name.txt"));

Solution 8 - Java

try using "." E.g.

File currentDirectory = new File(".");

This worked for me

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSaobiView Question on Stackoverflow
Solution 1 - JavaJoonas PulakkaView Answer on Stackoverflow
Solution 2 - JavaLaurence GonsalvesView Answer on Stackoverflow
Solution 3 - JavaTony VuView Answer on Stackoverflow
Solution 4 - JavakatwekibsView Answer on Stackoverflow
Solution 5 - JavaJoel WestbergView Answer on Stackoverflow
Solution 6 - JavaHina HalaniView Answer on Stackoverflow
Solution 7 - JavaAshwinView Answer on Stackoverflow
Solution 8 - JavaCharlieView Answer on Stackoverflow