day21

145 阅读24分钟
  • demo01

package com.lufax.day21.demo01.BufferedStream;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*  02
    java.io.BufferedOutputStream extends OutputStream
    BufferedOutputStream:字节缓冲输出流

    继承自父类的共性成员方法
        public void close() : 关闭此输出流并释放与此流相关联的任何系统资源。
        public void flush() : 刷新此输出流并强制任何缓冲的输出字节被写出。
        public void write(byte[] b) : 将b.length字节从指定的字节数组写入此输出流。
        public void write(byte[] b, int off, int len) : 从指定的字节数组写入len字节,从偏移量off开始输出到此输出流
        public abstract void write(int b) : 将指定的字节输出流

    构造方法:
        BufferedOutputStream(OutputStream out) 创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
        BufferedOutputStream(OutputStream out, int size) 创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。
        参数:
            OutputStream out :字节输出流
                    我们可以传递FileOutputStream,缓冲流会给FileOutputStream增加一个缓冲区,提高FileOutputStream的写入效率。
            int size:指定缓冲流内部缓冲区的大小,不指定则有默认大小
    使用步骤(重点)
        1.创建FileOutputStream对象,构造方法中绑定要输出的目的地
        2.创建BufferedOutputStream对象,构造方法中传递FileOutputStream对象,提高FileOutputStream对象效率
        3.使用BufferedOutputStream对象中的方法write,把数据写入到内部缓冲区中。
        4.使用BufferedOutputStream对象中的方法flush,把内部缓冲区中的数据,刷新到文件中
        5.释放资源(会先调用flush方法刷新数据,第四步可以省略)
 */
public class Demo01BufferedOutputStream {
    public static void main(String[] args) throws IOException {
        //1.创建FileOutputStream对象,构造方法中绑定要输出的目的地
        FileOutputStream fos = new FileOutputStream("aa\\ccc\\a.txt");
        //2.创建BufferedOutputStream对象,构造方法中传递FileOutputStream对象,提高FileOutputStream对象效率
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        // 3.使用BufferedOutputStream对象中的方法write,把数据写入到内部缓冲区中。
        bos.write("我把数据写入到内部缓冲区中".getBytes());
        //4.使用BufferedOutputStream对象中的方法flush,把内部缓冲区中的数据,刷新到文件中
        bos.flush();

        //5.释放资源(会先调用flush方法刷新数据,第四步可以省略)
        bos.close();
    }
}

------------------------------------------------------------------------------------

package com.lufax.day21.demo01.BufferedStream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

/*
    java.io.BufferedInputStream extends InputStream
    BufferedInputStream: 字节缓冲输入流

    继承自父类的成员方法
        int read() 从输入流中读取数据的下一个字节
        int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中。
        void close() 关闭此输入流并释放与该流关联的所有系统资源

    构造方法:
        BufferedInputStream(InputStream in) 创建一个BufferedInputStream并保存其参数,即输入流in,以便将来使用。
        BufferedInputStream(InputStream in, int size) 创建具有指定缓冲区大小的BufferedInputStream并保存其参数,即输入流in,以便将来使用。
        参数:
            InputStream in :字节输入流
                    我们可以传递FileInputStream,缓冲流会给FileInputStream增加一个缓冲区,提高FileInputStream的读取效率
            int size:指定缓冲流内部缓冲区的大小,不指定则有默认大小

    使用步骤(重点):
        1.创建FileInputStream对象,构造方法中绑定要读取的数据源
        2.创建BufferedInputStream对象,构造方法中传递FileInputStream对象,提高FileInputStream对象的读取效率
        3.使用BufferedInputStream对象中的方法read,读取文件
        4.释放资源
 */
