Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with relative file path

Tags:

java

eclipse

So here is my program, which works ok:

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.Locale;

public class ScanSum {
    public static void main(String[] args) throws IOException {
        Scanner s = null;
        double sum = 0;
        try {
            s = new Scanner(new BufferedReader(new FileReader("D:/java-projects/HelloWorld/bin/usnumbers.txt")));
            s.useLocale(Locale.US);

            while (s.hasNext()) {
                if (s.hasNextDouble()) {
                    sum += s.nextDouble();
                } else {
                    s.next();
                }
            }
        } finally {
            s.close();
        }

        System.out.println(sum);
    }
}

As you can see, I am using absolute path to the file I am reading from:

s = new Scanner(new BufferedReader(new FileReader("D:/java-projects/HelloWorld/bin/usnumbers.txt")));

The problem arises when I try to use the relative path:

s = new Scanner(new BufferedReader(new FileReader("usnumbers.txt")));

I get an error:

Exception in thread "main" java.lang.NullPointerException
    at ScanSum.main(ScanSum.java:24)

The file usnumbers.txt is in the same directory as the ScanSum.class file:

D:/java-projects/HelloWorld/bin/ScanSum.class
D:/java-projects/HelloWorld/bin/usnumbers.txt

How could I solve this?

like image 919
Richard Knop Avatar asked May 18 '10 19:05

Richard Knop


People also ask

Is it better to use relative or absolute paths?

Relative links show the path to the file or refer to the file itself. A relative URL is useful within a site to transfer a user from point to point within the same domain. Absolute links are good when you want to send the user to a page that is outside of your server.

What is a relative file path?

A relative path refers to a location that is relative to a current directory. Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy.

Why is my file path not working?

The reason you may experience a Path or File not found error is due to the incorrect set up of file-system paths (folders or directories) when dealing with documents or forms. Essentially the software may be trying to find or save a document to a folder and that path does not exist.


2 Answers

If aioobe@'s suggestion doesn't work for you, and you need to find out which directory the app is running from, try logging the following:

new File(".").getAbsolutePath()
like image 111
pdbartlett Avatar answered Oct 04 '22 10:10

pdbartlett


From which directory is the class file executed? (That would be the current working directory and base directory for relative paths.)

If you simply launch the application from eclipse, the project directory will be the working directory, and you should in that case use "bin/usnumbers.txt".

like image 27
aioobe Avatar answered Oct 04 '22 10:10

aioobe