IO


#IO

概述

  • IO流介绍

    • IO:输入/输出(Input/Output)
    • 流:是一种抽象概念,是对数据传输的总称.也就是说数据在设备间的传输称为流,流的本质是数据传输
    • IO流就是用来处理设备间数据传输问题的.常见的应用: 文件复制; 文件上传; 文件下载
    • 参照物:内存 内存在读,内存在写
  • IO流的分类

  • 按照数据的流向

    • 输入流:读数据
    • 输出流:写数据
  • 按照数据类型来分

    • 字节流
      • 字节输入流
      • 字节输出流
    • 字符流
      • 字符输入流
      • 字符输出流
  • IO流的使用场景

    • 如果操作的是纯文本文件,优先使用字符流
    • 如果操作的是图片、视频、音频等二进制文件,优先使用字节流
    • 如果不确定文件类型,优先使用字节流.字节流是万能的流

纯文本文件?

用windows记事本打开能读懂的文件就是纯文本文件

纯文本:字符流,否则:字节流

字节流

字节流写数据

  • 字节流抽象基类

    • InputStream:这个抽象类是表示字节输入流的所有类的超类
    • OutputStream:这个抽象类是表示字节输出流的所有类的超类
    • 子类名特点:子类名称都是以其父类名作为子类名的后缀
  • 字节输出流

    • FileOutputStream(String name):创建文件输出流以指定的名称写入文件
  • 使用字节输出流写数据的步骤

    • 创建字节输出流对象(调用系统功能创建了文件,创建字节输出流对象,让字节输出流对象指向文件)
    • 调用字节输出流对象的写数据方法
    • 释放资源(关闭此文件输出流并释放与此流相关联的任何系统资源)
  • 示例代码

public class OutPutDemo {
    public static void main(String[] args) throws IOException {
        //1.创建字节输出流对象--- 告诉虚拟机我要往哪个文件中写数据  下面两个方法一样的我们用第一个简单的就行
        FileOutputStream fos = new FileOutputStream("F:\\itheima\\a.txt");
        FileOutputStream fs = new FileOutputStream(new File("F:\\itheima\\a.txt"));
        //2.写数据
        fos.write(97);
        //3.释放资源
        fos.close();
    }
}

注意事项:

  1. 创建字节输出流对象

    1.如果文件不存在,会帮我们创建
    2.如果文件存在,会把文件清空

  2. 写数据

    传递一个整数时,那么实际上写到文件中的,是这个整数在码表中对应的字符

  3. 释放资源

    告诉操作系统,我现在已经不需要再用这个文件了

    每次使用完流必须要释放资源

字节流写数据的三种方式

  • 写数据的方法分类

    方法名 说明
    void write(int b) 将指定的字节写入此文件输出流 一次写一个字节数据
    void write(byte[] b) 将 b.length字节从指定的字节数组写入此文件输出流 一次写一个字节数组数据
    void write(byte[] b, int off, int len) 将 len字节从指定的字节数组开始,从偏移量off开始写入此文件输出流 一次写一个字节数组的部分数据
  • 示例代码

一次写一个字节

public class OutPutDemo1 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("F:\\itheima\\a.txt");
        fos.write(97);
        fos.write(98);
        fos.write(99);
        fos.close();
    }
}

一次写一个字节数组

public class OutPutDemo2 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("F:\\itheima\\a.txt");
        byte[] arr = {97,98,99};
        fos.write(arr);
        fos.close();
    }
}

一次写一个字节数组的一部分

public class OutPutDemo3 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("F:\\itheima\\a.txt");
        byte[] arr = {97,98,99,100,101,102,103,104};
        fos.write(arr,1,3);
        fos.close();//bcd
    }
}

字节流写数据的两个小问题

  • 字节流写数据如何实现换行

    • windows:\r\n
    • linux:\n
    • mac:\r
  • 示例代码

public class OutPutDemo4 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("F:\\itheima\\a.txt");
        fos.write(97);
        fos.write("\r\n".getBytes());
        fos.write(98);
        fos.write("\r\n".getBytes());
        fos.write(99);
        fos.write("\r\n".getBytes());
        fos.write(100);
        fos.write("\r\n".getBytes());
        fos.write(101);
        fos.write("\r\n".getBytes());
        fos.close();
    }
}

