1. 读写Properties文件
2. 读写XML文件
2. 直接调用 new String(youChineseString.getBytes("ISO-8859-1"),"GBK");
Properties prop = null;
1. prop = Thread.currentThread().getContextClassLoader().getResourceAsStream("filename");
2. prop =this.getClass().getClassLoader().getResourceAsStream("filename");
Properties能读取以key,value存储的任何格式文件,究竟有什么神奇,猫一眼类结构,
原来它继承了Hashtable并实现了Map接口,这样大家放心了吧。
- packageapistudy;
- importjava.io.File;
- importjava.io.FileInputStream;
- importjava.io.FileOutputStream;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjava.io.OutputStream;
- importjava.io.UnsupportedEncodingException;
- importjava.util.Properties;
- publicclassPropertiesTest
- {
- staticvoidmain(String[]args)
- {
- Stringreadfile="d:"+File.separator+"readfile.properties";
- Stringwritefile="writefile.properties";
- Stringreadxmlfile="readxmlfile.xml";
- Stringwritexmlfile="writexmlfile.xml";
- Stringreadtxtfile="readtxtfile.txt";
- Stringwritetxtfile="writetxtfile.txt";
- readPropertiesFile(readfile);//读取properties文件
- writePropertiesFile(writefile);//写properties文件
- readPropertiesFileFromXML(readxmlfile);//读取XML文件
- writePropertiesFileToXML(writexmlfile);//写XML文件
- readPropertiesFile(readtxtfile);//读取txt文件
- writePropertiesFile(writetxtfile);//写txt文件
- }
- //读取资源文件,并处理中文乱码
- voidreadPropertiesFile(Stringfilename)
- Propertiesproperties=newProperties();
- try
- InputStreaminputStream=newFileInputStream(filename);
- properties.load(inputStream);
- inputStream.close();//关闭流
- }
- catch(IOExceptione)
- e.printStackTrace();
- Stringusername=properties.getProperty("username");
- Stringpasssword=properties.getProperty("password");
- Stringchinese=properties.getProperty("chinese");
- try
- chinese=newString(chinese.getBytes("ISO-8859-1"),"GBK");//处理中文乱码
- catch(UnsupportedEncodingExceptione)
- e.printStackTrace();
- System.out.println(username);
- System.out.println(passsword);
- System.out.println(chinese);
- //读取XML文件,255); font-weight:bold; background-color:inherit">voidreadPropertiesFileFromXML(Stringfilename)
- properties.loadFromXML(inputStream);
- inputStream.close();
- "chinese");//XML中的中文不用处理乱码,正常显示
- //写资源文件,含中文
- voidwritePropertiesFile(Stringfilename)
- OutputStreamoutputStream=newFileOutputStream(filename);
- properties.setProperty("username",0); background-color:inherit">"myname");
- "password",0); background-color:inherit">"mypassword");
- "chinese",0); background-color:inherit">"中文");
- properties.store(outputStream,"author:shixing_11@sina.com");
- outputStream.close();
- catch(IOExceptione)
- //写资源文件到XML文件,含中文
- voidwritePropertiesFileToXML(Stringfilename)
- newProperties();
- newFileOutputStream(filename);
- "myname");
- "mypassword");
- "中文");
- properties.storeToXML(outputStream,0); background-color:inherit">"author:shixing_11@sina.com");
- outputStream.close();
- }
运行本程序所需的资源文件,我是放在D盘根目录,如D:/readfile.properties
1. readfile.properties
username=mynamepassword=mypasswordchinese=中文
2.writefile.properties
#author: shixing_11@sina.com#Fri May 28 22:19:44 CST 2010chinese=/u4E2D/u6587username=myname
3.readxmlfile.xml
<?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>
4.writexmlfile.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?><comment>author: shixing_11@sina.com</comment></properties>
#author: shixing_11@sina.com#Fri May 28 22:25:16 CST 2010password=mypasswordchinese=/u4E2D/u6587username=myname