Obtain system attributes during Java application

Source: Internet
Author: User
Tags file separator

Java is favored by many programmers because of its object-oriented, cross-platform, good portability, and high security. More and more people use Java as the preferred language for application software development.

When running Java applications, especially in a cross-platform working environment, you must determine the operating system type, JDK version, working directory, and other information that changes with the working platform, to ensure that the program runs correctly. In general, you can use the methods in the System Properties class (properties) provided by JDK to quickly obtain the work environment information.

In addition, program developers can define system property files related to applications, and dynamically load property files defined by programmers during user program execution to control program running.

This article introduces how to define the system property file based on the analysis of the system property class and the actual example, and discusses the definition of the security policy file.

When the application starts to run, the program first reads the default attributes of the system. If a user property file is defined, the application loads the property file. When the program is running, you can dynamically modify the attribute definition based on the execution conditions, and save the attribute file before the program ends running.

How to Get attributes:

Contains (object value) and containskey (Object key): if a given parameter or attribute keyword is defined in the Attribute Table, true is returned for this method; otherwise, false is returned;

● Getproperty (string key) and getproperty (string key, string default): Get the keyword value based on the given attribute keywords;

● List (printstream S) and list (printwriter W): outputs the Attribute Table content in the output stream;

● Size (): returns the number of attribute keywords defined in the current Attribute Table.

How to Set attributes:

● Put (Object key, object Value): append the value of the attribute keyword and keyword to the Attribute Table;

● Remove (Object key): deletes a keyword from the Attribute Table.

Obtain system attributes

System Properties refer to the operating system configuration information and software information related to user programs. Attribute keywords related to user programs usually include:

● File. separator: file separator. In Windows, it is "\", and in UNIX, it is "/".

● User. Home: the user's home directory;

● Java. Home: the installation directory of the Java real-time runtime environment;

● 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: directory of the current user program.

The following describes how to obtain system attributes using examples:

/* Getsystemproperties. Java */
Import java. util. properties;
Public class getsystemproperties
{
Public static void main (string ARGs [])
{
// Construct the property class prop by obtaining System Properties
Properties prop = new properties (
System. getproperties ());
// Output System attribute content in standard output
Prop. List (system. Out );
}
// Determine the Program Execution Process Based on the obtained system attributes
......
}

After the above program is executed, the following output is generated in Windows:

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

Operation Method of attribute File

Java developers can define attribute files to set program running parameters. An Attribute file is an external program file. When an application is initially running, you can read the attribute file to obtain the program running parameters. For example, when running a program, you need to control the execution process of the program outside the program. In this case, you can define the attribute file to achieve the goal. The following examples describe how to operate an attribute file:

/* Propertyfile. Java */
// Introduce related classes
Import java. Io .*;
Import java. util. properties;
Public class propertyfile
{
// Define the input and output streams of the file
Static fileinputstream FCM;
Static fileoutputstream Fos;
Public static void main (string ARGs [])
{
// Generate a new property object
Properties prop = new properties ();
Try
{
// Generate file input and output streams,
The input stream points to the User-Defined Property file,
The output stream points to the new property file defined by the application.
FS = new fileinputstream
(“Firstprop.txt ");
Fos = new fileoutputstream
(“Secondprop.txt ");
}
Catch (filenotfoundexception E)
{
System. Out. println
("Cannot create the file stream ");
}
Try
{
// Load system attributes from the input file
Prop. Load (FS );
// Change the attribute keyword Value Based on Program Execution
Prop. Put ("Switch", "1 ");
// Output the new example file secondprop.txt
Prop. Save
(FOS, "-- A new properties file -");
}
Catch (ioexception E)
{
System. Out. println
("Exception in repleace the keyword ");
}
}
}

Before the program is executed, the user first defines the firstprop.txt file, the content of which is as follows:

Switch = 0

Version = 1.0

Directory = javatest

After the program runs, it outputs the new second file secondprop.txt. The content of this file is as follows (observe the differences between the switch content of the two file keywords ):

# -- A new properties file --

# Sun Mar 21:22:40 CST 2001

Switch = 1

Version = 1.0

Directory = javatest

As shown in the preceding example, the property class properties provided by JDK can be used to conveniently control the execution process of the application outside the program. This simplifies the programming difficulty and makes the process more controllable.

Security policy file

The security policy of the application environment provided by Java enables different codes to have different access permissions for system resources. Java application security policies are expressed by policy objects and implemented by defining security policy files. Java1.2 security policy files are classified into three levels: system security policy files, user security policy files, and default security policy files.

After the Java application is started, the security policy content is loaded sequentially. The following describes the definition of the security policy file based on the content of the typical security policy file:

Grant
{
// Set the "read" permission 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
};

Recommended for beginners a programming technology learning site, 96 stack Software Programming Network, http://www.96dz.com, which has c ++ video tutorial, C # video tutorial, Java video tutorial download, c \ c ++, Java, and C #. net and other programming technology abstracts, including the current mainstream Linux programming and web programming learning materials video tutorial download.

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.