歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java讀寫Properties文件

Java讀寫Properties文件

日期:2017/3/1 11:08:31   编辑:Linux編程

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接口,這樣大家放心了吧。

print?

  1. package apistudy;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.io.UnsupportedEncodingException;
  9. import java.util.Properties;
  10. public class PropertiesTest
  11. {
  12. public static void main(String[] args)
  13. {
  14. String readfile = "d:" + File.separator + "readfile.properties";
  15. String writefile = "d:" + File.separator + "writefile.properties";
  16. String readxmlfile = "d:" + File.separator + "readxmlfile.xml";
  17. String writexmlfile = "d:" + File.separator + "writexmlfile.xml";
  18. String readtxtfile = "d:" + File.separator + "readtxtfile.txt";
  19. String writetxtfile = "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. public static void readPropertiesFile(String filename)
  29. {
  30. Properties properties = new Properties();
  31. try
  32. {
  33. InputStream inputStream = new FileInputStream(filename);
  34. properties.load(inputStream);
  35. inputStream.close(); //關閉流
  36. }
  37. catch (IOException e)
  38. {
  39. e.printStackTrace();
  40. }
  41. String username = properties.getProperty("username");
  42. String passsword = properties.getProperty("password");
  43. String chinese = properties.getProperty("chinese");
  44. try
  45. {
  46. chinese = new String(chinese.getBytes("ISO-8859-1"), "GBK"); // 處理中文亂碼
  47. }
  48. catch (UnsupportedEncodingException e)
  49. {
  50. e.printStackTrace();
  51. }
  52. System.out.println(username);
  53. System.out.println(passsword);
  54. System.out.println(chinese);
  55. }
  56. //讀取XML文件,並處理中文亂碼
  57. public static void readPropertiesFileFromXML(String filename)
  58. {
  59. Properties properties = new Properties();
  60. try
  61. {
  62. InputStream inputStream = new FileInputStream(filename);
  63. properties.loadFromXML(inputStream);
  64. inputStream.close();
  65. }
  66. catch (IOException e)
  67. {
  68. e.printStackTrace();
  69. }
  70. String username = properties.getProperty("username");
  71. String passsword = properties.getProperty("password");
  72. String chinese = properties.getProperty("chinese"); //XML中的中文不用處理亂碼,正常顯示
  73. System.out.println(username);
  74. System.out.println(passsword);
  75. System.out.println(chinese);
  76. }
  77. //寫資源文件,含中文
  78. public static void writePropertiesFile(String filename)
  79. {
  80. Properties properties = new Properties();
  81. try
  82. {
  83. OutputStream outputStream = new FileOutputStream(filename);
  84. properties.setProperty("username", "myname");
  85. properties.setProperty("password", "mypassword");
  86. properties.setProperty("chinese", "中文");
  87. properties.store(outputStream, "author: [email protected]");
  88. outputStream.close();
  89. }
  90. catch (IOException e)
  91. {
  92. e.printStackTrace();
  93. }
  94. }
  95. //寫資源文件到XML文件,含中文
  96. public static void writePropertiesFileToXML(String filename)
  97. {
  98. Properties properties = new Properties();
  99. try
  100. {
  101. OutputStream outputStream = new FileOutputStream(filename);
  102. properties.setProperty("username", "myname");
  103. properties.setProperty("password", "mypassword");
  104. properties.setProperty("chinese", "中文");
  105. properties.storeToXML(outputStream, "author: [email protected]");
  106. outputStream.close();
  107. }
  108. catch (IOException e)
  109. {
  110. e.printStackTrace();
  111. }
  112. }
  113. }
Copyright © Linux教程網 All Rights Reserved