Java基础 笔记(七)

Properties:
Hashtable的子类。属于集合类。存储属性累心公的键值对, 键和值默认都是String 是集合中能够和流结合使用的一个集合类
Properties pro = new Properties();
pro.setProperty(“name”, “Tom”);
pro.setProperty(“age”, “19”);
pro.setProperty(“sex”, “boy”);
System.out.println(pro);

    // 获得全部属性名的集合
    Set<String> names = pro.stringPropertyNames();
    Iterator<String> it = names.iterator();
    while (it.hasNext()) {
        String key = it.next();
        String value = pro.getProperty(key);
        System.out.println(key + "----" + value);
    }

与文件流结合使用:
通过Properties类载入.properties文件。并读取信息
1 流 2 创建Properties对象,载入属性文件 3 通过Properties对象的方法,将信息读取出来
// 写入properties文件
static void writeProperties(File file, Map


数据流:专门用来写基本类型数据,读和写的顺序严格相应
DataInputStream(InputStream )
readInt()
readDouble()
readUTF()

DataOutputStream(OutputStream)
writeInt()
writeDouble()
writeUTF()
flush()

// 创建一个数据流
DataOutputStream os = new DataOutputStream(new FileOutputStream(
“src/data.txt”));
os.writeDouble(12.2);
os.writeInt(10);
os.writeUTF(“好好学习”);
os.flush();

    // 读
    DataInputStream is = new DataInputStream(new FileInputStream(
            "src/data.txt"));
    System.out.println(is.readDouble());
    System.out.println(is.readInt());
    System.out.println(is.readUTF());

posted on 2017-07-24 20:44  wgwyanfs  阅读(126)  评论(0)    收藏  举报

导航