Eclipse Oxygen 3 and Java 10 – how find a path to the file in the project?

If you write an application to read a file existing in the current project,
you have to put file name. However it is not sufficient. First you have to get path to the file.
How do it?
We begin with create very simple project to read file. My project I named net.dora.test
In this project in src folder I create text file, it is named test.txt.
Look at them:

And it is content this file:

It is a message from my first text file.

As you notice in this file is only one line.
In the next step I create main class to read this file.

And look at the code:

package net.dora.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class Test {

	public static void main(String[] args) 
        {
	try {
			 
		String fileSeparator= System.getProperty("file.separator");
		String filePath = fileSeparator+"src"+fileSeparator+"test.txt" ;
		File myFile = new File(  System.getProperty("user.dir") + filePath  );
		
		BufferedReader br = new BufferedReader(new FileReader(myFile));
		String st;
		  while ((st = br.readLine()) != null)
		    System.out.println(st);
		  
	    } catch(Exception e) { e.printStackTrace(); }
	}

If you would like find path or separate path in your OS, the System object in Java help in it. It has getProperty method has String parameter. System class maintains a Properties object that keeps the configuration of the current working environment. The list this possible parameters you can see on the  https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html website.

First you have to get path to user files. In Eclipse it is a workspace folder with your project:

System.getProperty("user.dir")

It returns in my eclipse IDE:

C:\Users\Dora\eclipse-workspace\net.dora.test

You see the path to the project with my application.
Next step is get separator using in OS in the file path. You get it by file.separator property:

System.getProperty("file.separator");

It returns in mys OS:

\

Now we have to join these parts of the path: first get user dir, next add separator and src folder contains text file and name of your text file.

String filePath = fileSeparator+"src"+fileSeparator+"test.txt" ;
File myFile = new File( System.getProperty("user.dir") + filePath );

Of course, it will be work fine, if you write your total path to the file, but it is not  good solution for working application in all OS, because they have different separators of the path. However look at this code on Windows:

File myFile = new File( "C:\\Users\\Dora\\eclipse-workspace\\net.dora.test\\src\\test.txt" );

The next line of the code read content of our file to test if it work:

BufferedReader br = new BufferedReader(new FileReader(myFile)); 
String st; while ((st = br.readLine()) != null) System.out.println(st);

Simple way to display all properties from your System object is use this code:

 System.getProperties().list(System.out);

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.