The Java Property class uses

Source: Internet
Author: User

http://blog.csdn.net/hopestar2/article/details/6372883

A variety of configuration files are often used in projects, with. Properties, and also in. xml format

Can be handled through the Java.utils.Property class.

1. Read the. properties file

[C-sharp]View PlainCopy
  1. File PFile = new File ("test.properties");
  2. FileInputStream pinstream=null;
  3. try {
  4. Pinstream = new FileInputStream (PFile);
  5. } catch (FileNotFoundException e) {
  6. E.printstacktrace ();
  7. }
  8. Properties P = new properties ();
  9. try {
  10. P.load (Pinstream);
  11. } catch (IOException e) {
  12. E.printstacktrace ();
  13. }
  14. Enumeration ENU = P.propertynames (); //Remove all keys
  15. P.list (System.   Out);
  16. While (Enu.hasmoreelements ())
  17. {
  18. String ThisKey = (string) enu.nextelement ();
  19. System.  out.println ("key=" +thiskey);
  20. System.  out.println ("value=" +p.getproperty (ThisKey));
  21. }

The. Properties test file reads as follows:

Name = Tinyfun
Age = 25
Sex = Mans
title = Software Developer

2. Read the XML file

[Java]View PlainCopy
  1. File PFile = new File ("Test.xml");
  2. FileInputStream pinstream = null;
  3. try {
  4. Pinstream = new FileInputStream (PFile);
  5. Properties P = new properties ();
  6. P.loadfromxml (Pinstream);
  7. P.list (System.out);
  8. } catch (IOException e) {
  9. E.printstacktrace ();
  10. }

The XML file is as follows:

[XHTML]View PlainCopy
  1. <? XML version= "1.0" encoding="UTF-8" standalone="No"?>
  2. <! DOCTYPE Properties SYSTEM "Http://java.sun.com/dtd/properties.dtd">
  3. <properties>
  4. <comment>test</comment>
  5. <entry key="age">25</entry>
  6. <entry key="name">tinyfun</entry>
  7. <entry key="Sex">man</entry>
  8. <entry key="title">software developer</entry>
  9. </Properties>

3. Writing an XML file

[Java]View PlainCopy
  1. Properties P = new properties ();
  2. P.setproperty ("name","Tinyfun");
  3. P.setproperty ("age","25");
  4. P.setproperty ("Sex","man");
  5. P.setproperty ("title","software developer");
  6. try{
  7. PrintStream FW = new PrintStream (new File ("Test.xml"));
  8. P.storetoxml (FW,"test");
  9. } catch (IOException e) {
  10. E.printstacktrace ();
  11. }

4. Write to the. Properties file

[Java]View PlainCopy
  1. Properties P = new properties ();
  2. P.setproperty ("name","Tinyfun");
  3. P.setproperty ("age","25");
  4. P.setproperty ("Sex","man");
  5. P.setproperty ("title","software developer");
  6. try{
  7. PrintStream FW = new PrintStream (new File ("test.properties"));
  8. P.list (FW); }
  9. catch (IOException e) {
  10. E.printstacktrace ();
  11. }

Java Property class use

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.