Link to home
Start Free TrialLog in
Avatar of Fionageo80
Fionageo80

asked on

read file with relative path of file inside of a project

Hi I have to read a file that is in my project, I have to read it line by line.

How can I get the relative path?

Thanks
BufferedReader d = null;
        try {
            d = new BufferedReader(new FileReader ("D:\\Users\\project\\test\\src\\queries\\sql.txt"));
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        String line;

Open in new window

Avatar of brunoguimaraes
brunoguimaraes
Flag of Brazil image

Try this:

BufferedReader d = null;
InputStream is = getClass().getResourceAsStream("relative path to file");
d = new BufferedReader(new InputStreamReader(is);

Open in new window

Avatar of Kin Fat SZE
System.getProperty("user.dir")
new File(".").getAbsolutePath();
If its within the project

Put your file in the classpath of your project and use the getClass().getResourceAsStream.

If file is outside your project( ie., written by someone else and your reading it)

Have a properties file in your class path which can have the path of the file where your file would be. read the properties file, get the file path from it and read the file.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial