Pages

System properties in java

This post contains information concerning, How to access system properties in JAVA ?.

Some times it is necessary to extract the system (OS) dependent information to write down System (OS) independent code.

it show we'd like to access home directory of a user. it is possible that some user uses Linux and a few uses Windows. thus we'd like to access current system setting and that we additionally understand that windows and Linux uses different characters (windows uses '/r/n' and Linux uses '/n') to point out finish of line during a file. thus we'd like to understand these special character to form our program to run in Windows and Linux or anything. for this we tend to used

System.getProperty("path.separator");


instead of using "\r\n"  or "\n" explicity.

exmple
 String lineSeparator = System.getProperty("line.separator"); 
 // all other code goes here

 File out = new File("/tmp/my.txt");
 BufferedWriter bw = new BufferedWriter(new FileWriter(out));

  bw.write(reverseStr);
 bw.write(lineSeparator); // system independent new line

 bw.close();

 

\n = CR (Carriage Return) // Used as a new line character in Unix
\r = LF (Line Feed) // Used as a new line character in Mac OS
\n\r = CR + LF // Used as a new line character in Windows

This table define some most commonly used keys for accessing system property. This tabls is provided by http://docs.oracle.com

Key Meaning
"file.separator" Character that separates components of a file path. This is "/" on UNIX and "\" on Windows.
"java.class.path" Path used to find directories and JAR archives containing class files. Elements of the class path are separated by a platform-specific character specified in the path.separator property.
"java.home" Installation directory for Java Runtime Environment (JRE)
"java.vendor" JRE vendor name
"java.vendor.url" JRE vendor URL
"java.version" JRE version number
"line.separator" Sequence used by operating system to separate lines in text files
"os.arch" Operating system architecture
"os.name" Operating system name
"os.version" Operating system version
"path.separator" Path separator character used in java.class.path
"user.dir" User working directory
"user.home" User home directory
"user.name" User account name

1 comment:

  1. very good information you share and really this post is very helpful for beginners for knowing the features of java in their system. keep posting such good and knowledgeable posts in your blog.

    ReplyDelete