- 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
- {
- publicstaticvoidmain(String[]args)
- {
- Stringreadfile="d:"+File.separator+"readfile.properties";
- Stringwritefile="d:"+File.separator+"writefile.properties";
- Stringreadxmlfile="d:"+File.separator+"readxmlfile.xml";
- Stringwritexmlfile="d:"+File.separator+"writexmlfile.xml";
- Stringreadtxtfile="d:"+File.separator+"readtxtfile.txt";
- Stringwritetxtfile="d:"+File.separator+"writetxtfile.txt";
- readPropertiesFile(readfile);//读取properties文件
- writePropertiesFile(writefile);//写properties文件
- readPropertiesFileFromXML(readxmlfile);//读取XML文件
- writePropertiesFileToXML(writexmlfile);//写XML文件
- readPropertiesFile(readtxtfile);//读取txt文件
- writePropertiesFile(writetxtfile);//写txt文件
- }
- //读取资源文件,并处理中文乱码
- publicstaticvoidreadPropertiesFile(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文件,并处理中文乱码
- publicstaticvoidreadPropertiesFileFromXML(Stringfilename)
- {
- Propertiesproperties=newProperties();
- try
- {
- InputStreaminputStream=newFileInputStream(filename);
- properties.loadFromXML(inputStream);
- inputStream.close();
- }
- catch(IOExceptione)
- {
- e.printStackTrace();
- }
- Stringusername=properties.getProperty("username");
- Stringpasssword=properties.getProperty("password");
- Stringchinese=properties.getProperty("chinese");//XML中的中文不用处理乱码,正常显示
- System.out.println(username);
- System.out.println(passsword);
- System.out.println(chinese);
- }
- //写资源文件,含中文
- publicstaticvoidwritePropertiesFile(Stringfilename)
- {
- Propertiesproperties=newProperties();
- try
- {
- OutputStreamoutputStream=newFileOutputStream(filename);
- properties.setProperty("username","myname");
- properties.setProperty("password","mypassword");
- properties.setProperty("chinese","中文");
- properties.store(outputStream,"author:shixing_11@sina.com");
- outputStream.close();
- }
- catch(IOExceptione)
- {
- e.printStackTrace();
- }
- }
- //写资源文件到XML文件,含中文
- publicstaticvoidwritePropertiesFileToXML(Stringfilename)
- {
- Propertiesproperties=newProperties();
- try
- {
- OutputStreamoutputStream=newFileOutputStream(filename);
- properties.setProperty("username","中文");
- properties.storeToXML(outputStream,"author:shixing_11@sina.com");
- outputStream.close();
- }
- catch(IOExceptione)
- {
- e.printStackTrace();
- }
- }
- }