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(); } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。