【question】
Line separators, path separators, etc. are often different under different system platforms. like
The code copy is as follows:
The line separator is /r/n under Windows, /n under Linux, and /r under Mac
The path separator is / under Windows and / under Linux
How to get the delimiter of the current platform and other system-related states in a Java program?
【accomplish】
The code copy is as follows:
import java.util.Properties;
public class SeparatorUtils {
/* system properties to get separators */
static final Properties PROPERTIES = new Properties(System.getProperties());
/**
* get line separator on current platform
* @return line separator
*/
public static String getLineSeparator(){
return PROPERTIES.getProperty("line.separator");
}
/**
* get path separator on current platform
* @return path separator
*/
public static String getPathSeparator(){
return PROPERTIES.getProperty("path.separator");
}
}
class SeparatorUtilTest{
public static void main (String[] args){
System.out.println("Line separator is: " + SeparatorUtils.getLineSeparator());
System.out.println("Path separator is: " + SeparatorUtils.getPathSeparator());
}
}
【Note】
Other attributes that can be obtained:
java.version | Java Runtime Environment Version |
java.vendor | Java Runtime Environment Vendor |
java.vendor.url | Java vendor's URL |
java.home | Java installation directory |
java.vm.specification.version | Java Virtual Machine Specification Version |
java.vm.specification.vendor | Java Virtual Machine Specification Vendor |
java.vm.specification.name | Java Virtual Machine Specification Name |
java.vm.version | Java virtual machine implementation version |
java.vm.vendor | Java virtual machine implementation vendor |
java.vm.name | Java virtual machine implementation name |
java.specification.version | Java Runtime Environment Specification Version |
java.specification.vendor | Java Runtime Environment Specification Vendor |
java.specification.name | Java Runtime Environment Specification Name |
java.class.version | Java class format version number |
java.class.path | Java classpath |
java.library.path | List of paths to search when loading the library |
java.io.tmpdir | Default temporary file path |
java.compiler | The name of the JIT compiler to be used |
java.ext.dirs | Paths to one or more extension directories |
os.name | The name of the operating system |
os.arch | Operating system architecture |
os.version | Operating system version |
file.separator | File delimiter ("/" in UNIX systems) |
path.separator | Path separator (":" in UNIX systems) |
line.separator | Line delimiter ("/n" in UNIX systems) |
user.name | User's account name |
user.home | User's home directory |
user.dir | The user's current working directory |