Properties类(一)

- package apistudy;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.UnsupportedEncodingException;
- import java.util.Properties;
- public class PropertiesTest
- {
- public static void main(String[] args)
- {
- String readfile = "d:" + File.separator + "readfile.properties";
- String writefile = "d:" + File.separator + "writefile.properties";
- String readxmlfile = "d:" + File.separator + "readxmlfile.xml";
- String writexmlfile = "d:" + File.separator + "writexmlfile.xml";
- String readtxtfile = "d:" + File.separator + "readtxtfile.txt";
- String writetxtfile = "d:" + File.separator + "writetxtfile.txt";
- readPropertiesFile(readfile); //读取properties文件
- writePropertiesFile(writefile); //写properties文件
- readPropertiesFileFromXML(readxmlfile); //读取XML文件
- writePropertiesFileToXML(writexmlfile); //写XML文件
- readPropertiesFile(readtxtfile); //读取txt文件
- writePropertiesFile(writetxtfile); //写txt文件
- }
- //读取资源文件,并处理中文乱码
- public static void readPropertiesFile(String filename)
- {
- Properties properties = new Properties();
- try
- {
- InputStream inputStream = new FileInputStream(filename);
- properties.load(inputStream);
- inputStream.close(); //关闭流
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- String username = properties.getProperty("username");
- String passsword = properties.getProperty("password");
- String chinese = properties.getProperty("chinese");
- try
- {
- chinese = new String(chinese.getBytes("ISO-8859-1"), "GBK"); // 处理中文乱码
- }
- catch (UnsupportedEncodingException e)
- {
- e.printStackTrace();
- }
- System.out.println(username);
- System.out.println(passsword);
- System.out.println(chinese);
- }
- //读取XML文件,并处理中文乱码
- public static void readPropertiesFileFromXML(String filename)
- {
- Properties properties = new Properties();
- try
- {
- InputStream inputStream = new FileInputStream(filename);
- properties.loadFromXML(inputStream);
- inputStream.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- String username = properties.getProperty("username");
- String passsword = properties.getProperty("password");
- String chinese = properties.getProperty("chinese"); //XML中的中文不用处理乱码,正常显示
- System.out.println(username);
- System.out.println(passsword);
- System.out.println(chinese);
- }
- //写资源文件,含中文
- public static void writePropertiesFile(String filename)
- {
- Properties properties = new Properties();
- try
- {
- OutputStream outputStream = new FileOutputStream(filename);
- properties.setProperty("username", "myname");
- properties.setProperty("password", "mypassword");
- properties.setProperty("chinese", "中文");
- properties.store(outputStream, "author: shixing_11@sina.com");
- outputStream.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- //写资源文件到XML文件,含中文
- public static void writePropertiesFileToXML(String filename)
- {
- Properties properties = new Properties();
- try
- {
- OutputStream outputStream = new FileOutputStream(filename);
- properties.setProperty("username", "myname");
- properties.setProperty("password", "mypassword");
- properties.setProperty("chinese", "中文");
- properties.storeToXML(outputStream, "author: shixing_11@sina.com");
- outputStream.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
username=mynamepassword=mypasswordchinese=中文
#author: shixing_11@sina.com#Fri May 28 22:19:44 CST 2010password=mypasswordchinese=/u4E2D/u6587username=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>
<?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>
#author: shixing_11@sina.com#Fri May 28 22:25:16 CST 2010password=mypasswordchinese=/u4E2D/u6587username=myname
Properties类(一)的更多相关文章
- Java中Properties类知识的总结
一.Properties类与配置文件 注意:是一个Map集合,该集合中的键值对都是字符串.该集合通常用于对键值对形式的配置文件进行操作. 配置文件:将软件中可变的部分数据可以定义到一个文件中,方便以后 ...
- java编程中Properties类的具体作用和使用
如果不熟悉 java.util.Properties类,那么现在告诉您它是用来在一个文件中存储键-值对的,其中键和值是用等号分隔的.(如清单 1 所示).最近更新的java.util.Properti ...
- 【JAVA Properties类概述】
一.概述. 之前说过,该对象是和IO流相结合的技术,所以和IO流结合在一起来讲比较合适. public class Propertiesextends Hashtable<Object,Obje ...
- java 21 - 14 Properties类
类 Properties Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串. 注意:Properties是Has ...
- Java中Properties类的操作
知识学而不用,就等于没用,到真正用到的时候还得重新再学.最近在看几款开源模拟器的源码,里面涉及到了很多关于Properties类的引用,由于Java已经好久没用了,而这些模拟器大多用Java来写,外加 ...
- Properties类一些常用的用法
直接上代码: package test.properties; import java.io.File; import java.io.FileInputStream; import java.io. ...
- Java常用类之Properties类
1.特性 Properties类表示了一个持久的属性集,可保存在流中或从流中加载,实现内存和文件的交互.Properties继承了Hashtable<Object,Object>类,可以使 ...
- Java的Properties类和读取.properties文件
一..properties文件的作用 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必 ...
- Java中Properties类的操作配置文件
知识学而不用,就等于没用,到真正用到的时 候还得重新再学.最近在看几款开源模拟器的源码,里面涉及到了很多关于Properties类的引用,由于Java已经好久没用了,而这些模拟器大多用 Java来写, ...
- Properties类与读取properties文件
Properties类 在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用"#"来注释. 这个类的几个常 ...
随机推荐
- 经验分享:Essay写作遇到困难请你这样做
很多留学生在essay写作中可能会遇到很多困难,要么是essay写作内容出现问题,又或者是对于essay写作格式的不了解,导致自己无法顺利完成essay.今天小编就收集了几位留学生的写作经验分享,希望 ...
- C语言编程实现SYN-Flood(Dos)攻击
## 实验环境为了方便,直接在win10 VS2013Ultimate实现(攻击机),靶机为同一局域网的另外一台主机或外网服务器. ## 实验依赖基于WinPcap实现,需要安装WinPcap4. ...
- POJ 2186:Popular Cows Tarjan模板题
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25945 Accepted: 10612 De ...
- 洛谷 P1018乘积最大
题目描述 今年是国际数学联盟确定的“20002000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰9090周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一个好朋友 ...
- 利用zed相机为rtabmap_ros录制rosbag包及其使用
1,录制rosbag包 rosbag record /zed_node/rgb/image_rect_color /zed_node/rgb/camera_info /zed_node/depth/d ...
- HTML5 可缩放矢量图形(2)—SVG基础
参考文档——权威 SVG常识 渲染顺序——后来居上:越后面的元素越可见 单位——可以指定,也可以不指定,默认px,其他:em.%.cm.mm... SVG画布——绘制图像的区域,无限大 SVG视窗—— ...
- nodejs(14)express获取url中的参数
问号传参获取参数 获取 http://127.0.0.1:3001/user?id=10&name=zs 中的查询参数: 直接使用 req.query 获取参数即可: 注意:URL 地址栏中通 ...
- A - Shortest path of the king (棋盘)
The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose heart, becaus ...
- Lyft、Uber、滴滴涉足汽车租赁领域,能打破既有汽车所有权模式吗?
自共享经济出现之后,众多相关项目遍地开花.这些共享经济项目对于人们来说,最直观的感受就是实惠.性价比高.方便.不过抛开这些使用层面的优点来看的话,共享经济项目最大的特色或许就是改变了事物的所有权.一件 ...
- 动态类型识别&动态创建
以下大部分内容摘自<windows程序设计 第2版> 王艳平 张铮 编著 动态类型识别:在程序运行过程中,辨别对象是否属于特定类的技术. 应用举例:函数辨别参数类型.需要针对对象的类编写特 ...