Get System Properties in Java

Source: Internet
Author: User
Tags define object definition execution file separator key string version
Get System Properties in Java

Daqing Oilfield Limited Liability company Soyoung
01-5-22 11:12:51

--------------------------------------------------------------------------------


The Java language is favored by many programmers because of its object-oriented, cross-platform, portability and high security, and more and more people use it as the first choice of application software development language.
When a Java application is running, especially when running across a platform, you need to determine the operating system type, user JDK version, and user working directory that changes with the work platform to ensure that the program runs correctly. In general, you can use the methods in the System attribute class (properties) provided by JDK to quickly get information about your work environment. In addition, the program developer can define the system properties files related to the application and dynamically load the programmer-defined property files to control the operation during the execution of the user program.
Through the analysis of the System attribute class, this paper introduces how to define the System Properties file and discusses the definition of the security policy file.
Attribute Class
The inheritance relationships of Java properties classes are as follows:
Java.lang.Object
+--java.util.dictionary
+--java.util.hashtable
+--java.util.properties
When the application starts executing, the program first reads the system's default properties. If a user property file is defined, the application loads the property file. The program can dynamically modify the property definition according to the execution, and save the property file before the program finishes running.
Gets the method of the property:
Contains (object value), ContainsKey (object key): If a given argument or property keyword is defined in the property sheet, the method returns True, otherwise false;
GetProperty (String key), GetProperty (string key, string default): Gets the key value based on the given property keyword;
List (PrintStream s), List (printwriter W): Output The contents of the property sheet in the output stream;
Size (): Returns the number of property keywords defined in the current property sheet.
To set the method for a property:
Put (object key, Object value): Appends the property key and keyword value to the property sheet;
Remove (Object key): Removes the keyword from the property sheet.
Get System Properties
System Properties refer to the operating system configuration information and software information associated with the user program. The property keywords that are typically associated with user programs include:
File.separator: File separator, Windows environment "\", UNIX environment is "/";
User.home: Directory of Household heads;
Java.home:Java the installation directory of real-time operating environment;
installation directory of Java.ext.dirs:JDK;
Os.name: Operating system name;
User.Name: User login name;
Os.version: Operating system version;
Path.separator: The current operating system's path separator;
User.dir: The directory where the current user program resides.
The following is an example of how to obtain system properties.
/*getsystemproperties.java*/
Import java.util.Properties;
public class Getsystemproperties
{
public static void Main (String args[])
{
Construct attribute class prop by obtaining system properties
Properties prop = new properties (
System.getproperties ());
Output the contents of system properties in standard output
Prop.list (System.out);
}
Determining program execution flow based on acquired system properties
......
}
After the above program is executed, output similar to the following is produced in the Windows environment:
User.home=c:\win95
Java.home=d:\jdk1.2\jre
java.ext.dirs=d:\jdk1.2
Os.name=windows 95
User.name=office
Java.vm.name=classic VM
os.version=4.10
path.separator=;
File.separator=\
User.dir=d:\javatest
How property files are manipulated
Java program developers can set parameters for the program to run by defining a property file. A property file is a program external file that, when the application is initially run, can be used to get parameters about the program's running by reading the property file. For example, when a program is running, it needs to execute the program's external control procedure, at which point the method of defining the property file can be achieved. The following example illustrates how the property file is manipulated:
/*propertyfile.java*/
Introducing related Classes
Import java.io.*;
Import java.util.Properties;
public class Propertyfile
{
Define file input and output streams
Static FileInputStream fis;
static FileOutputStream Fos;
public static void Main (String args[])
{
To generate a new Property object
Properties prop = new properties ();
Try
{
Generates file input and output streams, the input stream points to a user-defined property file, and the output stream points to a newly defined property file by the application
FIS = new FileInputStream
("FirstProp.txt");
FOS = new FileOutputStream
("SecondProp.txt");
}
catch (FileNotFoundException e)
{
SYSTEM.OUT.PRINTLN ("Cannot create the file stream");
}
Try
{
Loading System Properties from an input file
Prop.load (FIS);
Change the value of a property key based on program execution
Prop.put ("Switch", "1");
Output new Property file SecondProp.txt
Prop. Save (FOS, "--A new properties file-");
}
catch (IOException E)
{
System.out.println ("Exception in Repleace the keyword");
}
}
}
Before the program executes, the user must first define the property file FirstProp.txt, which reads as follows:
Switch= 0
version= 1.0
directory= Javatest
After the program is run, output the new property file SecondProp.txt, which reads as follows (note the difference between the two file keyword switch contents):
#--a New Properties file--
#sun Mar 21:22:40 CST 2001
Switch= 1
version= 1.0
directory= Javatest
From the example, it can be seen that the property class properties provided by JDK can easily control the execution flow of the application outside the program, thus simplifying the programming difficulty and making the program flow controllability better.
Security Policy Files
Java provides a security policy for the application environment that enables different code to have different access permissions to system resources. The Java Application Security policy is expressed by the policy object and is implemented by defining a security policy file. The security policy files for Java 1.2 are divided into level three: System security policy files, user security policy files, and default security policy files. When the Java application is started, load the security policy content in sequence. The following describes the definition of a security policy file in conjunction with the typical security policy file content:
Grant
{
Set Read permissions on system and user directories
Permission Java.util.PropertyPermission "User.dir",
"Read";
Permission Java.util.PropertyPermission "User.home", "read";
Permission Java.util.PropertyPermission "Java.home", "read";
Permission Java.util.PropertyPermission "Java.class.
Path "," read ";
Permission Java.util.PropertyPermission "User.Name", "read";
Other security policy content
};
The above security policy file defines the permissions that the application has read for system content such as user directory, user login directory, JDK installation directory, user name, and so on.

(Web page edit: Xu Xianyang)




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.