In the File
class there are two strings, separator
and pathSeparator
.
What's the difference? When should I use one over the other?
In the File
class there are two strings, separator
and pathSeparator
.
What's the difference? When should I use one over the other?
If you mean File.separator
and File.pathSeparator
then:
File.pathSeparator
is used to separate individual file paths in a list of file paths. Consider on windows, the PATH environment variable. You use a ;
to separate the file paths so on Windows File.pathSeparator
would be ;
.
File.separator
is either /
or \
that is used to split up the path to a specific file. For example on Windows it is \
or C:\Documents\Test
File.separator
should be File.fileSeparator
regarding to File.pathSeparator
–
Willms File
. I think the file part is implied. But who knows why they did a lot of what they did with Java. –
Caretaker File.separator
is an anti-pattern because Windows
paths aren't required to use it (they can use forward slashes instead). –
Clipfed java.io.File
class contains four static separator variables. For better understanding, Let's understand with the help of some code
Note that all of these are final variables and system dependent.
Here is the java program to print these separator variables. FileSeparator.java
import java.io.File;
public class FileSeparator {
public static void main(String[] args) {
System.out.println("File.separator = "+File.separator);
System.out.println("File.separatorChar = "+File.separatorChar);
System.out.println("File.pathSeparator = "+File.pathSeparator);
System.out.println("File.pathSeparatorChar = "+File.pathSeparatorChar);
}
}
Output of above program on Unix system:
File.separator = /
File.separatorChar = /
File.pathSeparator = :
File.pathSeparatorChar = :
Output of the program on Windows system:
File.separator = \
File.separatorChar = \
File.pathSeparator = ;
File.pathSeparatorChar = ;
To make our program platform independent, we should always use these separators to create file path or read any system variables like PATH, CLASSPATH.
Here is the code snippet showing how to use separators correctly.
//no platform independence, good for Unix systems
File fileUnsafe = new File("tmp/abc.txt");
//platform independent and safe to use across Unix and Windows
File fileSafe = new File("tmp"+File.separator+"abc.txt");
String
and char
returned by the above-mentioned methods do return the properly formatted backslash (if on Windows). –
Loferski You use separator when you are building a file path. So in unix the separator is /
. So if you wanted to build the unix path /var/temp
you would do it like this:
String path = File.separator + "var"+ File.separator + "temp"
You use the pathSeparator
when you are dealing with a list of files like in a classpath. For example, if your app took a list of jars as argument the standard way to format that list on unix is: /path/to/jar1.jar:/path/to/jar2.jar:/path/to/jar3.jar
So given a list of files you would do something like this:
String listOfFiles = ...
String[] filePaths = listOfFiles.split(File.pathSeparator);
/var/temp
then it's useless to use File.separator
since you already have platform-dependent code. Might as well hardcode the path. –
Godunov © 2022 - 2024 — McMap. All rights reserved.