IO流-Properties
来源:互联网 发布:java 读取压缩包文件 编辑:程序博客网 时间:2024/06/06 13:11
Properties
1.是一个属性集合类,是一个可以和IO流相结合使用的集合类
2.可以保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
3.一般使用其构造方法
4.Hashtable的子类,说明是一个Map集合
5.没有泛型类
public class PropertiesDemo { public static void main(String[] args) { //作为Map集合的使用 Properties prop = new Properties(); //添加元素 默认是Object类型 在下方讲解为何是String prop.put("Helllo","world"); prop.put("xixi","haha"); prop.put("nihao","hehe"); //遍历集合 Set<Object> set = prop.ketSet(); for(Object key : set) { Obect value = prop.get(key); System.out.println(key+"------"+value); } }}
特殊功能的使用:
1.作为集合来说,添加功能,public void setProperty(String key,String value) //注意String类型
2.public String getProperty(String key); 获取元素
3.public Set<String> stringPropertyNames(); 获取所有键的集合
public class PropertiesDemo2 { public static void main(String[] args) { //创建集合对象 Properties prop = new Properties(); prop.setProperty("张三","30"); prop.setProperty("李四","40"); prop.setProperty("王五","50"); //public Set<String> stringPropertyNames(); 获取所有的键的集合 Set<String> set = prop.stringPropertyNames(); fro(String key : set) { String value = prop.getProperty(key); System.out.println(key+"------"+value); } }}
字符串类型讲解
class Hashtable<K,V> { public V put(K key,V value) { ... }}
//Properties继承自Hashtable类,重写父类的方法,但是在子类(Properties)中新写了一个方法,在返回值处调用了父类的 put方法,如如此何成了String类型
class Properties extends Hashtable { public V setProperty(String key,String value) { return put(key,value); }}
Properties和IO流的结合
1.public void load(Reader reader); 把文件中的数据读取到集合(Properties)中
2.public void store(Writer writer,String comments); 把集合(Properties)中的数据存储到文件中
load方法读取文件中的数据必须是键值形式
Reader r = new FileReader("prop.txt");
prop.load(r);
r.close;
需要抛异常
store保存数据
//创建集合对象
Properties prop = new Properties(); prop.setProperty("张三","30"); prop.setProperty("李四","40"); prop.setProperty("王五","50");
//有了元素,下一步存储到文件中,创建对象
<span style="white-space:pre"></span>Writer w = new FileWriter("name.txt"); prop.store(w,null); //null处可以添加注释如 “这是注释” w.close;
有一个文本文件(User.txt),判断程序中时候有“lisi”这个键,有就改变其值为“100”
分析:
a:把文件中的数据加载到集合中
b:遍历集合,获取得到每一个键
c:判断键是否有位"lisi"的,如果有就修改其值为“100”
d:把集合中的数据重新存储到文件中
public class PropertiesTest { public static void main(String[] args) throws IOException { //把文件中的数据加载到集合中 Properties prop = new Properties(); Reader r = new FileReader("user.txt"); prop.load(r); r.close(); //遍历集合 Set<String> set = prop.stringPropertyNames(); for(String key : set) { if("lisi".equals(key)) { prop.setProperty(key,"100"); break; } } //把集合中的数据重新存储到文件中 Writer w = new FileWriter("user.txt"); prop.store(w,null); w.close(); }}
JDK7要了解的新IO类
1.path:与平台无关的路径
2.paths:包含了返回Path的静态方法
public static Path get(Uri uri); 根据给定的URI来确定文件的路径。
3.Files:操作文件的工具类。
public static long copy(Path source,OutputStram out); 复制文件
public static Path write(Path path,Iterable<? extends CharSequence> lines, Charset cs,OpenOption... options); 把集合的数据写到文件。
例子:
//复制文件 Files.copy(Paths.get("Demo.java"),newFileOutputStream("Copy.java")); //把集合中的数据写到文件 Files.write(Parns.get("array.txt"),array,Charset.forName("GBK"));
0 0
- IO流(Properties练习)
- java IO流(properties)
- IO流-Properties
- IO流(Properties简述)-(Properties存取)-(Properties存取配置文件)
- java--IO流-Properties类
- io流对Properties操作
- Java IO流,Properties集合
- 【IO流】Properties操作配置文件
- Java IO流 、 Properties 、 枚举
- IO流--Properties类,打印流
- Java编码问题(IO流/JSP/Properties)
- IO流-----File类与Properties集合
- 黑马程序员-day20-IO流(Properties)
- java学习--IO流(3)-Properties
- Java基础--IO流之Properties
- java IO流 File类,Properties--20
- Java基础知识IO流(Properties)
- JAVASE基础-day22(IO(其他流)&Properties)
- labcode
- c语言 程序 快速排序
- sqlplus执行SQL文件
- hdu 最短路径
- 《用tcp/ip进行网际互联》第6章 网际协议:无连接数据报交付
- IO流-Properties
- random forest
- Java NIO
- Codeforces-Round-#356-(Div.-2)-Bear-and-Finding-Criminals
- CPU寄存器
- win10 uefi Ubuntu14.04双系统
- MyEclipse 10 启动配置优化
- property和constructor-arg设值注入
- C语言 程序 cantor表