字节流写数据如何实现追加写入

  • public FileOutputStream(String name,boolean append)
  • 创建文件输出流以指定的名称写入文件。如果第二个参数为true ,则字节将写入文件的末尾而不是开头
  • 示例代码
public class OutPutDemo4 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("F:\\itheima\\a.txt",true);
        fos.write(69);
        fos.write("\r\n".getBytes());
        fos.write(70);
        fos.write("\r\n".getBytes());
        fos.close();
    }
}

第二个参数是续写开关,如果不写就是false,表示不打开续写功能会清空文件内容

字节流写数据加异常处理

  • 异常处理格式

    • try-catch-finally

      try{
      	可能出现异常的代码;
      }catch(异常类名 变量名){
      	异常的处理代码;
      }finally{
      	执行所有清除操作;
      }
      
    • finally特点

      • 被finally控制的语句一定会执行,除非JVM退出
  • 示例代码

public class OutPutDemo5 {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("F:\\itheima\\a.txt", true);
            fos.write(69);
            fos.write("\r\n".getBytes());
            fos.write(70);
            fos.write("\r\n".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

字节流读数据

字节流读取数据方式

一次读取一个字节

  • 字节输入流

    • FileInputStream(String name):通过打开与实际文件的连接来创建一个FileInputStream,该文件由文件系统中的路径名name命名
  • 字节输入流读取数据的步骤

    • 创建字节输入流对象

      如果文件存在,那么正常

      如果文件不存在会报FileNotFoundException

    • 调用字节输入流对象的读数据方法

      一次读取一个字节,返回值就是本次读到的那个字节数据

      也就是字符在码表中的数字,如果先要看到字符数据,那么一定要强转为char

    • 释放资源

  • 示例代码

public class OutPutDemo6 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("F:\\itheima\\a.txt");
        int read = fis.read();
        System.out.println(read);
        fis.close();//a
    }
}
读多个字节
public class OutPutDemo7 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("F:\\itheima\\a.txt");
        //文件中多个字节怎么办
      int b;
      while ((b = fis.read()) != -1){
          System.out.println((char)b);
      }
      fis.close();
    }
}

复制文件

  • 案例需求

    ​ 把“E:\itcast\窗里窗外.txt”复制到模块目录下的“窗里窗外.txt” (文件可以是任意文件)

  • 实现步骤

    • 复制文本文件,其实就把文本文件的内容从一个文件中读取出来(数据源),然后写入到另一个文件中(目的地)

    • 数据源:

      ​ E:\itcast\窗里窗外.txt --- 读数据 --- InputStream --- FileInputStream

    • 目的地:

      ​ myByteStream\窗里窗外.txt --- 写数据 --- OutputStream --- FileOutputStream

  • 代码实现

一次读一个字节数组

  • 案例需求

    ​ 把“E:\itcast\mn.jpg”复制到模块目录下的“mn.jpg” (文件可以是任意文件去)

  • 实现步骤

    • 根据数据源创建字节输入流对象
    • 根据目的地创建字节输出流对象
    • 读写数据,复制图片(一次读取一个字节数组,一次写入一个字节数组)
    • 释放资源
  • 代码实现

public class OutPutDemo9 {
    public static void main(String[] args) throws IOException {
        //准备读文件
        FileInputStream fis = new FileInputStream("F:\\itheima\\a.txt");
        //准备写文件
        FileOutputStream fos = new FileOutputStream("F:\\setup\\a.txt");
        //一次读一个拷贝速度慢怎么办
        //相当于买鸡蛋的篮子  容器
        byte[] buf = new byte[1024];
        //本次读到的有效字节个数 -- 这次读了几个字节
        int len;
        while ((len = fis.read(buf)) != -1){
        	//0索引开始len个字节,这里是文件中字符个数
            fos.write(buf, 0, len);
        }
        fis.close();
        fos.close();
    }
}

字节缓冲流

  • 字节缓冲流介绍

    • lBufferOutputStream:该类实现缓冲输出流.通过设置这样的输出流,应用程序可以向底层输出流写入字节,而不必为写入的每个字节导致底层系统的调用
    • lBufferedInputStream:创建BufferedInputStream将创建一个内部缓冲区数组.当从流中读取或跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次很多字节
  • 构造方法:

    方法名 说明
    BufferedOutputStream(OutputStream out) 创建字节缓冲输出流对象
    BufferedInputStream(InputStream in) 创建字节缓冲输入流对象
  • 为什么构造方法需要的是字节流,而不是具体文件或路径呢?

    字节缓冲流仅仅提供缓冲区,而真正读写数据还得靠基本的字节流对象进行操作

    缓冲流只是为了提高效率而存在的

