Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path delimiter in windows and linux for java code

In my java code, I have some hard coded paths which I have written as

String workingPath = initPath + "\\" + tmpPath;

the initPath and tmpPath are obtained by File.getParent(). Now, that works on windows and if I move my code to linux, the \\ will be problematic since the other two are determined by system methods. The results is something like this

/home/mahmood/project/alpha\temp1

How can I fix that? I don't want to put / in my code for linux systems.

like image 429
mahmood Avatar asked May 29 '17 20:05

mahmood


People also ask

What is the path delimiter used in the Linux path variable?

The delimiting character is most commonly the slash ("/"), the backslash character ("\"), or colon (":"), though some operating systems may use a different delimiter.

What is path separator in Java?

For file path or directory separator, the Unix system introduced the slash character / as directory separator, and the Microsoft Windows introduced backslash character \ as the directory separator. In a nutshell, this is / on UNIX and \ on Windows.

What is path delimiter?

The path separator is a character commonly used by the operating system to separate individual paths in a list of paths.

What is path in Java file?

A Path can represent a root, a root and a sequence of names, or simply one or more name elements. A Path is considered to be an empty path if it consists solely of one name element that is empty. Accessing a file using an empty path is equivalent to accessing the default directory of the file system.


1 Answers

There is a variable you can use: File.separator

The system-dependent default name-separator character, represented as a string for convenience. This field is initialized to contain the first character of the value of the system property file.separator. On UNIX systems the value of this field is '/'; on Microsoft Windows systems it is '\'.

String workingPath = initPath + File.separator + tmpPath;
like image 100
Samuel Avatar answered Oct 04 '22 03:10

Samuel