Reputation: 35772
I have a directory pointed to by a File object called baseDir in which I keep most of my files.
I have a String f which may contain a relative filename, like "data.gz", or an absolute filename like "/mnt/data.gz" (its specified in a config file).
In the event that f is relative, it should be assumed that the file specified is within baseDir, but if it is absolute then it should be treated accordingly.
How do I create a File object that points to the file represented by f?
I can think of hacky solutions to this (eg. if (f.startsWith("/")) ...
but I'm hoping there is a "right way" to do this.
Oh, I tried using the File(File, String) constructor for the File object, but it doesn't do what I need it to do.
Upvotes: 0
Views: 96
Reputation: 18601
You can try the following:
File file = new File(f);
if(file.isAbsolute())
...
Be careful that the definition of absolute path is system dependent!
Upvotes: 0
Reputation: 2382
Use File.isAbsolute(x)
http://download.oracle.com/javase/6/docs/api/java/io/File.html#isAbsolute%28%29
If this returns false, prepend your basedir.
Upvotes: 1