java读写xml和properties配置文件and Properties支持中文(转)

来源:互联网 发布:php后台 编辑:程序博客网 时间:2024/06/08 09:29

      【注:内容有点长,但解说得很详细清楚】

      在java.util 包下面有一个类 Properties,该类主要用于读取以项目的配置文件(以.properties结尾的文件和xml文件)。
  Properties的构造函数有两个,一个不带参数,一个使用一个Properties对象作为参数。

  使用Properties读取.properties文件

  test.properties文件如下:

 

 

[java] view plaincopyprint?
  1.  #测试环境配置:平台路径配置  
  2.   jstrd_home=D:/TMS2006/webapp/tms2006/WEB-INF/  
  3.   dbPort = localhost  
  4.   databaseName = mydb  
  5.   dbUserName = root  
  6.   dbPassword = root  
  7.   # 以下为数据库表信息  
  8.     dbTable = mytable  
  9.   # 以下为服务器信息  
  10.   ip = 192.168.0.9  

 

  读取test.properties的方法如下:

  

[java] view plaincopyprint?
  1. impor java.io.*;  
  2.   
  3.   import java.util.*;  
  4.   
  5.   public class ReadProperties{  
  6.   
  7.   public static void main(String[] args) {  
  8.   
  9.   File pFile = new File("e://test.properties");    // properties文件放在e盘下(windows)  
  10.   
  11.   FileInputStream   pInStream=null;  
  12.   
  13.   try {  
  14.   
  15.   pInStream = new FileInputStream(pFile );  
  16.   
  17.   }   
  18.   
  19.       catch (FileNotFoundException e) {  
  20.   
  21.   e.printStackTrace();   //To change body of catch statement use File | Settings | File Templates.  
  22.   
  23.   }  
  24.   
  25.   Properties p = new Properties();  
  26.   
  27.   try {  
  28.   
  29.   p .load(pInStream );       //Properties 对象已生成,包括文件中的数据  
  30.   
  31.   }   
  32.   
  33.       catch (IOException e) {  
  34.   
  35.   e.printStackTrace();    //To change body of catch statement use File | Settings | File Templates.  
  36.   
  37.   }  
  38.   
  39.   Enumeration enu = p.propertyNames();     //取出所有的key  
  40.   
  41.   //输出--1   
  42.   
  43.   p.list(System.out) ;        //System.out可以改为其他的输出流(包括可以输出到文件)  
  44.   
  45.   //输出--2   
  46.   
  47.       while( enu .hasMoreElements()){  
  48.   
  49.   System.out.print("key="+enu.nextElement());  
  50.   
  51.   System.out.print("value="+p.getProperty((String)enu .nextElement()));  
  52.   
  53.   }  
  54.   
  55.   }  
  56.   
  57.   }  
  

  读取xml格式的配置文件

  test.xml文件ruxi

  

[xhtml] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3.   <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">  
  4.   
  5.   <properties>  
  6.   
  7.   <entry key="koo">bar</entry>  
  8.   
  9.   <entry key="fu">baz</entry>  
  10.   
  11.   </properties>  
  

  读取xml的方法

  

[java] view plaincopyprint?
  1. import java.io.IOException;  
  2.   
  3.   import java.io.File;  
  4.   
  5.   import java.io.FileInputStream;  
  6.   
  7.   import java.util.Properties;  
  8.   
  9.   public class Test {  
  10.   
  11.   public static void main(String[] args) {  
  12.   
  13.   File pFile = new File("e://test.xml");    // properties文件放在e盘下(windows)  
  14.   
  15.   FileInputStream pInStream = null;  
  16.   
  17.   try {  
  18.   
  19.   pInStream = new FileInputStream(pFile);  
  20.   
  21.   Properties p = new Properties();  
  22.   
  23.   p.loadFromXML(pInStream);  
  24.   
  25.   p.list(System.out);  
  26.   
  27. catch (IOException e) {  
  28.   
  29.   e.printStackTrace();  
  30.   
  31.   }  
  32.   
  33.   }  
  34.   
  35.   }  
  

  通过list 方法将Properties写入Properties文件

  

[java] view plaincopyprint?
  1. import java.io.IOException;  
  2.   
  3.   import java.io.File;  
  4.   
  5.   import java.io.FileInputStream;  
  6.   
  7.   import java.io.PrintStream;  
  8.   
  9.   import java.util.Properties;  
  10.   
  11.   public class Test {  
  12.   
  13.   public static void main(String[] args) {  
  14.   
  15.   Properties p = new Properties();  
  16.   
  17.   p.setProperty("id","dean");  
  18.   
  19.   p.setProperty("password","123456");  
  20.   
  21.   try{  
  22.   
  23.   PrintStream fW = new PrintStream(new File("e://test1.properties"));  
  24.   
  25.   p.list(fW );          
  26.   
  27. catch (IOException e) {  
  28.   
  29.   e.printStackTrace();  
  30.   
  31.   }  
  32.   
  33.   }  
  34.   
  35.   }  
  

  保存为xml

  

[java] view plaincopyprint?
  1. import java.io.IOException;  
  2.   
  3.   import java.io.File;  
  4.   
  5.   import java.io.FileInputStream;  
  6.   
  7.   import java.io.PrintStream;  
  8.   
  9.   import java.util.Properties;  
  10.   
  11.   public class Test {  
  12.   
  13.   public static void main(String[] args) {  
  14.   
  15.   Properties p = new Properties();  
  16.   
  17.   p.setProperty("id","dean");  
  18.   
  19.   p.setProperty("password","123456");  
  20.   
  21.   try{  
  22.   
  23.   PrintStream fW = new PrintStream(new File("e://test1.xml"));  
  24.   
  25.   p.storeToXML(fW,"test");  
  26.   
  27.   } catch (IOException e) {  
  28.   
  29.   e.printStackTrace();  
  30.   
  31.   }  
  32.   
  33.   }  
  34.   
  35.   }  
  

以上转自百度文库:http://wenku.baidu.com/view/36557f69a45177232f60a27b.html

以下转自:http://liudaoru.javaeye.com/blog/340367

将文件改为UTF_8编码格式, 用Edit 编辑器打开,就可以看见文件内容显示汉字。

 

在读取的时候,还需要转换才能读取来汉字字符串,读取方法: 

String path = new String(com.GetKey("sql").getBytes("ISO-8859-1"), "UTF-8");

 其中方法(GetKey) 是获取配置文件字符串的

 

 

原创粉丝点击