欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java--Proreties

程序员文章站 2022-05-03 22:22:53
Prorerties ......

prorerties

/* 
 * properties,内存与文件信息交互
 * 表示了一个持久的属性集
 * 
 * 构造方法:
 *  properties()
 * 
 * 
 */


 //简单使用  创建,添加,遍历,
    private static void method() {
        // 创建属性列表对象
        properties prop = new properties();
        //添加映射关系
        prop.put("czbk001","zhangs");
        prop.put("czbk002","lis");
        prop.put("czbk003","wangs");
        
        //遍历
        //获取所有的key,根据key获取value
        set<java.lang.object> keys = prop.keyset();
        for(java.lang.object key:keys)
        {
            java.lang.object value = prop.get(key);
            system.out.println(key+"="+value);
        }
        system.out.println("------");
        set<entry<java.lang.object, java.lang.object>> entrys = prop.entryset();
        for(entry<java.lang.object, java.lang.object> entry:entrys)
        {
            java.lang.object value = entry.getvalue();
            java.lang.object key = entry.getkey();
            system.out.println(key + "=" + value);
        }




//与io流的结合
//保存到文件(只能接收打印流)
private static void method2() throws filenotfoundexception {
    // void list(printwriter out)
    // void list(printstream out)
    //创建属性列表
    properties prop = new properties();
    //添加映射关系
    prop.setproperty("czbk001","zhangs");
    prop.setproperty("czbk002", "wangs");
    prop.setproperty("czbk003", "lis");
    
    //创建打印流对象
    printwriter out = new printwriter("d.txt");
    
    //void list(printwriter out)
    prop.list(out);
    //释放资源
    out.close();
}



//从文件读取到prop
private static void method3() throws filenotfoundexception, ioexception {
    // void load(reader reader)
    
    //创建属性列表对象
    properties prop = new properties();
    //创建一个输入流对象
    filereader fr = new filereader("d.txt");
    
    //void load(reader reader)
    prop.load(fr);
    
    //释放资源
    fr.close();
    
    system.out.println(prop);
}



//保存到文件(任意)
//void store(writer writer, string comments)    
private static void method4() throws ioexception {
    // 创建属性列表对象
    properties prop = new properties();
    // 添加映射关系
    prop.setproperty("czbk001","zhangs");
    prop.setproperty("czbk002", "wangs");
    prop.setproperty("czbk003", "lis");
    // 创建输出流对象
    filewriter fw = new filewriter("e.txt");
    
    //void store(writer writer, string comments)
    prop.store(fw, "comments message");
    
    //释放资源
    fw.close();
}



//编码问题
//如果写入文件时出现了乱码,可以尝试修改编码

outputstreamwriter osw = new outputstreamwriter(new fileoutputstream("b.txt"), "utf-8");
string s = "你好鸭";
osw.write(s);
osw.close();