《Properties XML文件写出》

前端之家收集整理的这篇文章主要介绍了《Properties XML文件写出》前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  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. publicstaticvoidmain(String[]args)
  13. {
  14. Stringreadfile="d:"+File.separator+"readfile.properties";
  15. Stringwritefile="d:"+File.separator+"writefile.properties";
  16. Stringreadxmlfile="d:"+File.separator+"readxmlfile.xml";
  17. Stringwritexmlfile="d:"+File.separator+"writexmlfile.xml";
  18. Stringreadtxtfile="d:"+File.separator+"readtxtfile.txt";
  19. Stringwritetxtfile="d:"+File.separator+"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. publicstaticvoidreadPropertiesFile(Stringfilename)
  29. {
  30. Propertiesproperties=newProperties();
  31. try
  32. {
  33. InputStreaminputStream=newFileInputStream(filename);
  34. properties.load(inputStream);
  35. inputStream.close();//关闭
  36. }
  37. catch(IOExceptione)
  38. {
  39. e.printStackTrace();
  40. }
  41. Stringusername=properties.getProperty("username");
  42. Stringpasssword=properties.getProperty("password");
  43. Stringchinese=properties.getProperty("chinese");
  44. try
  45. {
  46. chinese=newString(chinese.getBytes("ISO-8859-1"),"GBK");//处理中文乱码
  47. }
  48. catch(UnsupportedEncodingExceptione)
  49. {
  50. e.printStackTrace();
  51. }
  52. System.out.println(username);
  53. System.out.println(passsword);
  54. System.out.println(chinese);
  55. }
  56. //读取XML文件,并处理中文乱码
  57. publicstaticvoidreadPropertiesFileFromXML(Stringfilename)
  58. {
  59. Propertiesproperties=newProperties();
  60. try
  61. {
  62. InputStreaminputStream=newFileInputStream(filename);
  63. properties.loadFromXML(inputStream);
  64. inputStream.close();
  65. }
  66. catch(IOExceptione)
  67. {
  68. e.printStackTrace();
  69. }
  70. Stringusername=properties.getProperty("username");
  71. Stringpasssword=properties.getProperty("password");
  72. Stringchinese=properties.getProperty("chinese");//XML中的中文不用处理乱码,正常显示
  73. System.out.println(username);
  74. System.out.println(passsword);
  75. System.out.println(chinese);
  76. }
  77. //写资源文件,含中文
  78. publicstaticvoidwritePropertiesFile(Stringfilename)
  79. {
  80. Propertiesproperties=newProperties();
  81. try
  82. {
  83. OutputStreamoutputStream=newFileOutputStream(filename);
  84. properties.setProperty("username","myname");
  85. properties.setProperty("password","mypassword");
  86. properties.setProperty("chinese","中文");
  87. properties.store(outputStream,"author:shixing_11@sina.com");
  88. outputStream.close();
  89. }
  90. catch(IOExceptione)
  91. {
  92. e.printStackTrace();
  93. }
  94. }
  95. //写资源文件到XML文件,含中文
  96. publicstaticvoidwritePropertiesFileToXML(Stringfilename)
  97. {
  98. Propertiesproperties=newProperties();
  99. try
  100. {
  101. OutputStreamoutputStream=newFileOutputStream(filename);
  102. properties.setProperty("username","中文");
  103. properties.storeToXML(outputStream,"author:shixing_11@sina.com");
  104. outputStream.close();
  105. }
  106. catch(IOExceptione)
  107. {
  108. e.printStackTrace();
  109. }
  110. }
  111. }

猜你在找的XML相关文章