properties类读取.properties 、简单XML文件、txt文件

前端之家收集整理的这篇文章主要介绍了properties类读取.properties 、简单XML文件、txt文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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存储的任何格式文件,究竟有什么神奇,猫一眼类结构,
原来它继承了Hashtable并实现了Map接口,这样大家放心了吧。

  1. packageapistudy;
  2. importjava.io.File;
  3. importjava.io.FileInputStream;
  4. importjava.io.FileOutputStream;
  5. importjava.io.IOException;
  6. importjava.io.InputStream;
  7. importjava.io.OutputStream;
  8. importjava.io.UnsupportedEncodingException;
  9. importjava.util.Properties;
  10. publicclassPropertiesTest
  11. {
  12. staticvoidmain(String[]args)
  13. {
  14. Stringreadfile="d:"+File.separator+"readfile.properties";
  15. Stringwritefile="writefile.properties";
  16. Stringreadxmlfile="readxmlfile.xml";
  17. Stringwritexmlfile="writexmlfile.xml";
  18. Stringreadtxtfile="readtxtfile.txt";
  19. Stringwritetxtfile="writetxtfile.txt";
  20. readPropertiesFile(readfile);//读取properties文件
  21. writePropertiesFile(writefile);//写properties文件
  22. readPropertiesFileFromXML(readxmlfile);//读取XML文件
  23. writePropertiesFileToXML(writexmlfile);//写XML文件
  24. readPropertiesFile(readtxtfile);//读取txt文件
  25. writePropertiesFile(writetxtfile);//写txt文件
  26. }
  27. //读取资源文件,并处理中文乱码
  28. voidreadPropertiesFile(Stringfilename)
  29. Propertiesproperties=newProperties();
  30. try
  31. InputStreaminputStream=newFileInputStream(filename);
  32. properties.load(inputStream);
  33. inputStream.close();//关闭
  34. }
  35. catch(IOExceptione)
  36. e.printStackTrace();
  37. Stringusername=properties.getProperty("username");
  38. Stringpasssword=properties.getProperty("password");
  39. Stringchinese=properties.getProperty("chinese");
  40. try
  41. chinese=newString(chinese.getBytes("ISO-8859-1"),"GBK");//处理中文乱码
  42. catch(UnsupportedEncodingExceptione)
  43. e.printStackTrace();
  44. System.out.println(username);
  45. System.out.println(passsword);
  46. System.out.println(chinese);
  47. //读取XML文件,255); font-weight:bold; background-color:inherit">voidreadPropertiesFileFromXML(Stringfilename)
  48. properties.loadFromXML(inputStream);
  49. inputStream.close();
  50. "chinese");//XML中的中文不用处理乱码,正常显示
  51. //写资源文件,含中文
  52. voidwritePropertiesFile(Stringfilename)
  53. OutputStreamoutputStream=newFileOutputStream(filename);
  54. properties.setProperty("username",0); background-color:inherit">"myname");
  55. "password",0); background-color:inherit">"mypassword");
  56. "chinese",0); background-color:inherit">"中文");
  57. properties.store(outputStream,"author:shixing_11@sina.com");
  58. outputStream.close();
  59. catch(IOExceptione)
  60. //写资源文件到XML文件,含中文
  61. voidwritePropertiesFileToXML(Stringfilename)
  62. newProperties();
  63. newFileOutputStream(filename);
  64. "myname");
  65. "mypassword");
  66. "中文");
  67. properties.storeToXML(outputStream,0); background-color:inherit">"author:shixing_11@sina.com");
  68. outputStream.close();
  69. }

运行本程序所需的资源文件,我是放在D盘根目录,如D:/readfile.properties
1. readfile.properties
username=myname
password=mypassword
chinese=中文
2.writefile.properties
#author: shixing_11@sina.com
#Fri May 28 22:19:44 CST 2010
chinese=/u4E2D/u6587
username=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>
5. readtxtfile.txt
username=myname
password=mypassword
chinese=中文
6.writetxtfile.txt
#author: shixing_11@sina.com
#Fri May 28 22:25:16 CST 2010
password=mypassword
chinese=/u4E2D/u6587
username=myname
原文链接:https://www.f2er.com/xml/298139.html

猜你在找的XML相关文章