Java常用类 - Properties读写配置文件

本文详细介绍了Java中Properties类的功能,包括读写Properties文件、XML文件和其他格式文件的方法,并提供了处理中文字符的具体步骤。此外,还展示了如何在Web程序中加载资源文件。
Java中读写资源文件最重要的类是Properties,功能大致如下:
1. 读写Properties文件
2. 读写XML文件
3. 不仅可以读写上述两类文件,还可以读写其它格式文件如txt等,只要符合key=value格式即可.
 
注意:资源文件中含有中文时的处理方法 
1. 将中文字符通过工作转成utf8编码,可以通过Java自带的nativetoascii或Eclipse中的属性编辑器。
2. 直接调用 new String(youChineseString.getBytes("ISO-8859-1"), "GBK");
 
附:WEB程序中加载资源文件的方法
Properties prop = null; 
1. prop = Thread.currentThread().getContextClassLoader().getResourceAsStream("filename");
2. prop = this.getClass().getClassLoader().getResourceAsStream("filename");
 
Properties能读取以key,value存储的任何格式文件,究竟有什么神奇,猫一眼类结构,
Properties类结构
原来它继承了Hashtable并实现了Map接口,这样大家放心了吧。
在D:\的测试文件:
1、readfile.properties
username=myname
password=mypassword
chinese=中文
2、readxmlfile.xml  (注意保存为UTF-8格式)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="password">mypassword</entry>
<entry key="chinese">中文</entry>
<entry key="username">myname</entry>
</properties>

测试代码:
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  
import java.io.UnsupportedEncodingException;  
import java.util.Properties;  
  
public class PropertiesTest  
{  
  
    public static void main(String[] args)  
    {  
  
        String readfile = "d:" + File.separator + "readfile.properties";  
        String writefile = "d:" + File.separator + "writefile.properties";  
        String readxmlfile = "d:" + File.separator + "readxmlfile.xml";  
        String writexmlfile = "d:" + File.separator + "writexmlfile.xml";  
        String readtxtfile = "d:" + File.separator + "readtxtfile.txt";  
        String writetxtfile = "d:" + File.separator + "writetxtfile.txt";  
  
        readPropertiesFile(readfile); //读取properties文件  
        writePropertiesFile(writefile); //写properties文件  
        readPropertiesFileFromXML(readxmlfile); //读取XML文件  
        writePropertiesFileToXML(writexmlfile); //写XML文件  
        readPropertiesFile(readtxtfile); //读取txt文件  
        writePropertiesFile(writetxtfile); //写txt文件  
    }  
  
    //读取资源文件,并处理中文乱码  
    public static void readPropertiesFile(String filename)  
    {  
        Properties properties = new Properties();  
        try  
        {  
            InputStream inputStream = new FileInputStream(filename);  
            properties.load(inputStream);  
            inputStream.close(); //关闭流  
        }  
        catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
        String username = properties.getProperty("username");  
        String passsword = properties.getProperty("password");  
        String chinese = properties.getProperty("chinese");  
        try  
        {  
            chinese = new String(chinese.getBytes("ISO-8859-1"), "GBK"); // 处理中文乱码  
        }  
        catch (UnsupportedEncodingException e)  
        {  
            e.printStackTrace();  
        }  
        System.out.println(username);  
        System.out.println(passsword);  
        System.out.println(chinese);  
    }  
  
    //读取XML文件,并处理中文乱码  
    public static void readPropertiesFileFromXML(String filename)  
    {  
        Properties properties = new Properties();  
        try  
        {  
            InputStream inputStream = new FileInputStream(filename);  
            properties.loadFromXML(inputStream);  
            inputStream.close();  
        }  
        catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
        String username = properties.getProperty("username");  
        String passsword = properties.getProperty("password");  
        String chinese = properties.getProperty("chinese"); //XML中的中文不用处理乱码,正常显示  
        System.out.println(username);  
        System.out.println(passsword);  
        System.out.println(chinese);  
    }  
  
    //写资源文件,含中文  
    public static void writePropertiesFile(String filename)  
    {  
        Properties properties = new Properties();  
        try  
        {  
            OutputStream outputStream = new FileOutputStream(filename);  
            properties.setProperty("username", "myname");  
            properties.setProperty("password", "mypassword");  
            properties.setProperty("chinese", "中文");  
            properties.store(outputStream, "author: shixing_11@sina.com");  
            outputStream.close();  
        }  
        catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
    }  
  
    //写资源文件到XML文件,含中文    
    public static void writePropertiesFileToXML(String filename)  
    {  
        Properties properties = new Properties();  
        try  
        {  
            OutputStream outputStream = new FileOutputStream(filename);  
            properties.setProperty("username", "myname");  
            properties.setProperty("password", "mypassword");  
            properties.setProperty("chinese", "中文");  
            properties.storeToXML(outputStream, "author: shixing_11@sina.com");  
            outputStream.close();  
        }  
        catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
    }  
  
}  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值