一次读取一个字节数据

  • 示例代码
public class OutPutDemo10 {
    public static void main(String[] args) throws IOException {
        //创建字节缓冲输入流
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\itheima\\a.txt"));
        //创建字节缓冲输出流
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\setup\\a.txt"));
        int b;
        while ((b = bis.read()) != -1){
            bos.write(b);
        }
        //方法的底层会把字节流给关闭
        bis.close();
        bos.close();
    }

BufferedInputStream、BufferedOutputStream相当于在底层创建了一个长度为8192的字节数组

一次读写一个字节数组
public class OutPutDemo11 {
    public static void main(String[] args) throws IOException {
        //创建字节缓冲输入流
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\itheima\\a.txt"));
        //创建字节缓冲输出流
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\setup\\a.txt"));
        byte[] buf = new byte[1024];
        int len;
        while ((len = bis.read(buf)) != -1){
            bos.write(buf, 0, len);
        }
        bis.close();
        bos.close();
    }
}

字符流

为什么会出现字符流

  • 字符流的介绍

    由于字节流操作中文不是特别的方便,所以Java就提供字符流

    字符流 = 字节流 + 编码表

  • 中文的字节存储方式

    用字节流复制文本文件时,文本文件也会有中文,但是没有问题,原因是最终底层操作会自动进行字节拼接成中文,如何识别是中文的呢?

    汉字在存储的时候,无论选择哪种编码存储,第一个字节都是负数

编码表

  • 什么是字符集

    是一个系统支持的所有字符的集合,包括各国家文字、标点符号、图形符号、数字等

    l计算机要准确的存储和识别各种字符集符号,就需要进行字符编码,一套字符集必然至少有一套字符编码。常见字符集有ASCII字符集、GBXXX字符集、Unicode字符集等

  • 常见的字符集

    • ASCII字符集:

      lASCII:是基于拉丁字母的一套电脑编码系统,用于显示现代英语,主要包括控制字符(回车键、退格、换行键等)和可显示字符(英文大小写字符、阿拉伯数字和西文符号)

      基本的ASCII字符集,使用7位表示一个字符,共128字符。ASCII的扩展字符集使用8位表示一个字符,共256字符,方便支持欧洲常用字符。是一个系统支持的所有字符的集合,包括各国家文字、标点符号、图形符号、数字等

    • GBXXX字符集:

      GBK:最常用的中文码表。是在GB2312标准基础上的扩展规范,使用了双字节编码方案,共收录了21003个汉字,完全兼容GB2312标准,同时支持繁体汉字以及日韩汉字等

    • Unicode字符集:

      UTF-8编码:可以用来表示Unicode标准中任意字符,它是电子邮件、网页及其他存储或传送文字的应用 中,优先采用的编码。互联网工程工作小组(IETF)要求所有互联网协议都必须支持UTF-8编码。它使用一至四个字节为每个字符编码

      编码规则:

      128个US-ASCII字符,只需一个字节编码

      拉丁文等字符,需要二个字节编码

      大部分常用字(含中文),使用三个字节编码

      其他极少使用的Unicode辅助字符,使用四字节编码

字符串中的编码解码问题

  • 相关方法

    方法名 说明
    byte[] getBytes() 使用平台的默认字符集将该 String编码为一系列字节
    byte[] getBytes(String charsetName) 使用指定的字符集将该 String编码为一系列字节
    String(byte[] bytes) 使用平台的默认字符集解码指定的字节数组来创建字符串
    String(byte[] bytes, String charsetName) 通过指定的字符集解码指定的字节数组来创建字符串
  • 代码演示

public class OutPutDemo12 {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String sf = "大吉大利,今晚吃鸡!";
        byte[] bytes = sf.getBytes();
        //利用idea默认的UTF-8编码为一系列字节
        System.out.println(Arrays.toString(bytes));
        byte[] bytes1 = sf.getBytes("GBK");
        System.out.println(Arrays.toString(bytes1));
    }
}

执行结果

[-27, -92, -89, -27, -112, -119, -27, -92, -89, -27, -120, -87, -17, -68, -116, -28, -69, -118, -26, -103, -102, -27, -112, -125, -23, -72, -95, -17, -68, -127]
[-76, -13, -68, -86, -76, -13, -64, -5, -93, -84, -67, -15, -51, -19, -77, -44, -68, -90, -93, -95]
大吉大利,
大吉大利,今晚吃鸡!

字节流为什么会出现乱码

因为字节流一次读取一个字节,而不管GBK还是UTF-8一个中文都是多个字节,用字节流每次只能读取其中一部分内容,所以就会出现乱码问题。

字符流读取中文过程

字符流 = 字节流 + 码表;

基础知识:

不管在哪张码表中,中文的第一个字节一定是负数

字符流写数据

  • 介绍

    Writer: 用于写入字符流的抽象父类

    FileWriter: 用于写入字符流的常用子类

  • 构造方法

    方法名 说明
    FileWriter(File file) 根据给定的 File 对象构造一个 FileWriter 对象
    FileWriter(File file, boolean append) 根据给定的 File 对象构造一个 FileWriter 对象
    FileWriter(String fileName) 根据给定的文件名构造一个 FileWriter 对象
    FileWriter(String fileName, boolean append) 根据给定的文件名以及指示是否附加写入数据的 boolean 值来构造 FileWriter 对象
  • 成员方法

    方法名 说明
    void write(int c) 写一个字符
    void write(char[] cbuf) 写入一个字符数组
    void write(char[] cbuf, int off, int len) 写入字符数组的一部分
    void write(String str) 写一个字符串
    void write(String str, int off, int len) 写一个字符串的一部分
  • 刷新和关闭的方法

    方法名 说明
    flush() 刷新流,之后还可以继续写数据
    close() 关闭流,释放资源,但是在关闭之前会先刷新流。一旦关闭,就不能再写数据
  • 代码演示

一次写出一个字符
public class OutPutDemo13 {
    public static void main(String[] args) throws IOException {
        //创建字符输出流对象
        FileWriter fw = new FileWriter(new File("F:\\itheima\\a.txt"));
        //写出数据
        fw.write(97);
        fw.write(98);
        fw.write(99);
        //释放资源
        fw.close();
    }
}
一次写出一个字符数组
public class OutPutDemo13 {
    public static void main(String[] args) throws IOException {
        //创建字符输出流对象
        FileWriter fw = new FileWriter(new File("F:\\itheima\\a.txt"));
        //写出数据
        char [] chars = {97,98,99,100};
        fw.write(chars);
        //释放资源
        fw.close();
    }
}
写一个字符串(常用)
public class OutPutDemo13 {
    public static void main(String[] args) throws IOException {
        //创建字符输出流对象
        FileWriter fw = new FileWriter(new File("F:\\itheima\\a.txt"));
        //写出数据
        String s = "大吉大利,今晚吃鸡";
        fw.write(s);
        //释放资源
        fw.close();
    }
}

注意事项

flush和close
//创建字符输出流对象
FileWriter fw = new FileWriter(new File("F:\\itheima\\a.txt"));
//写出数据
String s = "大吉大利,今晚吃鸡";
fw.write(s);
//把数据刷新到本地文件中
fw.flush();
//释放资源
fw.close();

使用flush()刷新数据到本地后,还可以继续录入

close()也会刷新数据到本地,但是就关流了,之后不能再输入

字符流读数据

一次读取一个字符
public class OutPutDemo14 {
    public static void main(String[] args) throws IOException {
        //创建字符输出流对象
        FileReader fr = new FileReader(new File("F:\\itheima\\a.txt"));

        //读数据
        //一次读取一个字符
        int ch;
        while ((ch = fr.read()) != -1) {
            System.out.println((char)ch);
        }

        //释放资源
        fr.close();
    }
}
一次读取多个字符
public class OutPutDemo15 {
    public static void main(String[] args) throws IOException {
        //创建字符输出流对象
        FileReader fr = new FileReader(new File("F:\\itheima\\a.txt"));

        //读数据
        //创建一个字符数组
        //一次读取多个字符
        char [] chars = new char[1024];
        int len;
        while ((len = fr.read(chars)) != -1) {
            System.out.println(new String(chars, 0, len));
        }

        //释放资源
        fr.close();
    }
}

