How to know if a file path is relative or absolute

N

Nomak

Hello,

i want to test if a file path is relative. Here is code at the moment:

public static boolean isPathRelative(File file) {
String path = file.toString();

// if linux (root)
if (path.startsWith("/") || !path.startsWith("\\"))
return false;

// if windows (UNC, or drive letter)
if (path.startsWith("\\\\") || path.indexOf(":\\") > 0)
return false;

return true;
}


but i don't know how to test the OS i'm running on. Can somebody help me?

TIA
 
R

Roedy Green

but i don't know how to test the OS i'm running on. Can somebody help me?

another way to do it is to get the absolute path and see how different
it is from the original.

In windows you can have both / and \

You could find out what os by checking the system properties. See
http://mindprod.com/applets/wassup.html

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
A

Andrea Desole

Roedy said:
another way to do it is to get the absolute path and see how different
it is from the original.

how about "new File( path ).isAbsolute()" ?
 
P

Patricia Shanahan

Nomak said:
Hello,

i want to test if a file path is relative. Here is code at the moment:

public static boolean isPathRelative(File file) {
String path = file.toString();

// if linux (root)
if (path.startsWith("/") || !path.startsWith("\\"))
return false;

// if windows (UNC, or drive letter)
if (path.startsWith("\\\\") || path.indexOf(":\\") > 0)
return false;

return true;
}


but i don't know how to test the OS i'm running on. Can somebody help me?

TIA

The Java API let's you do a lot of stuff without worrying about which OS:

public static boolean isPathRelative(File file) {
return !file.isAbsolute();
}

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top