properties类读取.properties 、简单XML文件、txt文件
发布时间:2020-12-15 22:29:06 所属栏目:百科 来源:网络整理
导读:Java中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XML文件 3. 不仅可以读写上述两类文件,还可以读写其它格式文件如txt等,只要符合key=value格式即可. 注意:资源文件中含有中文时的处理方法 1. 将中文字符通过工作
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接口,这样大家放心了吧。
运行本程序所需的资源文件,我是放在D盘根目录,如D:/readfile.properties
1. readfile.properties
2.writefile.properties
3.readxmlfile.xml <!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
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |