Java Attribute Class: common methods of Properties, javaproperties

Source: Internet
Author: User

Java Attribute Class: common methods of Properties, javaproperties
The Properties class itself is a subclass of the Hashtable class and stores data in the form of key-value. Set and obtain attributes:

Public class PropertiesDemo01 {public static void main (String args []) {Properties pro = new Properties (); // create the Properties object pro. setProperty ("BJ", "BeiJing"); // sets the property pro. setProperty ("TJ", "TianJin"); pro. setProperty ("NJ", "NanJing"); System. out. println ("1. BJ attributes exist:" + pro. getProperty ("BJ"); System. out. println ("2. SC property does not exist:" + pro. getProperty ("SC"); System. out. println ("3. The SC property does not exist, and the displayed default value is also set:" + pro. getProperty ("SC", "no "));}};


Save the property to a common property file:
Public class PropertiesDemo02 {public static void main (String args []) {Properties pro = new Properties (); // create the Properties object pro. setProperty ("BJ", "BeiJing"); // sets the property pro. setProperty ("TJ", "TianJin"); pro. setProperty ("NJ", "NanJing"); File file = new File ("D:" + File. separator + "area. properteis "); // specify the file to be operated try {pro. store (new FileOutputStream (file), "Area Info"); // Save the attribute to a common file} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}};


Read content from the property file:
Public class PropertiesDemo03 {public static void main (String args []) {Properties pro = new Properties (); // create the Properties object File = new file ("D: "+ File. separator + "area. properteis "); // specify the file to be operated try {pro. load (new FileInputStream (file); // read attribute file} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} System. out. println ("1. BJ attributes exist:" + pro. getProperty ("BJ"); System. out. println ("2. SH attributes exist:" + pro. getProperty ("SH "));}};


Save attributes in an XML file:
Public class PropertiesDemo04 {public static void main (String args []) {Properties pro = new Properties (); // create the Properties object pro. setProperty ("BJ", "BeiJing"); // sets the property pro. setProperty ("TJ", "TianJin"); pro. setProperty ("NJ", "NanJing"); File file = new File ("D:" + File. separator + "area. xml "); // specify the file to be operated try {pro. storeToXML (new FileOutputStream (file), "Area Info"); // Save the attribute to a common file} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}};


Read attributes from XML files:
Public class PropertiesDemo05 {public static void main (String args []) {Properties pro = new Properties (); // create the Properties object File = new file ("D: "+ File. separator + "area. xml "); // specify the file to be operated try {pro. loadFromXML (new FileInputStream (file); // read attribute file} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} System. out. println ("1. BJ attributes exist:" + pro. getProperty ("BJ "));}};







The specific functions and usage of the Properties class in java programming

If you are not familiar with the java. util. Properties class, it is used to store key-value pairs in a file. The key-value pairs are separated by equal signs. (As shown in Listing 1 ). Recently updated java. util. the Properties class now provides an easier way to configure program loading and storage: loadFromXML (InputStreamis) and storeToXML (OutputStream OS, String comment.

Here is a detailed description, hoping to help you.

Listing 1. Example of a group of Properties

Foo = bar
Fu = baz

After listing 1 is loaded into the Properties object, you can find two keys (foo and fu) and two values (bar of foo and baz of fu. This class supports embedding Unicode strings with \ u, but here it is important that each item is treated as a String.

Listing 2 shows how to load an attribute file and list its current set of keys and values. You only need to pass the InputStream of this file to the load () method, and then add each key-value pair to the Properties instance. Use list () to list all attributes or use getProperty () to obtain individual attributes.

Listing 2. Loading attributes

Import java. util .*;
Import java. io .*;

Public class LoadSample {
Public static void main (String args []) throws Exception {
Properties prop = new Properties ();
FileInputStream FCM =
New FileInputStream ("sample. properties ");
Prop. load (FS );
Prop. list (System. out );
System. out. println ("\ nThe foo property:" +
Prop. getProperty ("foo "));
}
}

Run the LoadSample program to generate the output shown in listing 3. Note that the order of the output key-value pairs in the list () method is different from that in the input file. The Properties class stores a group of key-value pairs in a hash (hashtable, actually a Hashtable subclass), so the sequence cannot be guaranteed.

Listing 3. LoadSample output

-- Listing properties --
Fu = baz
Foo = bar

The foo property: bar

XML property File
There is nothing new here. The Properties class always works like this. However, the new feature is to load a set of attributes from an XML file. Its DTD is shown in Listing 4.

Listing 4. Attribute DTD

<? Xml version = "1.0" encoding = "UTF-8"?>
<! -- DTD for properties -->
<! ELEMENT properties (comment ?, Entry *)>
<! ATTLIST properties version CDATA # fixed" 1.0 ">
<! ELEMENT comment (# PCDATA)>
... The remaining full text>

What are the definitions of attribute classes and attribute files in Java?

Well, what I just learned first is to connect to the database (SQL2005) and put the driver url and other information of the database connection in that attribute file (*. db. properties. microsoft. sqlserver. jdbc. SQLServerDriver
Url = jdbc: sqlserver: // localhost: 1433; databaseName = Books
User = sa
Password = actually, when defining an attribute, there is no difficulty in the form of name = value. Then I load the attribute file information through a static class public final class Env extends Properties {// inherit Properties
Private static Env instance; public static Env getInstance (){
If (instance! = Null ){
Return instance;
} Else {
MakeInstance ();
Return instance;
}
} // This is the synchronization method private static synchronized void makeInstance (){
If (instance = null ){
Instance = new Env ();
}
} // Load the property File
Private Env (){
InputStream is = getClass (). getResourceAsStream ("/db. properties ");
Try {
Load (is );
} Catch (Exception e ){
System. out. println ("error: failed to read attribute file ");
}
}
}
This static class uses the singleton mode, but it does not need to be so troublesome. The key is to read the attribute file. You can see the public static synchronized Connection getConnection ()
Throws Exception {
// Read configuration information
String driverClassName = Env. getInstance (). getProperty ("driver ");
String url = Env. getInstance (). getProperty ("url ");
String user = Env. getInstance (). getProperty ("user ");
String password = Env. getInstance (). getProperty ("password ");
Connection con = null;
Try {
// Load the database program
Class. forName (driverClassName );
Con = DriverManager. getConnection (url, user, password );
} Catch (Exception e ){
Throw new Exception ("the database connection cannot be obtained! ");
... The remaining full text>
 

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.