流的学习

转换流

转换流可以查指定的编码表进行读写

  • OutputStreamWriter(字符流转向字节流的桥梁)
    将程序中的字符按照创建转换流时给出的编码格式去查对应的码表,使用字节流 将文件写入

  • InputStreamReader(字节流转向字符流的桥梁)
    先按字节读,读完了用转换流去查对应的表,最终以字符的形式读到程序中

使用实例

//  转换流写个UTF-8格式的文件
    public static void getWriterUTF8() throws IOException{
        //  文件字节流
        FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Test/hong.txt");
        //  创建转换流
        //  使用操作平台的默认编码格式写文件
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        //  写
        osw.write("无耻老贼");
        //  关闭资源
        //  1.多个流嵌套 只需要关闭最外层的流
        //  2.自己创建出来的流 自己关
        //  3.从系统中获取的流 不用自己关闭
        osw.close();
    }

    //  转换流写个GBK格式的文件
    public static void getWriterGBK() throws IOException{
            //  文件字节流
            FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Test/jie.txt");
            //  编码格式 大小写都行
            OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
            osw.write("无耻老贼");
            osw.close();
    }

    public static void getReadUtf8() throws IOException {
        //  默认编码格式读(UTF-8)
        FileInputStream  fis = new FileInputStream("/Users/lanou/Desktop/Test/jie.txt");
        InputStreamReader isr = new InputStreamReader(fis);
        char [] c = new char[1024];
        int len = 0;
        while ((len = isr.read(c)) != -1) {
            System.out.println(new String(c, 0, len));
        }
        isr.close();
    }
    //  按GBK码读
    public static void getReadGBK() throws IOException {
        FileInputStream  fis = new FileInputStream("/Users/lanou/Desktop/Test/jie.txt");
        InputStreamReader isr = new InputStreamReader(fis,"GBK");
        char [] c = new char[1024];
        int len = 0;
        while ((len = isr.read(c)) != -1) {
            System.out.println(new String(c, 0, len));
        }
        isr.close();    
    }
}

缓冲流(高效流)

关键字:BufferedOutputStream
默认缓冲区大小:8k
使用实例


        // 缓冲流读取
        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/Test/liu.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        byte []b = new byte[1024];
        int len = 0;
        while ((len = bis.read(b)) != -1) {
            System.out.println(new String(b, 0, len));
        }
        bis.close();
        // 缓冲流写入
        FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Test/liu.txt");
        BufferedOutputStream bos =new BufferedOutputStream(fos);
        //  写入
        bos.write("wanglong".getBytes());
        //  关闭资源
        bos.close();

高效字符流

  • BufferedWriter:高效写入
  • BufferedReader:高效读取

使用实例

// 读文件
        FileReader fr = new FileReader("/Users/lanou/Desktop/ss.txt");
        BufferedReader br = new BufferedReader(fr);
        String str = "";
        //  读取一行字符串方法 读到末尾 返回null
        while ((str = br.readLine()) != null) {
            // 注意:该方法不能读取到换行符
            System.out.print(str);
        }
        br.close();
// 写文件
        FileWriter fw = new FileWriter("/Users/lanou/Desktop/ss.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        // 写
        bw.write("白日依山尽");
        // 换行
        bw.newLine();
        bw.write("黄河入海流");
        bw.newLine();
        bw.flush();
        bw.close();
// 复制文件
        FileReader fr = new FileReader("/Users/lanou/Desktop/ss.txt");
        BufferedReader br = new BufferedReader(fr);
        FileWriter fw = new FileWriter("/Users/lanou/Desktop/Test/ss.txt");
        BufferedWriter bW = new BufferedWriter(fw);
        String str = "";
        while ((str = br.readLine()) != null) {
            bW.write(str);
            // 注意:复制文件时 需要加入换行
            // 复制文件会多一个换行
            bW.newLine();
            bW.flush();
        }
        br.close();
        bW.close();

Properties集合(双列集合 是Hashtable的子类)

  • 该集合 是唯一一个能和IO流有关系的集合
  • 注意:一般该集合只存储 字符串类型的键值对
  • load:将文件直接读入到集合中
  • store:将集合中的键值对直接写入到文件中

  • 读取文件的格式key=value

  • 注意:
  • 1.等号前后别加空格
  • 2.一般该文件使用.properties为后缀(起标识作用)
  • 3.#为注释

        // 将文件直接读入集合中
        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/ss.properties");
        // 创建集合
        Properties properties = new Properties();
        // 加载文件
        properties.load(fis);
        Set<Object> keySet = properties.keySet();
        // 迭代器遍历
        Enumeration enumeration = properties.propertyNames();
        while (enumeration.hasMoreElements()) {
            // 取出每一个元素
            String key = (String)enumeration.nextElement();
            System.out.println(key + "=" + properties.getProperty(key));
        }     
        fis.close();
        // 将集合写入文件
        FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/ss.properties");
        Properties properties = new Properties();
        // 保存键值对
        properties.setProperty("name", "dongdong");
        properties.setProperty("age", "100");
        properties.setProperty("gander", "nan");
        // 写入到文件中
        // 参数2:传入的注释(推荐英文)
        properties.store(fos, "star");
        fos.close();

序列化流(反序列化流 对象流)

关键词:
ObjectOutputStream
ObjectInputStream
使用方法,首先创建一个类

public class Person implements Serializable{
    /**
     * 解决序列化ID冲突的方案
     * 将序列化号写死
     * 只要你类中有序列化号 系统将不会为你重新生成了
     */
    private static final long serialVersionUID = -7627711003665276994L;
    /*
     * 阻止对象的成员变量 进行序列化
     * 1.静态变量 不会被写入文件
     * 2.使用关键字 transient(瞬态) 标识成员变量
     */
    private String name; 
    private int age;
    public Person() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }

}

        // 将对象写入文件
        FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/121.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        // 写入
        // NotSerializableException 
        // 要保存的对象的类没有实现序列化接口
        // Serializable 序列化接口是个标示性接口
        // 标识这个类的对象 可以进行序列化
        oos.writeObject(new Person("dongdong", 10));
        // 关闭流
        oos.close();

        // 读取文件
        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/121.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        // ClassNotFoundException 找不到这个类
        // 将文件中对象 进行反序列化 需要依赖
        // 存入对象的.class文件
        Object object = ois.readObject();
        System.out.println(object);
        ois.close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值