小练习

  • 案例需求

    将键盘录入的用户名和密码保存到本地实现永久化存储

  • 实现步骤

    • 获取用户输入的用户名和密码
    • 将用户输入的用户名和密码写入到本地文件中
    • 关流,释放资源
  • 代码实现

public class OutPutDemo16 {
    public static void main(String[] args) throws IOException {
        //需求
        //将键盘录入的用户名和密码保存到本地实现永久化存储

        //分析
        //1.实现键盘录入,把用户名密码录入进来

        Scanner sc = new Scanner(System.in);
        System.out.println("请录入用户名:");
        String username = sc.next();
        System.out.println("请录入密码:");
        String password = sc.next();

        //2.分别把用户名密码写入到本地
        //创建字符输出流对象
        FileWriter fw = new FileWriter("F:\\itheima\\b.txt");
        //把用户名密码写入到本地
        fw.write(username);
        //表示一个回车换行符
        fw.write("\r\n");
        fw.write(password);
        //刷新流
        fw.flush();
        //释放资源
        fw.close();
    }
}

字符缓冲流

  • 字符缓冲流介绍

    • BufferedWriter:将文本写入字符输出流,缓冲字符,以提供单个字符,数组和字符串的高效写入,可以指定缓冲区大小,或者可以接受默认大小。默认值足够大,可用于大多数用途

    • BufferedReader:从字符输入流读取文本,缓冲字符,以提供字符,数组和行的高效读取,可以指定缓冲区大小,或者可以使用默认大小。 默认值足够大,可用于大多数用途

  • 构造方法

    方法名 说明
    BufferedWriter(Writer out) 创建字符缓冲输出流对象
    BufferedReader(Reader in) 创建字符缓冲输入流对象
  • 代码演示

public class OutPutDemo17 {
    public static void main(String[] args) throws IOException {
        //创建字符输出流对象
        BufferedReader br = new BufferedReader(new FileReader(new File("F:\\itheima\\a.txt")));

        //读数据
        //创建一个字符数组
        //一次读取多个字符
        char [] chars = new char[1024];
        int len;
        while ((len = br.read(chars)) != -1) {
            System.out.println(new String(chars, 0, len));
        }

        //释放资源
        br.close();
    }
}
字符缓冲输出流
public class OutPutDemo18 {
    public static void main(String[] args) throws IOException {
        //字符缓冲输出流
        BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\itheima\\c.txt"));
        //写出数据
        bw.write(97);
        bw.write(98);
        bw.write(99);
        bw.write(100);
        bw.write("\r\n");
        char [] chars = {
            101,102,103,104,105
        };

        bw.write(chars);
        bw.write("\r\n");
        bw.write(chars,2,2);
        bw.write("\r\n");
        bw.write("大吉大利");
        bw.write("\r\n");

        bw.flush();
        bw.close();
    }
}
字符缓冲流特有功能
  • 方法介绍

    BufferedWriter:

    方法名 说明
    void newLine() 写一行行分隔符,行分隔符字符串由系统属性定义

    BufferedReader:

    方法名 说明
    String readLine() 读一行文字。 结果包含行的内容的字符串,不包括任何行终止字符如果流的结尾已经到达,则为null
  • 代码演示

public class OutPutDemo19 {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\itheima\\d.txt"));

        //写出数据
        bw.write("大吉大利,今晚吃鸡");
        bw.newLine();
        bw.write("abcdefg");
        bw.newLine();

        bw.flush();
        bw.close();

    }
}
public class OutPutDemo20 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("F:\\itheima\\d.txt"));

        //读出数据
        String s = br.readLine();
        String l = br.readLine();
        String r = br.readLine();
        System.out.println(s);
        System.out.println(l);
        System.out.println(r);

        br.close();
    }
}

使用循环改进读取

public class OutPutDemo21 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("F:\\itheima\\d.txt"));

        //读出数据
        String line;
        while ((line = br.readLine()) != null){
            System.out.println(line);
        }
        //释放资源
        br.close();
    }
}

readline为什么可以读取一整行?

可以读取一整行数据,一直读直到回车换行为止

但是它不会读取回车换行符

字符缓冲流操作文件中数据排序案例

  • 案例需求

    使用字符缓冲流读取文件中的数据,排序后再次写到本地文件

  • 实现步骤

    • 将文件中的数据读取到程序中
    • 对读取到的数据进行处理
    • 将处理后的数据添加到集合中
    • 对集合中的数据进行排序
    • 将排序后的集合中的数据写入到文件中
  • 代码实现

