IO流-Properties

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值