Gorilla带您学java之流之深入

转换流

转换流呢是可以查指定编码表进行读写的流。
OutputStreamWriter(字符流转向字节流的桥梁)
InputStreamReader(字节流转向字符流的桥梁)
具体操作如下:

    // 转换流写个UTF-8格式的文件
    public static void getWriterUTF8() throws IOException {

        // 文件字节流
        FileOutputStream fos = new FileOutputStream("xx/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("xx/Desktop/Test/jie.txt");
        // 创建转换流 编码格式 大小写都行
        OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
        // 写
        osw.write("无耻老贼");
        // 关闭资源 1.多个流嵌套 只需要关闭最外层的流 2.自己创建出来的流 自己关 3.从系统中获取的流不用管
        osw.close();
    }
    // 读UTF-8
    public static void getReaderUTF8() throws IOException{

        FileInputStream fis = new FileInputStream("xx/Desktop/Test/hong.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 getReaderGBK() throws IOException{
        FileInputStream fis = new FileInputStream("xx/Desktop/Test/hong.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 8192
具体操作:

        // 创建一个缓冲流
        FileOutputStream fos = new FileOutputStream("xx/Desktop/Test/liu.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        // 写入
        bos.write("xx".getBytes());

        bos.close();

        // 缓冲流读取
        FileInputStream fis = new FileInputStream("xx/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();

高效字符流

即BufferedWriter
BufferedReader
具体操作:

        FileWriter fw = new FileWriter("xx/Desktop/Test/liu.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        // 写
        bw.write("白日依山尽");
        bw.newLine();// 换行
        bw.write("黄河入海流");
        bw.newLine();// 换行
        bw.flush();
        bw.close();

        FileReader fReader = new FileReader("xx/Desktop/Test/liu.txt");
        BufferedReader bufferedReader = new BufferedReader(fReader);
        FileWriter fw2 = new FileWriter("xx/Desktop/Test/liu123.txt");
        BufferedWriter bufferedWriter = new BufferedWriter(fw2);

        String str = "";
        // 读取一行字符串方法 读到末尾 返回null
        while ((str = bufferedReader.readLine()) != null) {
            /*注意: 该方法不能读取到换行符
            System.out.print(str);
            */
            bufferedWriter.write(str);
            // 注意:复制文件时,需要加入换行
            // 复制文件会多一个换行
            bufferedWriter.newLine();
            bufferedWriter.flush();
        }
        bufferedReader.close();
        bufferedWriter.close();

Properties集合

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

读取文件的格式: key=value
1.注意等号前后别加空格
2.一般该文件使用.properties为后缀(起标识作用)
3.#为注释
具体操作:

        // 将文件直接读入集合中
        FileInputStream fis = new FileInputStream("xx/Desktop/Test/wangge.properties");
        // 创建集合
        Properties properties = new Properties();
        // 加载文件
        properties.load(fis);

        // 迭代器遍历
        Enumeration names = properties.propertyNames();
        while (names.hasMoreElements()) {
            String str = (String) names.nextElement();
            System.out.println(str + "=" + properties.getProperty(str));
        }

        fis.close();
        FileOutputStream fos = new FileOutputStream("xx/Desktop/Test/wl.properties");
        Properties properties = new Properties();
        // 保存键值对
        properties.setProperty("name", "wangge");
        properties.setProperty("age", "30");
        properties.setProperty("gender", "nan");
        // 写入到文件中
        // 参数2 是传入的注释 推荐英文
        properties.store(fos, "这是一个fos流");
        fos.close();

序列化流

将对象直接写入到文件中即ObjectOutputStream、ObjectInputStream。
具体操作:

    // 需要创建一个对象类 然后才能对这个操作序列化流
    public static void readObject() throws FileNotFoundException, IOException, ClassNotFoundException {
        // 读取文件
        FileInputStream fis = new FileInputStream("xx/Desktop/Test/mj.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        // ClassNotFoundException 找不到这个类
        // 将文件中对象 进行反序列化 需要依赖 存入对象的.class文件
        Object object = ois.readObject();
        System.out.println(object);
        ois.close();
    }

    public static void writeObject() throws FileNotFoundException, IOException {
        // 将对象写入文件
        FileOutputStream fos = new FileOutputStream("xx/Desktop/Test/mj.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        // 写入 NotSerializableException 要保存的对象的类没有实现序列化接口
        // Serializable 序列化接口是个标识行接口 标识这个类的对象可以进行序列化
        oos.writeObject(new Person("wangge", 30));
        // 关闭流
        oos.close();
    }

猜你喜欢

转载自blog.csdn.net/qq_37113621/article/details/82712636