Get system properties in Java
At the Java application run, especially when running in a cross-platform work environment, you need to determine how the operating system type, user JDK version, and user working directory, etc., to ensure that the program is operating correctly. In general, work environment information can be quickly obtained using methods in the system attribute class (Properties) provided by JDK. In addition, program developers can also define system properties files related to the application, dynamically load programmers defined attribute files during user program execution to control program operation.
This article describes how to define system properties files in connection with the actual examples of the system attribute class, and discuss the definition of the security policy file.
The inheritance relationship between the attribute class Java Properties class is as follows:
Java.lang.object
- java.util.dictionary
- java.util.hashtable
- Java.util.Properties
When the application starts execution, the program first reads the default properties of the system. If the user property file is defined, the application loads the property file. The program run can dynamically modify the attribute definition according to the execution, and save the properties file before the program ends.
Get attribute methods:
● Contains (Object Value), Containskey: If a given parameter or attribute key is defined in the attribute table, the method returns true, otherwise returns false;
● getProperty (String Key), getProperty (String Key, String Default): Get keyword values based on a given attribute key;
● List (PrintStream S), List (PrintWriter W): Output attribute table content in the output stream;
● Size (): Returns the number of properties defined in the current property table.
Set the method of setting attributes:
● PUT (Object Key, Object Value): Additional property keywords and keywords to the properties table;
● Remove (Object Key): Remove keywords from the property table.
Get system properties
System properties refer to operating system configuration information related to user programs and software information. The property keywords that are usually related to the user program include:
● File.seParetor: File separator, "/" in the Windows environment, "/" in the UNIX environment;
● User.home: User main directory;
● Java.home: The installation directory of the Java real-time running environment;
● Java.ext.dirs: The installation directory of JDK;
● OS.NAME: Operating system name;
● User.name: User login name;
● Os.Version: Operating system version;
● path.seParetor: Path separator of the current operating system;
● User.dir: The current user program is located.
Next, a method of obtaining a system attribute will be described below.
/* fulsystemproperties.java*/
Import java.util.properties;
Public Class getSystemProperties
{
Public static void main (string args [])
{
// By obtaining system properties constructs property class PropPerties Prop = New Properties
System.getproperties ());
/ / Output system properties in standard output
Prop.List (System.out);
}
/ / Determine the program execution process based on the acquired system attribute
......
}
After the above program is executed, the following output is generated 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
Attribute file operation method
Java program developers can set the parameters running by defining the property file. The property file is a program external file. When the application is initially run, you can get the parameters of the program run by reading the properties file. For example, the execution process of the program external control program is required at runtime, which can be achieved by defining the method of defining the attribute file. Next, the example of the operating method of the attribute file is explained:
/*PropertyFile.java*/
// introduce related classes
Import java.io. *;
Import java.util.properties;
Public Class PropertyFile
{
/ / Define file input and output flow
Static FileInputStream Fis;
Static FileOutputStream Fos;
Public static void main (string args [])
{
/ / Generate new attribute objects
Properties Prop = New Properties ();
Try
{
/ / Generate a file input and output stream, the input stream points to the user that the user has defined, the output stream points to the attribute file that is defined 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
{
// Load system properties from entering file
Prop.Load (FIA);
/ / Change the value of the property key based on the program execution
Prop.Put ("Switch", "
1"
);
// Output new property file secondprop.txt
Prop .save (fos, "- a new profment");
}
Catch (IOException E)
{
System.out.println ("Exception in Repleace The Keyword");
}
}
}
Before the program is executed, the user must first define the attribute file firstprop.txt, which is as follows:
Switch = 0
Version = 1.0
Directory = javatests
After the program is run, the new property file SECONDPROP.TXT is output, which is as follows (note to observe the difference between the two file key Switch contents):
# - a new profment - # Sun Mar 11 21:22:40 CST 2001
Switch = 1
Version = 1.0
Directory = javatests
As you can see from the above example, the attribute class Properties provided by JDK can easily control the execution process of the application outside the program, simplifying the programming difficulty, making the program process more controllability.
Security policy file
The security policy of the application environment provided by Java enables different code to have different access licenses for system resources. The Java application security policy is expressed by the Policy object and is implemented by defining security policy files. Java 1.2 security policy files are divided into three levels: 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 order. The definition of the security policy file is described below with a typical security policy file content:
Grant
{
/ / To "read" permission to the system and user directory
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 the user directory, user login directory, JDK installation directory, user name and other system content.