Raj
Raj

Reputation: 1

Configure path for win and linux

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 separator every time.

(If I missed anything, tell me i will update the same) Thank u.

Upvotes: 0

Views: 538

Answers (4)

Gadolin
Gadolin

Reputation: 2686

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

Upvotes: 2

Ray Tayek
Ray Tayek

Reputation: 10003

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: /

Upvotes: 0

walters
walters

Reputation: 1467

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.

Upvotes: 0

Vinko Vrsalovic
Vinko Vrsalovic

Reputation: 340191

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.

Upvotes: 0

Related Questions