A standalone application for windows and linux environment
While i use the windows environment the file system will be like c:\file\abc\xyz\sample.txt In linux environment the file system will be /home/qa/file/abc/xyz/sample.txt
abc and xyz are the name of the folders that depends on the user click. (abc is the country code folder) & (xyz is language folders)
To construct the path i use properties like
base_path : /home/test/file/ or c:\test\file\ file_name : sample.txt
in the program the construction of the path is :
String path = base_path+country_code+"/"+language_name+"/"+file_name ("/" for Linux)
String path = base_path+country_code+"\"+language_name+"\"+file_name ( "\" for windows)Example for Linux:
/home/test/file/spain/es/sample.txt
Example for Windows:
c:\test\file\Italy\it\sample.txt
country_code and language_name will differ for each user click.
Every time while i test the application with different environment im changing the file separator to "/" or "\" (windows and linux). How do i do the code without changing the file sep开发者_如何学Goarator every time.
(If I missed anything, tell me i will update the same) Thank u.
You may get the separator from java.io.File.separatorChar (public static final char)
or wrapped as String java.io.File.separator (public static final String) - "The system-dependent default name-separator character, represented as a string for convenience".
See: http://docs.oracle.com/javase/6/docs/api/java/io/File.html#separator
Windows accepts / as a valid file separator, so there's no need to change it back and forth, just use the forward slash everywhere.
Note that Windows accepts even mixed separator strings as valid paths, e.g., "C:\test\file\Italy/it/sample.txt" will work.
Java has System properties that you can query for a file separator. If you use System.getProperty("file.separator")
, it will return the "\" or "/" depending on the platform you are running in. Or use the File.separator
, which is also system-dependent default file separator.
try the stuff in System.getProperies like so:
public class So3803335 {
public static void main(String[] arguments) {
System.out.println("java home dir: "+System.getProperty("java.home"));
System.out.println("user dir: "+System.getProperty("user.dir"));
System.out.println("user home: "+System.getProperty("user.home"));
System.out.println("file separator: "+System.getProperty("file.separator"));
}
}
on my windoze box, it says:
java home dir: C:\Program Files\Java\jdk1.6.0_20\jre
user dir: D:\home\ray\dev\stsworkspace\rtec
user home: C:\Documents and Settings\ray
file separator: \
while on my linux box it says:
java home dir: /usr/lib/jvm/java-6-sun-1.6.0.17/jre
user dir: /home/ray
user home: /home/ray
file separator: /
精彩评论