How to get system information in Java (ZT)

xiaoxiao2021-03-06  55

???? Java language With its object-oriented, cross-platform, highly portable, high security, has been favored by numerous programming staff, more and more people use their first choice for application software development. • When the Java application is 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 by analyzing 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?                                        

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. • How to get attributes:? ● Contains (Object? Value), Containskey (Object? Key):

If a given parameter or attribute key is defined in the attribute table, the method returns true, otherwise returns false ;?

Get keyword values ​​according to 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. ? Setting the properties:? ● PUT (Object? Key,? Object? Value)

?: Additional property keywords and keywords in the property table; • Remove (Object? Key)?

Remove keywords from the property table. • Get system properties? System properties refer to operating system configuration information and software information related to the user program. The attribute 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 catalog;? ● java.home ?:

The installation directory of the Java real-time running environment; • java.ext.dirs ?:

JDK installation directory; • 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 properties will be described below. ?

/ * PUBLIC? Class? {? PUBLIC? Static? Void? {?? // by obtaining system properties Constructing the property class? Prop? New? Proties (?? system.getproperties () ;? // Output system properties in the standard output? Prop.List (system.out) ;?} ? // Depending on the acquired system attribute determines the program execution process? .ext.dirs = D: JDK1.2? os.name = windows? 95? user.name = office? java.vm.name = Classic? vm? os.version = 4.10? path.seParetor =;? file.separator =? user.dir = d: JavateSt? How does the property file? 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, a method of operating the property file:?

/*PropertyFile.java*/? // Introduced 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 object? Properties? Prop? =? New? Properties () ;? try? {?? // Generate file input and output flow, input stream points to the user's already defined properties file, 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? crete? the? file? stream" );?}? try? {? // From the input file load system properties? Prop.Load (fis); change the value of the properties keyword based on the program execution? Prop.Put ("Switch", "1" );? // Output new property file secondprop.txt? Prop ?save? (? Fos?, "? A? New? Proties? File-");?}? Catch (ooException? E)? {? System.out.println ("Exception? IN? Repleace ??}?}?}?

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 =? JavateSt? After running, output a new property file secondprop.txt, The contents of the document are as follows (pay attention to observe the difference between the two file key Switch contents):? # - a? New? Proties? File -? #Sun? Mar? 11? 21: 22: 40? Cst? 2001? Switch = ? 1? Version =? 1.0? Directory =? Javatest

• From the above example, it can be seen that the attribute class Properties provided by JDK can easily control the execution process of the application outside the program to simplify the programming difficulty, so that the program process is more controllable. • Security Policy File? The security policy for 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? {?? // Set the "read" for 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.

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

New Post(0)