public class Demo02BufferedInputStream {
    public static void main(String[] args) throws IOException {
        //1.创建FileInputStream对象,构造方法中绑定要读取的数据源
        FileInputStream fis = new FileInputStream("aa\\ccc\\a.txt");
        //2.创建BufferedInputStream对象,构造方法中传递FileInputStream对象,提高FileInputStream对象的读取效率
        BufferedInputStream bis = new BufferedInputStream(fis);
        //3.使用BufferedInputStream对象中的方法read,读取文件
       /* int len = 0;
        while ((len = bis.read()) != -1) {
            System.out.println(len);
        }*/

        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = bis.read(bytes)) != -1) {
            System.out.println(new String(bytes, 0, len));
        }
        //4.释放资源
        bis.close();
    }
}


------------------------------------------------------------------------------------
package com.lufax.day21.demo01.BufferedStream;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/*
    java.io.BufferedWriter extends Writer
    BufferedWriter:字符缓冲输出流

    继承自父类的共性成员方法:
        void write(int c) 写入单个字符
        void write(char[] cbuf) 写入字符数组
        abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分,off数组的开始索引,len写的字符个数
        void write(String str) :写入字符串
        void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个数。
        void flush()刷新该流的缓冲
        void close() 关闭此流,但要先刷新它。

    构造方法:
        BufferedWriter(Writer out) 创建一个使用默认大小输出缓冲区的缓冲字符输出流
        BufferedWriter(Writer out, int sz) 创建一个使用指定大小输出缓冲区的新缓冲字符输出流
        参数:
            Writer out : 字符输出流
                我们可以传递FileWriter,缓冲流会给FileWriter增加一个缓冲区,提高FileWriter的写入效率
            int sz : 指定缓冲区的大小,不写默认大小
    特有的成员方法:
        void newLine() 写入一个行分隔符。会根据不同的操作系统,获取不同的行分隔符
        换行符号:
        windows : \r\n
          linux : /n
           mac : /r
    使用步骤:
        1.创建字符缓冲输出流对象,构造方法中传递字符输出流
        2.调用字符缓冲输出流中的方法write,把数据写入到内存缓冲区中
        3.调用字符缓冲输出流中的方法flush,把内存缓冲区中的数据,刷新到文件中
        4、释放资源
 */
public class Demo03BufferedWriter {
    public static void main(String[] args) throws IOException {
        //1.创建字符缓冲输出流对象,构造方法中传递字符输出流
        BufferedWriter bw = new BufferedWriter(new FileWriter("aa\\ccc\\c.txt"));
        //2.调用字符缓冲输出流中的方法write,把数据写入到内存缓冲区中
        for (int i = 0; i < 10; i++) {
            bw.write("传值博客");
            //bw.write("\r\n");
            bw.newLine();
        }
        //3.调用字符缓冲输出流中的方法flush,把内存缓冲区中的数据,刷新到文件中
        bw.flush();
        //4、释放资源
        bw.close();
    }
}


------------------------------------------------------------------------------------
package com.lufax.day21.demo01.BufferedStream;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/*
    java.io.BufferedReader extends Reader

    继承自父类的共性成员方法:
        int read() 读取单个字符并返回
        int read(char[] cbuf) 一次读取多个字符,将字符读入数组。
        void close() 关闭该流并释放与之关联的所有资源。

    构造方法:
        BufferedReader(Reader in) 创建一个使用默认大小输入缓冲区的缓冲字符输入流。
        BufferedReader(Reader in, int sz) 创建一个使用指定大小输入缓冲区的缓冲字符输入流。
        参数:
            Reader in : 字符输入流
                我们可以传递FileReader,缓冲流会给FileReader增加一个缓冲区,提高FileReader的读取效率
            int sz : 指定缓冲区的大小,不写默认大小
    特有的成员方法:
        String readLine() 读取一个文本行。读取一行数据
            行的终止符号:通过下列字符之一即可认为某行已终止:换行('\n')、回车('\r')或回车后直接跟着换行(\r\n).
         返回值:包含改行内容的字符串,不包含任何终止符。如果已到达流末尾,则返回null

    使用步骤:
        1.创建字符缓冲输入流对象,构造方法中传递字符输入流
        2.使用字符缓冲输入流对象中的方法read/readLine读取文本
        3.释放资源
 */
public class Demo04BufferedReader {
    public static void main(String[] args) throws IOException {
        //1.创建字符缓冲输入流对象,构造方法中传递字符输入流
        BufferedReader br = new BufferedReader(new FileReader("aa\\ccc\\c.txt"));
        //2.使用字符缓冲输入流对象中的方法read/readLine读取文本
        /*String line = br.readLine();
        System.out.println(line);//传值博客1

        line = br.readLine();
        System.out.println(line);//传值博客2

        line = br.readLine();
        System.out.println(line);//传值博客3

        line = br.readLine();
        System.out.println(line);//null*/

        /*
            以上可以使用循环优化
         */
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }
}


------------------------------------------------------------------------------------

package com.lufax.day21.demo01.BufferedStream;

import java.io.*;
import java.util.HashMap;

/*
    练习:
        对文本的内容进行排序
        按照(1,2,3...)顺序排序
    分析:
        1、创建一个HashMap集合对象,可以:存储每行文本的序号(1,2,3,...);value:存储每行的文本
        2、创建字符缓冲输入流对象,构造方法中传递字符输入流
        3、创建字符缓冲输出流对象,构造方法中传递字符输出流
        4、使用字符缓冲输入流的方法readLine,逐行读取文本
        5、对读取到的文本进行切割,获取行中的序号和文本内容
        6、把切割好的序号和文本的内容存储到HashMap集合中(key序号是有序的,会自动排序1,2,3,4...)
        7、遍历hashMap集合,获取每一个键值对
        8、把每一个键值对,拼接为一个文本行
        9、把拼接好的文本,使用字符缓冲流中的write方法,写入到文件中
        10、释放资源
 */
public class Demo05Test {
    public static void main(String[] args) throws IOException {
        // 1、创建一个HashMap集合对象,可以:存储每行文本的序号(1,2,3,...);value:存储每行的文本
        HashMap<String, String> map = new HashMap<>();
        // 2、创建字符缓冲输入流对象,构造方法中传递字符输入流
        BufferedReader br = new BufferedReader(new FileReader("aa\\ccc\\in.txt"));
        //3.创建字符缓冲输出流对象,构造方法中传递字符输出流
        BufferedWriter bw = new BufferedWriter(new FileWriter("aa\\ccc\\out.txt"));
        //4、使用字符缓冲输入流的方法readLine,逐行读取文本
        String line;
        while ((line = br.readLine()) != null) {
            //5、对读取到的文本进行切割,获取行中的序号和文本内容
            String[] arr = line.split("\\.");
            // 6、把切割好的序号和文本的内容存储到HashMap集合中(key序号是有序的,会自动排序1,2,3,4...)
            map.put(arr[0], arr[1]);
        }
        //7、遍历hashMap集合,获取每一个键值对
        for (String key : map.keySet()) {
            String value = map.get(key);
            // 8、把每一个键值对,拼接为一个文本行
            line = key + "." + value;
            // 9、把拼接好的文本,使用字符缓冲流中的write方法,写入到文件中
            bw.write(line);
            bw.newLine();
        }
        //10、释放资源
        bw.close();
        br.close();
    }
}

------------------------------------------------------------------------------------


  • demo02

package com.lufax.day21.demo02.CopyFile;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
    13
    文件的复制的练习: 一读一写

    明确:
        数据源: c:\\1.jpg
        数据的目的地: d:\\1.jpg

    文件复制的步骤:
        1、创建一个字节输入流对象,构造方法中绑定要读取的数据源
        2、创建一个字节输出流对象,构造方法中绑定要写入的目的地
        3、使用字节输入流对象中的方法read读取文件
        4、使用字节输出流中的方法write,把读取到的字节写入到目的地的文件中
        5、释放资源
 */
public class Demo01CopyFile {
    public static void main(String[] args) throws IOException {
        long s = System.currentTimeMillis();
        //1、创建一个字节输入流对象,构造方法中绑定要读取的数据源
        FileInputStream fis = new FileInputStream("c:\\bk_login.jpg");
        //2、创建一个字节输出流对象,构造方法中绑定要写入的目的地
        FileOutputStream fos = new FileOutputStream("d:\\bk_login.jpg");
        //一次读取一个字节,写入一个字节的方法
        //3、使用字节输入流对象中的方法read读取文件
       /* int len = 0;
        while ((len = fis.read()) != -1) {
            //4、使用字节输出流中的方法write,把读取到的字节写入到目的地的文件中
            fos.write(len);
        }*/

        //使用数组缓冲读取多个字节,写入多个字节
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = fis.read(bytes)) != -1) {
            fos.write(bytes, 0, len);
        }


        //5、释放资源(先关闭写的,后关闭读的,如果写完了,肯定读取完毕了)
        fos.close();
        fis.close();
        long e = System.currentTimeMillis();
        System.out.println("复制文件共耗时:" + (e - s) + "毫秒");
    }
}

------------------------------------------------------------------------------------

package com.lufax.day21.demo02.CopyFile;

import java.io.*;

/*
    文件的复制的练习: 一读一写

    明确:
        数据源: c:\\1.jpg
        数据的目的地: d:\\1.jpg
     文件复制的步骤:
        1、创建字节缓冲输入流对象,构造方法中传递字节输入流
        2、创建字节缓冲输出流对象,构造方法中传递字节输出流
        3、使用字节缓冲输入流对象中的方法read,读取文件
        4、使用字节缓冲输出流中的方法write,把读取到的数据写入到内部缓冲区中
        5、释放资源(会先把缓冲区中的数据刷新到文件中)
 */
public class Demo02CopyFile {
    public static void main(String[] args) throws IOException {
        long s = System.currentTimeMillis();
        //1、创建字节缓冲输入流对象,构造方法中传递字节输入流
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("c:\\bk_login.jpg"));
        // 2、创建字节缓冲输出流对象,构造方法中传递字节输出流
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:\\bk_login.jpg"));
        //3、使用字节缓冲输入流对象中的方法read,读取文件
        //一次读取一个字节写入一个字节的方法
        /*int len = 0;
        while ((len = bis.read()) != -1) {
            bos.write(len);
        }*/

        //使用数组缓冲读取多个字节,写入多个字节
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }
        bos.close();
        bis.close();

        long e = System.currentTimeMillis();
        System.out.println("复制文件共耗时:" + (e - s) + "毫秒");
    }
}


------------------------------------------------------------------------------------



  • demo03
字符编码和字符集
编码:字符(能看懂的) --> 字节(看不懂的)
解码:字节(看不懂的) --> 字符(能看懂的)

字符编码:就是一套自然语言的字符与二进制数之间的对应规则
编码表:生活中文字和计算机中二进制的对应规则

字符集(charset):也叫编码表。是一个系统支持的所有字符的集合,包括国家文字,标点符号,图形符号,数字等。
    计算机要准确的存储和识别各种字符集符号,需要进行字符编码,一套字符集必然至少有一套字符编码。常见字符集有ASCII字符集、GBK字符集、Unicode字符集等。
ASCII字符集 ------  ASCII编码
GBK字符集   ------  GBK编码
Unicode字符集 ------ UTF-8编码、UTF-16编码、UTF-32编码
当指定了编码,它所对应的字符集自然就指定了,所以编码才是我们最终要关心的。

ASCII字符集:
    美国信息交换标准代码,是基于拉丁字母的一套电脑编码系统,用于显示现代英语,主要包括控制字符(回车、退格、换行键等)和可显示字符(英文大小写字符,阿拉伯数字和西文符号)
    基本的ASCII字符集,使用7位(bits)表示一个字符,共128字符。ASCII的扩展字符集使用8位(bits)表示一个字符,共256字符,方便支持欧洲常用字符。
ISO-8859-1字符集:
    拉丁码表,别名Latin-1,用于显示欧洲使用的语言,包括荷兰、丹麦、德语、意大利语、西班牙语等
    ISO-8859-1使用单字节编码,兼容ASCII编码。
GBxxx字符集:
    GB就是国标的意思,为了显示中文而设计的一套字符集
    GB2312:简体中文码表
    GBK:最常用的中文码表。是在GB2312标准基础上的扩展规范,使用双字节编码方案,共收录了21003个汉字,完全兼容GB2312标准,支持繁体汉字以及日韩汉字等。
    GB18030:最新的中文码表。收录汉字70244个,采用多字节编码,每个字可以由1个、2个或者4个字节组成。支持中国少数名族的文字,同时支持繁体汉字以及日韩汉字等。
Unicode字符集:
    unicode编码系统为表达任意语言的任意字符而设计,是业界的一种标准,也称为统一码,标准万国码。
    它最多使用4个字节的数字来表达每个字母、符号、或者文字。有三种编码方案,UTF-8/UTF-16/UTF-32,最常用的是UTF-8
    UTF-8编码,可以用来表示Unicode标准中任何字符。它使用一至四个字节为每个字符编码,编码规则:
        1.128个US-ASCII字符,只需要1个字节编码
        2.拉丁文等字符,需要二个字节编码
        3.大部分常用字(含中文),使用3个字节编码
        4.其他极少使用的Unicode辅助字符,使用四个字节编码

------------------------------------------------------------------------------------

package com.lufax.day21.demo03.ReverseStream;

import java.io.FileReader;
import java.io.IOException;

/* 11
    FileReader可以读取默认编码格式(UTF-8)的文件
    FileReader读取系统默认编码(中文GBK)会产生乱码
 */
public class Demo01FileReader {
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("aa\\ccc\\我是GBK格式的文本.txt");
        int len = 0;
        while ((len = fr.read()) != -1) {
            System.out.println((char) len);
        }
        fr.close();
    }
}


------------------------------------------------------------------------------------
package com.lufax.day21.demo03.ReverseStream;

import java.io.*;

/*
    java.io.OutputStreamWriter extends Writer
    OutputStreamWriter:是字符流通向字节流的桥梁:可使用指定的charset将要写入流中的字符编码成字节。(编码:把能看懂的编程看不懂的)

    继承自父类的共性成员方法:
        void write(int c) 写入单个字符
        void write(char[] cbuf) 写入字符数组
        abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分,off数组的开始索引,len写的字符个数
        void write(String str) :写入字符串
        void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个数。
        void flush()刷新该流的缓冲
        void close() 关闭此流,但要先刷新它。
    构造方法:
        OutputStreamWriter(OutputStream out) 创建使用默认字符编码的OutputStreamWriter。
        OutputStreamWriter(OutputStream out, String charsetName) 创建使用指定字符集的OutputStreamWriter。
        参数:
            OutputStream out :字节输出流,可以用来写转换之后的字节到文件中
            String charsetName:指定的编码表名称,不区分大小写,可以是UTF-8/utf-8,GBK/gbk, 不指定默认使用idea的默认编码UTF-8

    使用步骤:
        1、创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
        2、使用OutputStreamWriter对象中的方法write,把字符转换为字节,存储缓冲区中(编码)
        3、使用OutputStreamWriter对象的flush方法,把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)
        4、释放资源
 */
public class Demo02OutputStreamWriter {
    public static void main(String[] args) throws IOException {
        //write_utf_8();
        write_gbk();
    }

    /*
        使用转换流OutputStreamWriter写gbk格式的文件
     */
    private static void write_gbk() throws IOException {
        //1、创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("aa\\ccc\\gbk.txt"), "gbk");
        // 2、使用OutputStreamWriter对象中的方法write,把字符转换为字节,存储缓冲区中(编码)
        osw.write("你好");
        // 3、使用OutputStreamWriter对象的flush方法,把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)
        osw.flush();
        //4、释放资源
        osw.close();
    }


    /*
        使用转换流OutputStreamWriter写UTF-8格式的文件
     */
    private static void write_utf_8() throws IOException {
        //1、创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("aa\\ccc\\utf_8.txt"), "UTF-8");
        // 2、使用OutputStreamWriter对象中的方法write,把字符转换为字节,存储缓冲区中(编码)
        osw.write("你好");
        // 3、使用OutputStreamWriter对象的flush方法,把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)
        osw.flush();
        //4、释放资源
        osw.close();
    }
}


------------------------------------------------------------------------------------
package com.lufax.day21.demo03.ReverseStream;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/*
    java.io.InputStreamReader extends Reader
    InputStreamReader : 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。(解码:把看不懂的变成能看懂的)

    继承自父类的共性成员方法:
        int read() 读取单个字符并返回
        int read(char[] cbuf) 一次读取多个字符,将字符读入数组。
        void close() 关闭该流并释放与之关联的所有资源。

    构造方法:
        InputStreamReader(InputStream in) 创建一个使用默认字符集的 InputStreamReader。
        InputStreamReader(InputStream in, String charsetName) 创建一个使用指定字符集的 InputStreamReader。
        参数:
            InputStream in : 字节输入流,用来读取文件中保存的字节
            String charsetName :编码表名称

    使用步骤:
        1、创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
        2、使用InputStreamReader对象中的方法read读取文件
        3、释放资源
    注意事项:
        构造方法中指定的编码表名称要和文件的编码相同,否则会发生乱码
 */
public class Demo03InputStreamReader {
    public static void main(String[] args) throws IOException {
        //read_uft_8();
        read_gbk();
    }


    /*
        使用InputStreamReader读取gbk格式的文件
     */
    private static void read_gbk() throws IOException {
        // 1、创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
        InputStreamReader isr = new InputStreamReader(new FileInputStream("aa\\ccc\\gbk.txt"), "gbk");
        //2、使用InputStreamReader对象中的方法read读取文件
        int len = 0;
        while ((len = isr.read()) != -1) {
            System.out.println((char) len);
        }
        // 3、释放资源
        isr.close();
    }


    /*
        使用InputStreamReader读取UTF-8格式的文件
     */
    private static void read_uft_8() throws IOException {
        // 1、创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
        InputStreamReader isr = new InputStreamReader(new FileInputStream("aa\\ccc\\utf_8.txt"), "utf-8");
        //2、使用InputStreamReader对象中的方法read读取文件
        int len = 0;
        while ((len = isr.read()) != -1) {
            System.out.println((char) len);
        }
        // 3、释放资源
        isr.close();
    }
}


------------------------------------------------------------------------------------
package com.lufax.day21.demo03.ReverseStream;

import java.io.*;

/*
    练习:转换文件编码
        将GBK编码的文本文件,转换为UTF-8编码的文本文件

    分析:
        1、创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称GBK
        2、创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称UTF-8
        3、使用InputStreamReader对象中的方法read读取文件
        4、使用OutputStreamWriter对象的方法write,把读取的数据写入到文件中
        5、释放资源
 */
public class Demo04Test {
    public static void main(String[] args) throws IOException {
        // 1、创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称GBK
        InputStreamReader isr = new InputStreamReader(new FileInputStream("aa\\ccc\\我是GBK格式的文本.txt"), "gbk");
        //2、创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称UTF-8
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("aa\\ccc\\我是utf_8格式的文本.txt"), "utf-8");
        // 3、使用InputStreamReader对象中的方法read读取文件
        int len = 0;
        while ((len = isr.read()) != -1) {
            //4、使用OutputStreamWriter对象的方法write,把读取的数据写入到文件中
            osw.write(len);
        }
        //5、释放资源
        osw.close();
        isr.close();
    }
}


------------------------------------------------------------------------------------



  • demo04

package com.lufax.day21.demo04.ObjectStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

/*
    15
    java.io.ObjectOutputStream extends OutputStream
    ObjectOutputStream:对象的序列化流
    作用:把对象以流的方式写入到文件中保存

    构造方法:
        ObjectOutputStream(OutputStream out) 创建写入指定 OutputStream的ObjectOutputStream
        参数:
            OutputStream out :字节输出流

     持有的成员方法:
        void writeObject(Object obj) 将指定的对象写入 ObjectOutputStream。

     使用步骤
        1、创建一个ObjectOutputStream对象,构造方法中传递字节输出流
        2、使用ObjectOutputStream对象中的writeObject方法,把对象写入到文件中
        3、释放资源
 */
public class Demo01ObjectOutputStream {
    public static void main(String[] args) throws IOException {
        //1、创建一个ObjectOutputStream对象,构造方法中传递字节输出流
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("aa\\ccc\\person.txt"));
        //2、使用ObjectOutputStream对象中的writeObject方法,把对象写入到文件中
        oos.writeObject(new Person("小美女", 18));
        //3、释放资源
        oos.close();
    }
}

------------------------------------------------------------------------------------

package com.lufax.day21.demo04.ObjectStream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

/*  19
    java.io.ObjectInputStream extends InputStream
    ObjectInputStream:对象的反序列化流
    作用:把文件中保存的对象,以流的方式读取出来使用

    构造方法
        ObjectInputStream(InputStream in) 创建从指定 InputStream读取的ObjectInputStream。
        参数:
            InputStream in : 字节输入流
     特有的成员方法:
        Object readObject() 从ObjectInputStream读取对象。

     使用步骤:
        1、创建ObjectInputStream对象,构造方法中传递字节输入流
        2、使用ObjectInputStream对象中的方法readObject读取保存对象的文件
        3、释放资源
        4、使用读取出来的对象(打印)


     readObject()方法声明抛出了ClassNotFoundException(class文件找不到异常)
     当不存在对象的class文件时抛出此异常
     反序列化的前提:
        1、类必须实现Serializable接口
        2、必须存在类对应的class文件
 */
public class Demo02ObjectInputStream {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //1、创建ObjectInputStream对象,构造方法中传递字节输入流
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("aa\\ccc\\person.txt"));
        //2、使用ObjectInputStream对象中的方法readObject读取保存对象的文件
        Object o = ois.readObject();
        //3、释放资源
        ois.close();
        //4、使用读取出来的对象(打印)
        System.out.println(o);
        Person p = (Person) o;
        System.out.println(p.getName() + p.getAge());
    }
}


------------------------------------------------------------------------------------

package com.lufax.day21.demo04.ObjectStream;

import java.io.*;
import java.util.ArrayList;

/*
    练习:序列化集合
        当我们想在文件中保存多个对象的时候
        可以把多个对象存储到一个集合中
        对集合进行序列化和反序列化
     分析:
        1、定义一个存储Person对象的ArrayList集合
        2、在ArrayList集合中存储Person对象
        3、创建一个序列化流ObjectOutputStream对象
        4、使用ObjectOutputStream对象中的方法writeObject对集合进行序列化
        5、创建一个反序列化ObjectInputStream对象
        6、使用ObjectInputStream对象中的方法readObject读取文件中保存的集合
        7、把Object类型的集合转换为ArrayList类型
        8、遍历ArrayList集合
        9、释放资源
 */
public class Demo03Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //1、定义一个存储Person对象的ArrayList集合
        ArrayList<Person> list = new ArrayList<>();
        //2、在ArrayList集合中存储Person对象
        list.add(new Person("张三", 18));
        list.add(new Person("李四", 19));
        list.add(new Person("王五", 20));
        //3、创建一个序列化流ObjectOutputStream对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("aa\\ccc\\list.txt"));
        //4、使用ObjectOutputStream对象中的方法writeObject对集合进行序列化
        oos.writeObject(list);
        //5、创建一个反序列化ObjectInputStream对象
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("aa\\ccc\\list.txt"));
        //6、使用ObjectInputStream对象中的方法readObject读取文件中保存的集合
        Object o = ois.readObject();
        //7、把Object类型的集合转换为ArrayList类型
        ArrayList<Person> list2 = (ArrayList<Person>) o;
        //8、遍历ArrayList集合
        for (Person p : list) {
            System.out.println(p);
        }
        //9、释放资源
        ois.close();
        oos.close();
    }
}

------------------------------------------------------------------------------------

package com.lufax.day21.demo04.ObjectStream;

import java.io.Serializable;

