trying to find if a file exists in a given path

Posted by Yeshwanth Venkatesh on Stack Overflow See other posts from Stack Overflow or by Yeshwanth Venkatesh
Published on 2012-10-08T21:32:16Z Indexed on 2012/10/08 21:37 UTC
Read the original article Hit count: 257

Filed under:
|

I am fairly new to java and I am trying to find if the file specified in the LINUX path exists.

private void validateFilePath(String filePath) {

        File dir = new File(filePath);

        if(dir.exists()){
                System.out.println("File exists in the path " + dir);
                setTARGET_IMG_DIR("filePath");
                return; 
        }else{
            System.out.println("File does not exists in the path: " + dir);
            return;
        }

    }

The dir.exists works fine if I give a absolute path from my root like this

/Users/yv/Documents/Eclipse-workspace/InputParser/bin/test.txt

but if I give a relative path like test.txt or /InputParser/bin/test.txt it says files does not exists.

I am planing on creating a jar of this projects and hence this should work with both relative path(files in the same directory) and absolute path from the root. How can I handle this ?

Is it possible to search for the absolute path of that file from the root and append it to the file name ?

© Stack Overflow or respective owner

Related posts about java

Related posts about filepath