How to get system information in Java

xiaoxiao2021-03-06  82

The Java language is favored by many programming staff, and more and more people are preferred by many programmers, and more and more people are preferred for application software development. 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. Inheritance attribute class Java Properties class 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 attributes: ● Contains (Object value): If a given parameter or attribute key is defined in the property table, the method returns true, otherwise returns false; ● getProperty (String Key), GetProperty (String Key, String Default): Get keyword values ​​based on a given attribute keyword; ● List (PrintStream S), List (PrintWriter W): Output attribute table content in the output stream; ● Size (): Return the current The number of properties defined in the property table. Method for setting up properties: ● PUT (Object Key, Object Value): Additional property keywords and keywords to the property table; ● Remove (Object Key): Delete keywords from the property table. Get System Properties System Properties refers to operating system configuration information and software information related to user programs. The properties keywords that are usually related to the user program include: ● File.Separator: File separator, "" in the Windows environment, "/" in the UNIX environment; ● User.home: User main directory; ● java.home: Java Install directory of real-time operating environment; ● Java.ext.dirs: JAVA.EXT.DIRS: JDK installation directory; ● OS.Name: Operating system name; ● user.name: User login name; ● Os.Version: Operating system version; ● Path.separator : The 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.

/*getSystemProperties.java*/ import java.util.Properties; public class getSystemProperties {public static void main (String args []) {// attributes system configured by obtaining attribute class prop Properties prop = new Properties (System.getProperties () ); // Output system properties in the standard output Prop.List (System.out); // Depending on the acquired system attribute, determine the program execution process} The above program is executed, producing a similar output similar to the Windows environment: User.home = C: Win95 java.home = D: jdk1.2jre java.ext.dirs = D: jdk1.2 os.name = Windows 95 user.name = office java.vm.name = Classic vm os.version = 4.10 path.seParetor =; file.sePare = user.dir = d: JavaTest Property File Operating Method JAVA program developers can set the parameters of the program run by defining the properties 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*/ // Introduced related class import java.io. *; import java.util.properties; public class print {// definition file input and output stream static fileInputstream FIS; Static FileOutputStream Fos; Public Static Void Main (String Args []) {// Generate New Properties Object Properties Prop = New Properties (); try {// Generate file input and output flow, input stream points to users have defined Attribute file, output stream points fis = new fileinputstream ("firstprop.txt"); fos = new fileoutputstream ("secondprop.txt");} catch (filenotfoundexcection); ("Cannot Create THE File Stream");} Try {// From Enter File Load System Properties Prop.Load (FIS); // Change the value of attribute keywords in Prop.Put ("Switch", "according to the program execution "); // 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 execution, the user must first define the attribute file firstprop.txt, which is as follows: switch = 0 version = 1.0 Directory = JavateSt program runs, output a new property file secondprop.txt, the file content is as follows (Note the difference between the two file key Switch content): # - a new profmenties File - #sun Mar 11 21:22 : 40 CST 2001 Switch = 1 Version = 1.0 Directory = Javatest It can be seen from the previous example that the attribute class Properties provided by JDK can easily control the execution process of the application outside the program, thereby simplifying the programming difficulty, so that the program The process is more controllable. The security policy of the application environment provided by the security policy file 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.

转载请注明原文地址:https://www.9cbs.com/read-121992.html

New Post(0)