public class OutPutDemo22 {
    public static void main(String[] args) throws IOException {
        //1.需要将文件中的数据读进来
        BufferedReader br = new BufferedReader(new FileReader("F:\\itheima\\f.txt"));
        //读出数据
        int [] ints = {};
        String line;
        while ((line = br.readLine()) != null){
            String[] s = line.split(" ");
            //2.排序
            ints = sortList(s);
        }
        System.out.println(Arrays.toString(ints));
        //3.把排序后的结果写出
        BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\itheima\\g.txt"));
        bw.write(Arrays.toString(ints));
        //释放资源
        br.close();
        bw.close();
    }

    //数组冒泡排序方法
    private static int[] sortList(String[] s) {
        //定义外层循环
        int[] ints = StringToInt(s);
        for (int i = 0; i < ints.length - 1; i++) {
            //定义内层循环
            for (int j = 0; j < ints.length - i -1; j++) {
                if (ints[j] > ints[j + 1]){
                    int temp = ints[j];
                    ints[j] = ints[j+1];
                    ints[j+1] = temp;
                }
            }
        }
        return ints;
    }
    //String数组转为int
    public static int[] StringToInt(String[] arr){
        int[] array = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            array[i] = Integer.parseInt(arr[i]);
        }
        return array;
    }
}

转换流

  • InputStreamReader:是从字节流到字符流的桥梁,父类是Reader

    ​ 它读取字节,并使用指定的编码将其解码为字符

    ​ 它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集

  • OutputStreamWriter:是从字符流到字节流的桥梁,父类是Writer

    ​ 是从字符流到字节流的桥梁,使用指定的编码将写入的字符编码为字节

    ​ 它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集

转换流读写数据

  • 构造方法

    方法名 说明
    InputStreamReader(InputStream in) 使用默认字符编码创建InputStreamReader对象
    InputStreamReader(InputStream in,String chatset) 使用指定的字符编码创建InputStreamReader对象
    OutputStreamWriter(OutputStream out) 使用默认字符编码创建OutputStreamWriter对象
    OutputStreamWriter(OutputStream out,String charset) 使用指定的字符编码创建OutputStreamWriter对象

  • 代码演示
package com.cloudcore.iotest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

/**
 * @Author: hepeng
 * @Date: 2021/10/18 19:45
 */
public class OutPutDemo23 {
    public static void main(String[] args) throws IOException {
        //会产生乱码
        method1();
        //如何解决乱码?
        method2();
        
    }

    private static void method2() throws IOException {
        InputStreamReader is = new InputStreamReader(new FileInputStream("F:\\itheima\\a.txt"),"UTF-8");
        int ch;
        while ((ch = is.read()) != -1){
            System.out.println((char)ch);
        }
        is.close();
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("F:\\itheima\\aa.txt"),"UTF-8");
        out.write("我爱学习谁也别打扰我");
        out.close();
    }

    private static void method1() throws IOException {
        FileReader fr = new FileReader("F:\\itheima\\a.txt");
        int ch;
        while ((ch = fr.read()) != -1){
            System.out.println((char)ch);
        }
        fr.close();
    }
}

对象操作流

对象序列化流

  • 对象序列化介绍

    • 对象序列化:就是将对象保存到磁盘中,或者在网络中传输对象
    • 这种机制就是使用一个字节序列表示一个对象,该字节序列包含:对象的类型、对象的数据和对象中存储的属性等信息
    • 字节序列写到文件之后,相当于文件中持久保存了一个对象的信息
    • 反之,该字节序列还可以从文件中读取回来,重构对象,对它进行反序列化
  • 对象序列化流: ObjectOutputStream

    • 将Java对象的原始数据类型和图形写入OutputStream。 可以使用ObjectInputStream读取(重构)对象。 可以通过使用流的文件来实现对象的持久存储。 如果流是网络套接字流,则可以在另一个主机上或另一个进程中重构对象
  • 构造方法

    方法名 说明
    ObjectOutputStream(OutputStream out) 创建一个写入指定的OutputStream的ObjectOutputStream
  • 序列化对象的方法

    方法名 说明
    void writeObject(Object obj) 将指定的对象写入ObjectOutputStream
  • 示例代码

    学生类