/*
    序列化和反序列化的时候,会抛出NotSerializableException没有序列化异常
    类通过实现java.io.Serializable接口以启用其序列化功能,未实现此接口的类将无法使其任何状态序列化或反序列化。
    Serializable接口也叫标记型接口
        要进行序列化和反序列化的类必须要实现Serializable接口,就会给类添加一个标记
        当我们进行序列化和反序列化的时候,就会检测类上是否有这个标记
            有:就可以序列化和反序列化
            没有:就会抛出NotSerializableException异常
    去市场买肉 --> 肉上有一个蓝色章(检测合格) --> 放心购买 --> 买回来怎么吃随意

    static关键字:静态关键字
        静态优先于非静态加载到内存中(静态优先于对象进入到内存中)
        被static修饰的成员变量不能被序列化的,序列化的都是对象
        private static int age;
        oos.writeObject(new Person("小美女", 18));
        Object o = ois.readObject();
        Person{name='小美女', age=0}

    transient关键字:瞬态关键字
        被transient修饰的成员变量不能被序列化
        private transient int age;
 */
public class Person implements Serializable {
    //解决冲突
    private static final long serialVersionUID = 1L;
    private String name;
    public int age;
    //private transient int age;
    //private static int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", 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;
    }
}

------------------------------------------------------------------------------------



  • demo05

package com.lufax.day21.demo05.PrintStream;

import java.io.FileNotFoundException;
import java.io.PrintStream;

/*
    java.io.PrintStream:打印流
        PrintStream 为其他输出流添加了功能,是他们能够方便地打印出各种数据值表示形式
    PrintStream特点:
        1、只负责数据的输出,不负责数据的读取
        2、与其他输出流不同,PrintStream永远不会抛出 IOException
        3、有特有的方法,print,println方法
            void print(任意类的值)
            void println(任意类的值并换行)
    构造方法:
        PrintStream(File file) 输出的目的地是一个文件
        PrintStream(OutputStream out) 输出的目的地是一个字节输出流
        PrintStream(String fileName) 输出的目的地是一个文件路径
    PrintStream extends OutputStream
    继承自父类的成员方法
        public void close() : 关闭此输出流并释放与此流相关联的任何系统资源。
        public void flush() : 刷新此输出流并强制任何缓冲的输出字节被写出。
        public void write(byte[] b) : 将b.length字节从指定的字节数组写入此输出流。
        public void write(byte[] b, int off, int len) : 从指定的字节数组写入len字节,从偏移量off开始输出到此输出流
        public abstract void write(int b) : 将指定的字节输出流

    注意:
        如果使用继承自父类的write方法写数据,那么查看数据的时候会查询编码表 97->a
        如果使用自己特有的方法print 、println方法写数据,写的数据会原样输出  97->97
 */
public class Demo01PrintStream {
    public static void main(String[] args) throws FileNotFoundException {
        //System.out.println("HelloWorld");
        //创建打印流PrintStream对象,构造方法中绑定要输出的目的地
        PrintStream ps = new PrintStream("aa\\ccc\\print.txt");
        //使用继承自父类的write方法写数据,那么查看数据的时候会查询编码表 97->a
        ps.write(97);
        //使用自己特有的方法print 、println方法写数据,写的数据会原样输出  97->97
        ps.println(97);
        ps.println(9.7);
        ps.println("字符");
        ps.println(true);

        ps.close();

    }
}

------------------------------------------------------------------------------------


package com.lufax.day21.demo05.PrintStream;

import java.io.FileNotFoundException;
import java.io.PrintStream;

/*
    可以改变输出语句的目的地(打印流的方向)
    输出语句,默认在控制台输出
    使用System.setOut()方法改变输出语句的目的地为参数传递的打印流的目的地
        static void setOut(PrintStream out)
            重新分配“标准”输出流
 */
public class Demo02PrintStream {
    public static void main(String[] args) throws FileNotFoundException {
        System.out.println("我在控制台输出");
        PrintStream ps = new PrintStream("aa\\ccc\\目的地是打印流.txt");
        System.setOut(ps);//把输出语句的目的地改变为打印流的目的地
        System.out.println("我在打印流的目的地中输出");
    }
}

------------------------------------------------------------------------------------