If you want to make your java code platform independent, especially for java file operation, you should know java file separators. There are four separators by default, they are : File.separator
, File.separatorChar
, File.pathSeparator
and File.pathSeparatorChar
. We will introduce them one by one.
1. Java File Separator
java.io.File
class contains two static variable which will be used to make file path separation java code platform independent.
File.separator
: This is the String value that your os used to separate file path. So to make your java code correctly, you should use below code when create a pathString filePath = "test" + File.separator + "dev2qa.txt";
not useString filePath = "test/dev2qa.txt";
.- You can also use
System.getProperty("file.separator")
to get current os file separator. File.separatorChar
: Return Char type instead of String type.- Example Code.
public String getOsFileSeparator() { String ret = ""; ret = File.separator; ret = System.getProperty("file.separator"); ret = String.valueOf(File.separatorChar); System.out.println("Current OS file separator = " + ret); String filePath = "test" + ret + "dev2qa.txt"; System.out.println("filePath = " + filePath); return ret; }
2.Java Path Separator
Path separator is used to separate multiple value in system environment variable’s value such as PATH, CLASSPATH etc.
If you are using windows. Open a dos command line window. Input command set
like below, then you can see the system environment variable Path and it’s value. You can see the multiple Path value is separated by ;
. For Unix and Linux, Path variable value is separated by :
.
File.pathSeparator
: This is the static variable which return Path separator.System.getProperty("path.separator")
can also be used to get it.File.pathSeparatorChar
: Return Char type instead of String type.- Example code.
public String getOsPathSeparator() { String ret = ""; ret = File.pathSeparator; ret = System.getProperty("path.separator"); ret = String.valueOf(File.pathSeparatorChar); System.out.println("Current OS path separator = " + ret); return ret; }
3. Get Current Working Directory.
Some times we need to know which directory this java application is running at. We can use System.getProperty("user.dir")
. Below is the code example.
public String getCurrWorkingDir() { String ret = ""; ret = System.getProperty("user.dir"); System.out.println("Current working directory = " + ret); return ret; } public static void main(String[] args) { FileSeparatorExample fse = new FileSeparatorExample(); //fse.getOsFileSeparator(); //fse.getOsPathSeparator(); fse.getCurrWorkingDir(); }
Now export the java class to a runnable jar as below.
- Right click the java project in Eclipse left project list tree as below. Click “Export” menu item.
- Choose “Runnable JAR File” in popup window.
- Choose “FileSeparatorExample” class, set export destinations as below picture.
- Now go to the runnable jar saved folder. And run it, you can see below output.
4. Get All System Environment Variables And Their Value.
Some times when you want to get some value from system environment variable, you may can not remember the variable name.
But System.getProperties()
can be used to get all system environment variable and their value in a java.util.Properties
object.
Below example will iterate the Properties object and print out each system environment variable and it’s value.
public void getAllSystemEnvVariable() { Properties allProps = System.getProperties(); /* Get the keys object. */ Set keySet = allProps.keySet(); /* Iterate the key and get the related value. */ Iterator it = keySet.iterator(); while(it.hasNext()) { Object keyObj = it.next(); String key = (String)keyObj; Object valObj = allProps.get(key); System.out.println(key + " = " + valObj.toString()); } }
Output of above code:
5. Example java code download.
- [download id=”2263″]