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. 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: shixing_11@sina.com");
  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: shixing_11@sina.com");
  106. outputStream.close();
  107. }
  108. catch (IOException e)
  109. {
  110. e.printStackTrace();
  111. }
  112. }
  113. }
运行本程序所需的资源文件,我是放在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
password=mypassword
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"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>author: shixing_11@sina.com</comment>
<entry key="password">mypassword</entry>
<entry key="chinese">中文</entry>
<entry key="username">myname</entry>
</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

Properties类(一)的更多相关文章

  1. Java中Properties类知识的总结

    一.Properties类与配置文件 注意:是一个Map集合,该集合中的键值对都是字符串.该集合通常用于对键值对形式的配置文件进行操作. 配置文件:将软件中可变的部分数据可以定义到一个文件中,方便以后 ...

  2. java编程中Properties类的具体作用和使用

    如果不熟悉 java.util.Properties类,那么现在告诉您它是用来在一个文件中存储键-值对的,其中键和值是用等号分隔的.(如清单 1 所示).最近更新的java.util.Properti ...

  3. 【JAVA Properties类概述】

    一.概述. 之前说过,该对象是和IO流相结合的技术,所以和IO流结合在一起来讲比较合适. public class Propertiesextends Hashtable<Object,Obje ...

  4. java 21 - 14 Properties类

    类 Properties Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串. 注意:Properties是Has ...

  5. Java中Properties类的操作

    知识学而不用,就等于没用,到真正用到的时候还得重新再学.最近在看几款开源模拟器的源码,里面涉及到了很多关于Properties类的引用,由于Java已经好久没用了,而这些模拟器大多用Java来写,外加 ...

  6. Properties类一些常用的用法

    直接上代码: package test.properties; import java.io.File; import java.io.FileInputStream; import java.io. ...

  7. Java常用类之Properties类

    1.特性 Properties类表示了一个持久的属性集,可保存在流中或从流中加载,实现内存和文件的交互.Properties继承了Hashtable<Object,Object>类,可以使 ...

  8. Java的Properties类和读取.properties文件

    一..properties文件的作用 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必 ...

  9. Java中Properties类的操作配置文件

    知识学而不用,就等于没用,到真正用到的时 候还得重新再学.最近在看几款开源模拟器的源码,里面涉及到了很多关于Properties类的引用,由于Java已经好久没用了,而这些模拟器大多用 Java来写, ...

  10. Properties类与读取properties文件

    Properties类 在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用"#"来注释. 这个类的几个常 ...

随机推荐

  1. 编程入门-Eclipse基本使用

    编程入门-Eclipse基本使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.设置Eclipse的基本参数 1>.修改Eclipse默认的文件编码为"utf- ...

  2. c++ opencv 入门

    //类型定义 CvPoint point(2, 3); CvPoint2D32f point1(2, 3); CvPoint3D32f point2(2, 3); CvSize size(2, 3); ...

  3. Egret Engine 2D - 显示对象

        alpha:透明度 width:宽度 height:高度 rotation:旋转角度 scaleX:横向缩放 scaleY:纵向缩放 skewX:横向斜切 skewY:纵向斜切 visible ...

  4. PHP ~ 通过程序删除图片,同时删除数据库中的图片数据 和 图片文件

    删除单张图片 <?php         require_once '../../conn.php';              //连接数据库         $ID = $_GET['ID' ...

  5. 一条命令解决:No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android

    1.找到目录D:\android\Sdk\ndk-bundle\toolchains.(根据自己的安装路径找到) 2.该路径下打开终端执行ln -sf aarch64-linux-android-4. ...

  6. React + umi +antd+antv/g6 实现力图

    官方示例效果:http://antv.alipay.com/zh-cn/g6/2.x/demo/net/2017-link-data.html 改编效果: 实现步骤: 环境:nodejs.yarn/n ...

  7. Redis: Reducing Memory Usage

    High Level Tips for Redis Most of Stream-Framework's users start out with Redis and eventually move ...

  8. jobs|ps|杀死nohup

    方法1:如果没有退出客户端界面,可以先通过 “jobs” 命令查看程序是否在运行,此时只有序号没有PID号:输入命令 “jobs -l” 会显示程序的PID号,然后通过 “kill -9 PID”杀死 ...

  9. sed使用案例

    简介: sed是一种流编辑器,它是文本处理中非常重要的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用 ...

  10. ZOJ 2301/HDU 1199 线段树+离散化

    给这个题目跪了两天了,想吐简直 发现自己离散化没学好 包括前一个离散化的题目,实际上是错了,我看了sha崽的博客后才知道,POJ那题简直数据弱爆了,本来随便一组就能让我WA掉的,原因在于离散化的时候, ...