public class ObjectOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        //ObjectOutputStream(OutputStream out):创建一个写入指定的OutputStream的ObjectOutputStream
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myOtherStream\\oos.txt"));

        //创建对象
        Student s = new Student("佟丽娅",30);

        //void writeObject(Object obj):将指定的对象写入ObjectOutputStream
        oos.writeObject(s);

        //释放资源
        oos.close();
    }
}

注意事项

  • 一个对象要想被序列化,该对象所属的类必须必须实现Serializable 接口
  • Serializable是一个标记接口,实现该接口,不需要重写任何方法

对象反序列化流

  • 对象反序列化流: ObjectInputStream

    • ObjectInputStream反序列化先前使用ObjectOutputStream编写的原始数据和对象
  • 构造方法

    方法名 说明
    ObjectInputStream(InputStream in) 创建从指定的InputStream读取的ObjectInputStream
  • 反序列化对象的方法

    方法名 说明
    Object readObject() 从ObjectInputStream读取一个对象
  • 示例代码

public class ObjectInputStreamDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //ObjectInputStream(InputStream in):创建从指定的InputStream读取的ObjectInputStream
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("myOtherStream\\oos.txt"));

        //Object readObject():从ObjectInputStream读取一个对象
        Object obj = ois.readObject();

        Student s = (Student) obj;
        System.out.println(s.getName() + "," + s.getAge());

        ois.close();
    }
}

serialVersionUID&transient

  • serialVersionUID

    • 用对象序列化流序列化了一个对象后,假如我们修改了对象所属的类文件,读取数据会不会出问题呢?
      • 会出问题,会抛出InvalidClassException异常
    • 如果出问题了,如何解决呢?
      • 重新序列化
      • 给对象所属的类加一个serialVersionUID
        • private static final long serialVersionUID = 42L;
  • transient

    • 如果一个对象中的某个成员变量的值不想被序列化,又该如何实现呢?
      • 给该成员变量加transient关键字修饰,该关键字标记的成员变量不参与序列化过程
  • 示例代码

    学生类

public class Student implements Serializable {
    private static final long serialVersionUID = 42L;
    private String name;
//    private int age;
    private transient int age;

    public Student() {
    }

    public Student(String name, int age) {
        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;
    }
}

Properties集合

  • Properties介绍

    • 是一个Map体系的集合类
    • Properties可以保存到流中或从流中加载
    • 属性列表中的每个键及其对应的值都是一个字符串
  • Properties基本使用

package com.cloudcore.iotest;

import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * @Author: hepeng
 * @Date: 2021/10/19 0:01
 */
public class properties {
    public static void main(String[] args) {
        Properties prop = new Properties();
        //增
        prop.put("小龙女","尹志平");
        prop.put("郭襄","杨过");
        prop.put("黄蓉","欧阳克");
        System.out.println(prop);
        //删
        prop.remove("小龙女");
        System.out.println(prop);
        //改
        prop.put("黄蓉","小沈阳");
        System.out.println(prop);
        //查
        Object value = prop.get("郭襄");
        System.out.println(value);
        //遍历
        Set<Object> keys = prop.keySet();
        for (Object key : keys) {
            Object o = prop.get(key);
            System.out.println(key + "=" + o);
        }

        Set<Map.Entry<Object, Object>> entries = prop.entrySet();
        for (Map.Entry<Object, Object> entry : entries) {
            Object key = entry.getKey();
            Object value1 = entry.getValue();
            System.out.println(key + "=" + value1);
        }
    }
}

Properties作为Map集合的特有方法

package com.cloudcore.iotest;

import java.util.Properties;
import java.util.Set;

/**
 * @Author: hepeng
 * @Date: 2021/10/19 0:08
 */
public class propertiesTest {
    public static void main(String[] args) {
        Properties prop = new Properties();
        prop.setProperty("山东","济南");
        prop.setProperty("浙江","杭州");
        prop.setProperty("山西","太原");
        prop.setProperty("安徽","合肥");
        System.out.println(prop);

        String key = prop.getProperty("浙江");
        System.out.println(key);

        Set<String> keys = prop.stringPropertyNames();
        for (String s : keys) {
            Object value = prop.get(s);
            System.out.println(s + "=" + value);
        }
    }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM