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

Fionageo80Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

brunoguimaraesCommented:
Try this:

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

Open in new window

0
fsze88Commented:
System.getProperty("user.dir")
0
fsze88Commented:
new File(".").getAbsolutePath();
0
see4me2002Commented:
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.
0
objectsCommented:
relative files are relative to where the app is being run

for example if it is in the same directory as you run it then you would use

            d = new BufferedReader(new FileReader ("sql.txt"));

or if it is in src/queries you would use

            d = new BufferedReader(new FileReader ("src\\queries\\sql.txt"));

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.