Path of directory on solaris
Hi
How can i format the file path string which would be machine independent? The code shown below works perfectly well in windows JVM, but iam worried whether the same code will also work on solaris?
String hostIp="";
try
{
hostIp = InetAddress.getLocalHost().getHostAddress();
String path1 = "\\" + "\\" + hostIp;
String path2 = "\\data\\field\\";
return path1 + path2;
}
Please advise.
Thanks in advance.
Gaurav
[492 byte] By [
GauravDeva] at [2007-11-27 10:48:22]

# 1
Use file.separator instead of "\\".
Better yet, if this path will always be used in a File or FileInputStream or something equivalent constructor and not in a Runtime.exec, then simply use "/".
Edit: Then again, I see you are also referencing a remote machine here using the \\<ip>\<path> structure. Well, that structure, in and of itself, does not work on unix. On unix you mount shared file systems, then use them like any other, or, you go over /net/<ip>/<path>.
# 2
u mean to say
path1 = file.separator + hostIp;
But i was thinking the path would be like this '\\10.3.16.128\data\field\' and hence file.separator will not work for this type of string (the initial two slashes). Kndly advise whether this will work on solaris as well.
# 3
See the edited text. The first part of the path will have be completely redone, probably using a check of the System property os.name.
If you need two "\" (as is the case for windows remote reference) then use file.separator twice.
In any case, using this remote syntax already cuts out all systems other than Windows and unix.
# 4
Sorry, but its unclear.. What format shud i use so that it works for Win as well as Solaris.
1path1 = file.separator + file.separator + (ipAddress + path)
2path1 = "\\" + "\\" + rest same as above
3path1 = "/" + rest same as above
Which one from the above or specify if none of the above.
Plz clarify.
# 5
As I have said twice now, the part of the path that indicates a remote file (i.e. \\<ip> on windows, /net/<ip> on unix, whatever else on whatever other OS) are simply different. There is no one path that will work for both. Period. You need to check the target OS.
try constructing a "file://<ip>/<path>" url (with all "/" characters, no "\" characters) and using it's toUri() method.
See http://en.wikipedia.org/wiki/File:_URL and the API for more info.