• java学习day17(Java核心类库)IO流


    17.1 IO 流的概念
    IO 就是 Input Output 的简写,也就是输入和输出的含义。
    IO 流就是指读写数据时像流水一样从一端流到另外一端,因此得名为 "
    17.2 基本分类

     按照读写数据的基本单位不同,分为 字节流 和 字符流。

    其中字节流主要指以字节为单位进行数据读写的流,可以读写任意类型的文件。
    其中字符流主要指以字符 (2 个字节 ) 为单位进行数据读写的流,只能读写文本文件。
    按照读写数据的方向不同,分为 输入流 和 输出流(站在程序的角度)。
    其中输入流主要指从文件中读取数据内容输入到程序中,也就是读文件。
    其中输出流主要指将程序中的数据内容输出到文件中,也就是写文件。
    按照流的角色不同分为节点流和处理流。
    其中节点流主要指直接和输入输出源对接的流。
    其中处理流主要指需要建立在节点流的基础之上的流。

     17.3 体系结构

     

    17.4 相关流的详解

    17.4.1 FileWriter 类(重点)
    1 )基本概念
    java.io.FileWriter 类主要用于将文本内容写入到文本文件。
    2 )常用的方法
    方法声明
    功能介绍
    FileWriter(String fileName)
    根据参数指定的文件名构造对象
    FileWriter(String fileName, boolean
    append)
    以追加的方式根据参数指定的文件名来构造对象
    void write(int c)
    写入单个字符
    void write(char[] cbuf, int off, int len)
    将指定字符数组中从偏移量 offff 开始的 len 个字符写入此
    文件输出流
    void write(char[] cbuf)
    cbuf.length 个字符从指定字符数组写入此文件输出
    流中
    void flush()
    刷新流
    void close()
    关闭流对象并释放有关的资源
    1. package com.lagou.task17;
    2. import java.io.FileWriter;
    3. import java.io.IOException;
    4. public class FileWriterTest {
    5. public static void main(String[] args) {
    6. // 选中代码后可以使用 ctrl+alt+t 来生成异常的捕获代码等
    7. FileWriter fw = null;
    8. try {
    9. // 1.构造FileWrite类型的对象与d:/a.txt文件关联
    10. // 若文件不存在,该流会自动创建新的空文件
    11. // 若文件存在,该流会清空文件中的原有内容
    12. fw = new FileWriter("d:/a.txt");
    13. // 以追加的方式创建对象去关联文件
    14. // 若文件不存在则自动创建新的空文件,若文件存在则保留原有数据内容
    15. //fw = new FileWriter("d:/a.txt", true);
    16. // 2.通过流对象写入数据内容 每当写入一个字符后则文件中的读写位置向后移动一位
    17. fw.write('a');
    18. // 准备一个字符数组
    19. char[] cArr = new char[]{'h', 'e', 'l', 'l', 'o'};
    20. // 将字符数组中的一部分内容写入进去
    21. fw.write(cArr, 1, 3); // ell
    22. // 将整个字符数组写进去
    23. fw.write(cArr); // hello
    24. // 刷新流
    25. fw.flush();
    26. System.out.println("写入数据成功!");
    27. } catch (IOException e) {
    28. e.printStackTrace();
    29. } finally {
    30. // 3.关闭流对象并释放有关的资源
    31. if (null != fw) {
    32. try {
    33. fw.close();
    34. } catch (IOException e) {
    35. e.printStackTrace();
    36. }
    37. }
    38. }
    39. }
    40. }

    17.4.2 FileReader类(重点)

    1 )基本概念
    java.io.FileReader 类主要用于从文本文件读取文本数据内容。
    2 )常用的方法
    方法声明
    功能介绍
    FileReader(String fileName)
    根据参数指定的文件名构造对象
    int read()
    读取单个字符的数据并返回,返回 -1 表示读取到末尾
    int read(char[] cbuf, int
    offset, int length)
    从输入流中将最多 len 个字符的数据读入一个字符数组中,返回读取
    到的字符个数,返回 -1 表示读取到末尾
    int read(char[] cbuf)
    从此输入流中将最多 cbuf.length 个字符的数据读入字符数组中,返
    回读取到的字符个数,返回 -1 表示读取到末尾
    void close()
    关闭流对象并释放有关的资源
    1. package com.lagou.task17;
    2. import java.io.FileReader;
    3. import java.io.IOException;
    4. public class FileReaderTest {
    5. public static void main(String[] args) {
    6. FileReader fr = null;
    7. try {
    8. // 1.构造FileReader类型的对象与d:/a.txt文件关联
    9. //fr = new FileReader("d:/a.txt");
    10. fr = new FileReader("d:/b.txt");
    11. // 2.读取数据内容并打印
    12. /*
    13. int res = fr.read();
    14. System.out.println("读取到的单个字符是:" + (char)res); // 'a'
    15. */
    16. int res = 0;
    17. while ((res = fr.read()) != -1) {
    18. System.out.println("读取到的单个字符是:" + (char)res + ",对应的编号是:" + res);
    19. }
    20. // 准备一个字符数组来保存读取到的数据内容
    21. // char[] cArr = new char[5];
    22. // 期望读满字符数组中的一部分空间,也就是读取3个字符放入数组cArr中下标从1开始的位置上
    23. /*int res = fr.read(cArr, 1, 3);
    24. System.out.println("实际读取到的字符个数是:" + res); // 3
    25. for (char cv : cArr) {
    26. System.out.println("读取到的单个字符是:" + (char)cv); // 啥也没有 a e l 啥也没有
    27. }*/
    28. // 期望读满整个字符数组
    29. /*int res = fr.read(cArr);
    30. System.out.println("实际读取到的字符个数是:" + res); // 5
    31. for (char cv : cArr) {
    32. System.out.println("读取到的单个字符是:" + (char)cv); // a e l l h
    33. }*/
    34. } catch (IOException e) {
    35. e.printStackTrace();
    36. } finally {
    37. // 3.关闭流对象并释放有关的资源
    38. if (null != fr) {
    39. try {
    40. fr.close();
    41. } catch (IOException e) {
    42. e.printStackTrace();
    43. }
    44. }
    45. }
    46. }
    47. }

    17.4.3 FileOutputStream类(重点)

    1 )基本概念
    java.io.FileOutputStream 类主要用于将图像数据之类的原始字节流写入到输出流中。
    2 )常用的方法
    方法声明
    功能介绍
    FileOutputStream(String name)
    根据参数指定的文件名来构造对象
    FileOutputStream(String name,
    boolean append)
    以追加的方式根据参数指定的文件名来构造对象
    void write(int b)
    将指定字节写入此文件输出流
    void write(byte[] b, int offff, int len)
    将指定字节数组中从偏移量 offff 开始的 len 个字节写入
    此文件输出流
    void write(byte[] b)
    b.length 个字节从指定字节数组写入此文件输出
    流中
    void flflush()
    刷新此输出流并强制写出任何缓冲的输出字节
    void close()
    关闭流对象并释放有关的资源
    17.4.4 FileInputStream 类(重点)
    1 )基本概念
    java.io.FileInputStream 类主要用于从输入流中以字节流的方式读取图像数据等。
    2 )常用的方法
    方法声明
    功能介绍
    FileInputStream(String
    name)
    根据参数指定的文件路径名来构造对象
    int read()
    从输入流中读取单个字节的数据并返回,返回 -1 表示读取到末尾
    int read(byte[] b, int
    off, int len)
    从此输入流中将最多 len 个字节的数据读入字节数组中,返回读取到的
    字节个数,返回 -1 表示读取到末尾
    int read(byte[] b)
    从此输入流中将最多 b.length 个字节的数据读入字节数组中,返回读
    取到的字节个数,返回 -1 表示读取到末尾
    void close()
    关闭流对象并释放有关的资源
    int available()
    获取输入流所关联文件的大小
    1. package com.lagou.task17;
    2. import java.io.FileInputStream;
    3. import java.io.FileOutputStream;
    4. import java.io.IOException;
    5. public class FileByteCopyTest {
    6. public static void main(String[] args) {
    7. // 获取当前系统时间距离1970年1月1日0时0分0秒的毫秒数
    8. long g1 = System.currentTimeMillis();
    9. FileInputStream fis = null;
    10. FileOutputStream fos = null;
    11. try {
    12. // 1.创建FileInputStream类型的对象与d:/03 IO流的框架图.png文件关联
    13. //fis = new FileInputStream("d:/03 IO流的框架图.png");
    14. fis = new FileInputStream("d:/02_IO流的框架结构.mp4");
    15. // 2.创建FileOutputStream类型的对象与d:/IO流的框架图.png文件关联
    16. //fos = new FileOutputStream("d:/IO流的框架图.png");
    17. fos = new FileOutputStream("d:/IO流的框架结构.mp4");
    18. // 3.不断地从输入流中读取数据内容并写入到输出流中
    19. System.out.println("正在玩命地拷贝...");
    20. // 方式一:以单个字节为单位进行拷贝,也就是每次读取一个字节后再写入一个字节
    21. // 缺点:文件稍大时,拷贝的效率很低
    22. /*int res = 0;
    23. while ((res = fis.read()) != -1) {
    24. fos.write(res);
    25. }*/
    26. // 方式二:准备一个和文件大小一样的缓冲区,一次性将文件中的所有内容取出到缓冲区然后一次性写入进去
    27. // 缺点:若文件过大时,无法申请和文件大小一样的缓冲区,真实物理内存不足
    28. /*int len = fis.available();
    29. System.out.println("获取到的文件大小是:" + len);
    30. byte[] bArr = new byte[len];
    31. int res = fis.read(bArr);
    32. System.out.println("实际读取到的文件大小是:" + res);
    33. fos.write(bArr);*/
    34. // 方式三:准备一个相对适当的缓冲区,分多次将文件拷贝完成
    35. byte[] bArr = new byte[1024];
    36. int res = 0;
    37. while ((res = fis.read(bArr)) != -1) {
    38. fos.write(bArr, 0, res);
    39. }
    40. System.out.println("拷贝文件成功!");
    41. } catch (IOException e) {
    42. e.printStackTrace();
    43. } finally {
    44. // 4.关闭流对象并释放有关的资源
    45. if (null != fos) {
    46. try {
    47. fos.close();
    48. } catch (IOException e) {
    49. e.printStackTrace();
    50. }
    51. }
    52. if (null != fis) {
    53. try {
    54. fis.close();
    55. } catch (IOException e) {
    56. e.printStackTrace();
    57. }
    58. }
    59. }
    60. long g2 = System.currentTimeMillis();
    61. System.out.println("使用文件流拷贝视频文件消耗的时间为:" + (g2-g1)); // 165
    62. }
    63. }
    案例题目
    编程实现两个文件之间的拷贝功能。
    1. package com.lagou.task17;
    2. import java.io.FileReader;
    3. import java.io.FileWriter;
    4. import java.io.IOException;
    5. public class FileCharCopyTest {
    6. public static void main(String[] args) {
    7. FileReader fr = null;
    8. FileWriter fw = null;
    9. try {
    10. // 1.创建FileReader类型的对象与d:/a.txt文件关联
    11. fr = new FileReader("d:/a.txt");
    12. //fr = new FileReader("d:/03 IO流的框架图.png");
    13. // 2.创建FileWriter类型的对象与d:/b.txt文件关联
    14. fw = new FileWriter("d:/b.txt");
    15. //fw = new FileWriter("d:/IO流的框架图.png"); 拷贝图片文件失败!!!
    16. // 3.不断地从输入流中读取数据内容并写入到输出流中
    17. System.out.println("正在玩命地拷贝...");
    18. int res = 0;
    19. while ((res = fr.read()) != -1) {
    20. fw.write(res);
    21. }
    22. System.out.println("拷贝文件成功!");
    23. } catch (IOException e) {
    24. e.printStackTrace();
    25. } finally {
    26. // 4.关闭流对象并释放有关的资源
    27. if (null != fw) {
    28. try {
    29. fw.close();
    30. } catch (IOException e) {
    31. e.printStackTrace();
    32. }
    33. }
    34. if (null != fr) {
    35. try {
    36. fr.close();
    37. } catch (IOException e) {
    38. e.printStackTrace();
    39. }
    40. }
    41. }
    42. }
    43. }
    17.4.5 BufferedOutputStream 类(重点)
    1 )基本概念
    java.io.BufferedOutputStream 类主要用于描述缓冲输出流,此时不用为写入的每个字节调用底层
    系统。
    2 )常用的方法
    方法声明
    功能介绍
    BufferedOutputStream(OutputStream out)
    根据参数指定的引用来构造对象
    BufferedOutputStream(OutputStream out, int
    size)
    根据参数指定的引用和缓冲区大小来构造
    对象
    void write(int b)
    写入单个字节
    void write(byte[] b, int offff, int len)
    写入字节数组中的一部分数据
    void write(byte[] b)
    写入参数指定的整个字节数组
    void flush()
    刷新流
    void close()
    关闭流对象并释放有关的资源
    1. package com.lagou.task17;
    2. import java.io.*;
    3. public class BufferedByteCopyTest {
    4. public static void main(String[] args) {
    5. // 获取当前系统时间距离1970年1月1日0时0分0秒的毫秒数
    6. long g1 = System.currentTimeMillis();
    7. BufferedInputStream bis = null;
    8. BufferedOutputStream bos = null;
    9. try {
    10. // 1.创建BufferedInputStream类型的对象与d:/02_IO流的框架结构.mp4文件关联
    11. bis = new BufferedInputStream(new FileInputStream("d:/02_IO流的框架结构.mp4"));
    12. // 2.创建BufferedOuputStream类型的对象与d:/IO流的框架结构.mp4文件关联
    13. bos = new BufferedOutputStream(new FileOutputStream("d:/IO流的框架结构.mp4"));
    14. // 3.不断地从输入流中读取数据并写入到输出流中
    15. System.out.println("正在玩命地拷贝...");
    16. byte[] bArr = new byte[1024];
    17. int res = 0;
    18. while ((res = bis.read(bArr)) != -1) {
    19. bos.write(bArr, 0, res);
    20. }
    21. System.out.println("拷贝文件成功!");
    22. } catch (IOException e) {
    23. e.printStackTrace();
    24. } finally {
    25. // 4.关闭流对象并释放有关的资源
    26. if (null != bos) {
    27. try {
    28. bos.close();
    29. } catch (IOException e) {
    30. e.printStackTrace();
    31. }
    32. }
    33. if (null != bis) {
    34. try {
    35. bis.close();
    36. } catch (IOException e) {
    37. e.printStackTrace();
    38. }
    39. }
    40. }
    41. long g2 = System.currentTimeMillis();
    42. System.out.println("使用缓冲区拷贝视频文件消耗的时间为:" + (g2-g1)); // 44
    43. }
    44. }
    17.4.6 BufferedInputStream 类(重点)
    1 )基本概念
    java.io.BufferedInputStream 类主要用于描述缓冲输入流。
    2 )常用的方法
    方法声明
    功能介绍
    BufferedInputStream(InputStream in)
    根据参数指定的引用构造对象
    BufferedInputStream(InputStream in, int size)
    根据参数指定的引用和缓冲区大小构造对象
    int read()
    读取单个字节
    int read(byte[] b, int off, int len)
    读取 len 个字节
    int read(byte[] b)
    读取 b.length 个字节
    void close()
    关闭流对象并释放有关的资源
    17.4.7 BufferedWriter 类(重点)
    1 )基本概念
    java.io.BufferedWriter 类主要用于写入单个字符、字符数组以及字符串到输出流中。
    2 )常用的方法
    方法声明
    功能介绍
    BufferedWriter(Writer out)
    根据参数指定的引用来构造对象
    BufferedWriter(Writer out, int sz)
    根据参数指定的引用和缓冲区大小来构造对象
    void write(int c)
    写入单个字符到输出流中
    void write(char[] cbuf, int off, int
    len)
    将字符数组 cbuf 中从下标 off 开始的 len 个字符写入输出流
    void write(char[] cbuf)
    将字符串数组 cbuf 中所有内容写入输出流中
    void write(String s, int off, int len)
    将参数 s 中下标从 offff 开始的 len 个字符写入输出流中
    void write(String str)
    将参数指定的字符串内容写入输出流中
    void newLine()
    用于写入行分隔符到输出流中
    void flush()
    刷新流
    void close()
    关闭流对象并释放有关的资源
    17.4.8 BufferedReader 类(重点)
    1 )基本概念
    java.io.BufferedReader 类用于从输入流中读取单个字符、字符数组以及字符串。
    2 )常用的方法
    方法声明
    功能介绍
    BufferedReader(Reader
    in)
    根据参数指定的引用来构造对象
    BufferedReader(Reader
    in, int sz)
    根据参数指定的引用和缓冲区大小来构造对象
    int read()
    从输入流读取单个字符,读取到末尾则返回 -1 ,否则返回实际读取到
    的字符内容
    int read(char[] cbuf, int
    off, int len)
    从输入流中读取 len 个字符放入数组 cbuf 中下标从 offff 开始的位置上,
    若读取到末尾则返回 -1 ,否则返回实际读取到的字符个数
    int read(char[] cbuf)
    从输入流中读满整个数组 cbuf
    String readLine()
    读取一行字符串并返回,返回 null 表示读取到末尾
    void close()
    关闭流对象并释放有关的资源
    1. package com.lagou.task17;
    2. import java.io.*;
    3. public class BufferedCharCopyTest {
    4. public static void main(String[] args) {
    5. BufferedReader br = null;
    6. BufferedWriter bw = null;
    7. try {
    8. // 1.创建BufferedReader类型的对象与d:/a.txt文件关联
    9. br = new BufferedReader(new FileReader("d:/a.txt"));
    10. // 2.创建BufferedWriter类型的对象与d:/b.txt文件关联
    11. bw = new BufferedWriter(new FileWriter("d:/b.txt"));
    12. // 3.不断地从输入流中读取一行字符串并写入到输出流中
    13. System.out.println("正在玩命地拷贝...");
    14. String str = null;
    15. while ((str = br.readLine()) != null) {
    16. bw.write(str);
    17. bw.newLine(); // 当前系统中的行分隔符是:\r\n
    18. }
    19. System.out.println("拷贝文件成功!");
    20. } catch (IOException e) {
    21. e.printStackTrace();
    22. } finally {
    23. // 4.关闭流对象并释放有关的资源
    24. if (null != bw) {
    25. try {
    26. bw.close();
    27. } catch (IOException e) {
    28. e.printStackTrace();
    29. }
    30. }
    31. if (null != br) {
    32. try {
    33. br.close();
    34. } catch (IOException e) {
    35. e.printStackTrace();
    36. }
    37. }
    38. }
    39. }
    40. }
    17.4.9 PrintStream
    1 )基本概念
    java.io.PrintStream 类主要用于更加方便地打印各种数据内容。
    2 )常用的方法
    方法声明
    功能介绍
    PrintStream(OutputStream out)
    根据参数指定的引用来构造对象
    void print(String s)
    用于将参数指定的字符串内容打印出来
    void println(String x)
    用于打印字符串后并终止该行
    void flush()
    刷新流
    void close()
    用于关闭输出流并释放有关的资源
    1. package com.lagou.task17;
    2. import java.io.*;
    3. import java.text.SimpleDateFormat;
    4. import java.util.Date;
    5. public class PrintStreamChatTest {
    6. public static void main(String[] args) {
    7. // 由手册可知:构造方法需要的是Reader类型的引用,但Reader类是个抽象类,实参只能传递子类的对象 字符流
    8. // 由手册可知: System.in代表键盘输入, 而且是InputStream类型的 字节流
    9. BufferedReader br = null;
    10. PrintStream ps = null;
    11. try {
    12. br = new BufferedReader(new InputStreamReader(System.in));
    13. ps = new PrintStream(new FileOutputStream("d:/a.txt", true));
    14. // 声明一个boolean类型的变量作为发送方的代表
    15. boolean flag = true;
    16. while(true) {
    17. // 1.提示用户输入要发送的聊天内容并使用变量记录
    18. System.out.println("请" + (flag? "张三": "李四") + "输入要发送的聊天内容:");
    19. String str = br.readLine();
    20. // 2.判断用户输入的内容是否为"bye",若是则聊天结束
    21. if ("bye".equals(str)) {
    22. System.out.println("聊天结束!");
    23. break;
    24. }
    25. // 3.若不是则将用户输入的内容写入到文件d:/a.txt中
    26. //else {
    27. // 获取当前系统时间并调整格式
    28. Date d1 = new Date();
    29. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    30. ps.println(sdf.format(d1) + (flag?" 张三说:":" 李四说:") + str);
    31. //}
    32. flag = !flag;
    33. }
    34. ps.println(); // 写入空行 与之前的聊天记录隔开
    35. ps.println();
    36. ps.println();
    37. } catch (IOException e) {
    38. e.printStackTrace();
    39. } finally {
    40. // 4.关闭流对象并释放有关的资源
    41. if (null != ps) {
    42. ps.close();
    43. }
    44. if (null != br) {
    45. try {
    46. br.close();
    47. } catch (IOException e) {
    48. e.printStackTrace();
    49. }
    50. }
    51. }
    52. }
    53. }

    17.4.10 PrintWriter

    1 )基本概念
    java.io.PrintWriter 类主要用于将对象的格式化形式打印到文本输出流。
    2 )常用的方法
    方法声明
    功能介绍
    PrintWriter(Writer out)
    根据参数指定的引用来构造对象
    void print(String s)
    将参数指定的字符串内容打印出来
    void println(String x)
    打印字符串后并终止该行
    void flush()
    刷新流
    void close()
    关闭流对象并释放有关的资源
    案例题目
    不断地提示用户输入要发送的内容,若发送的内容是 "bye" 则聊天结束,否则将用户输入的内容写
    入到文件 d:/a.txt 中。
    要求使用 BufferedReader 类来读取键盘的输入 System.in 代表键盘输入
    要求使用 PrintStream 类负责将数据写入文件
    17.4.11 OutputStreamWriter
    1 )基本概念
    java.io.OutputStreamWriter 类主要用于实现从字符流到字节流的转换。
    2 )常用的方法
    方法声明
    功能介绍
    OutputStreamWriter(OutputStream out)
    根据参数指定的引用来构造对象
    OutputStreamWriter(OutputStream out, String
    charsetName)
    根据参数指定的引用和编码构造
    对象
    void write(String str)
    将参数指定的字符串写入
    void flush()
    刷新流
    void close()
    用于关闭输出流并释放有关的资
    17.4.12 InputStreamReader
    1 )基本概念
    java.io.InputStreamReader 类主要用于实现从字节流到字符流的转换。
    2 )常用的方法
    方法声明
    功能介绍
    InputStreamReader(InputStream in)
    根据参数指定的引用来构造对象
    InputStreamReader(InputStream in, String
    charsetName)
    根据参数指定的引用和编码来构造对
    int read(char[] cbuf)
    读取字符数据到参数指定的数组
    void close()
    用于关闭输出流并释放有关的资源
    17.4.13 字符编码
    1 )编码表的由来
    计算机只能识别二进制数据,早期就是电信号。为了方便计算机可以识别各个国家的文字,就需要
    将各个国家的文字采用数字编号的方式进行描述并建立对应的关系表,该表就叫做编码表。
    2 )常见的编码表
    ASCII :美国标准信息交换码,
    使用一个字节的低 7 位二位进制进行表示。
    ISO8859-1 :拉丁码表,欧洲码表,使用一个字节的 8 位二进制进行表示。
    GB2312 :中国的中文编码表,最多使用两个字节 16 位二进制为进行表示。
    GBK :中国的中文编码表升级,融合了更多的中文文字符号,最多使用两个字节 16 位二进制位表
    示。
    Unicode :国际标准码,融合了目前人类使用的所有字符,为每个字符分配唯一的字符码。所有的
    文字都用两个字节 16 位二进制位来表示。
    3 )编码的发展
    面向传输的众多 UTF UCS Transfer Format )标准出现了, UTF-8 就是每次 8 个位传输数据,而
    UTF-16 就是每次 16 个位。这是为传输而设计的编码并使编码无国界,这样就可以显示全世界上所
    有文化的字符了。
    Unicode 只是定义了一个庞大的、全球通用的字符集,并为每个字符规定了唯一确定的编号,具体
    存储成什么样的字节流,取决于字符编码方案。推荐的 Unicode 编码是 UTF-8 UTF-16
    UTF-8 :变长的编码方式,可用 1-4 个字节来表示一个字符。
    17.4.14 DataOutputStream 类(了解)
    1 )基本概念
    java.io.DataOutputStream 类主要用于以适当的方式将基本数据类型写入输出流中。
    2 )常用的方法
    方法声明
    功能介绍
    DataOutputStream(OutputStream
    out)
    根据参数指定的引用构造对象 OutputStream 类是个抽象
    类,实参需要传递子类对象
    void writeInt(int v)
    用于将参数指定的整数一次性写入输出流,优先写入高字
    void close()
    用于关闭文件输出流并释放有关的资源
    1. package com.lagou.task17;
    2. import java.io.DataOutputStream;
    3. import java.io.FileOutputStream;
    4. import java.io.IOException;
    5. public class DataOutputStreamTest {
    6. public static void main(String[] args) {
    7. DataOutputStream dos = null;
    8. try {
    9. // 1.创建DataOutputStream类型的对象与d:/a.txt文件关联
    10. dos = new DataOutputStream(new FileOutputStream("d:/a.txt"));
    11. // 2.准备一个整数数据66并写入输出流
    12. // 66: 0000 0000 ... 0100 0010 => B
    13. int num = 66;
    14. //dos.writeInt(num); // 写入4个字节
    15. dos.write(num); // 写入1个字节
    16. System.out.println("写入数据成功!");
    17. } catch (IOException e) {
    18. e.printStackTrace();
    19. } finally {
    20. // 3.关闭流对象并释放有关的资源
    21. if (null != dos) {
    22. try {
    23. dos.close();
    24. } catch (IOException e) {
    25. e.printStackTrace();
    26. }
    27. }
    28. }
    29. }
    30. }

    17.4.15 DataInputStream类(了解)

    1 )基本概念
    java.io.DataInputStream 类主要用于从输入流中读取基本数据类型的数据。
    2 )常用的方法
    方法声明
    功能介绍
    DataOutputStream(OutputStream
    out)
    根据参数指定的引用构造对象 OutputStream 类是个抽象
    类,实参需要传递子类对象
    void writeInt(int v)
    用于将参数指定的整数一次性写入输出流,优先写入高字
    void close()
    用于关闭文件输出流并释放有关的资源
    1. package com.lagou.task17;
    2. import java.io.DataInputStream;
    3. import java.io.FileInputStream;
    4. import java.io.IOException;
    5. public class DataInputStreamTest {
    6. public static void main(String[] args) {
    7. DataInputStream dis = null;
    8. try {
    9. // 1.创建DataInputStream类型的对象与d:/a.txt文件关联
    10. dis = new DataInputStream(new FileInputStream("d:/a.txt"));
    11. // 2.从输入流中读取一个整数并打印
    12. //int res = dis.readInt(); // 读取4个字节
    13. int res = dis.read(); // 读取1个字节
    14. System.out.println("读取到的整数数据是:" + res); // 66
    15. } catch (IOException e) {
    16. e.printStackTrace();
    17. } finally {
    18. // 3.关闭流对象并释放有关的资源
    19. if (null != dis) {
    20. try {
    21. dis.close();
    22. } catch (IOException e) {
    23. e.printStackTrace();
    24. }
    25. }
    26. }
    27. }
    28. }

    17.4.15 DataInputStream类(了解)

    1 )基本概念
    java.io.DataInputStream 类主要用于从输入流中读取基本数据类型的数据。
    2 )常用的方法
    方法声明
    功能介绍
    DataInputStream(InputStream
    in)
    根据参数指定的引用来构造对象 InputStream 类是抽象类,
    实参需要传递子类对象
    int readInt()
    用于从输入流中一次性读取一个整数数据并返回
    void close()
    用于关闭文件输出流并释放有关的资源
    17.4.16 ObjectOutputStream 类(重点)
    1 )基本概念
    java.io.ObjectOutputStream 类主要用于将一个对象的所有内容整体写入到输出流中。
    只能将支持 java.io.Serializable 接口的对象写入流中。
    类通过实现 java.io.Serializable 接口以启用其序列化功能。
    所谓序列化主要指将一个对象需要存储的相关信息有效组织成字节序列的转化过程。
    2 )常用的方法
    方法声明
    功能介绍
    ObjectOutputStream(OutputStream out)
    根据参数指定的引用来构造对象
    void writeObject(Object obj)
    用于将参数指定的对象整体写入到输出流中
    void close()
    用于关闭输出流并释放有关的资源
    1. package com.lagou.task17;
    2. public class User implements java.io.Serializable {
    3. private static final long serialVersionUID = -5814716593800822421L;
    4. private String userName; // 用户名
    5. private String password; // 密码
    6. private transient String phoneNum; // 手机号 表示该成员变量不参与序列化操作
    7. public User() {
    8. }
    9. public User(String userName, String password, String phoneNum) {
    10. this.userName = userName;
    11. this.password = password;
    12. this.phoneNum = phoneNum;
    13. }
    14. public String getUserName() {
    15. return userName;
    16. }
    17. public void setUserName(String userName) {
    18. this.userName = userName;
    19. }
    20. public String getPassword() {
    21. return password;
    22. }
    23. public void setPassword(String password) {
    24. this.password = password;
    25. }
    26. public String getPhoneNum() {
    27. return phoneNum;
    28. }
    29. public void setPhoneNum(String phoneNum) {
    30. this.phoneNum = phoneNum;
    31. }
    32. @Override
    33. public String toString() {
    34. return "User{" +
    35. "userName='" + userName + '\'' +
    36. ", password='" + password + '\'' +
    37. ", phoneNum='" + phoneNum + '\'' +
    38. '}';
    39. }
    40. }
    1. package com.lagou.task17;
    2. import java.io.FileOutputStream;
    3. import java.io.IOException;
    4. import java.io.ObjectOutputStream;
    5. public class ObjectOutputStreamTest {
    6. public static void main(String[] args) {
    7. ObjectOutputStream oos = null;
    8. try {
    9. // 1.创建ObjectOutputStream类型的对象与d:/a.txt文件关联
    10. oos = new ObjectOutputStream(new FileOutputStream("d:/a.txt"));
    11. // 2.准备一个User类型的对象并初始化
    12. User user = new User("qidian", "123456", "13511258688");
    13. // 3.将整个User类型的对象写入输出流
    14. oos.writeObject(user);
    15. System.out.println("写入对象成功!");
    16. } catch (IOException e) {
    17. e.printStackTrace();
    18. } finally {
    19. // 4.关闭流对象并释放有关的资源
    20. if (null != oos) {
    21. try {
    22. oos.close();
    23. } catch (IOException e) {
    24. e.printStackTrace();
    25. }
    26. }
    27. }
    28. }
    29. }

    17.4.17 ObjectInputStream类(重点)

    1 )基本概念
    java.io.ObjectInputStream 类主要用于从输入流中一次性将对象整体读取出来。
    所谓反序列化主要指将有效组织的字节序列恢复为一个对象及相关信息的转化过程。
    2 )常用的方法
    方法声明
    功能介绍
    ObjectInputStream(InputStream in)
    根据参数指定的引用来构造对象
    Object readObject()
    主要用于从输入流中读取一个对象并返回 无法通过返回值
    来判断是否读取到文件的末尾
    void close()
    用于关闭输入流并释放有关的资源
    1. package com.lagou.task17;
    2. import java.io.FileInputStream;
    3. import java.io.IOException;
    4. import java.io.ObjectInputStream;
    5. public class ObjectInputStreamTest {
    6. public static void main(String[] args) {
    7. ObjectInputStream ois = null;
    8. try {
    9. // 1.创建ObjectInputStream类型的对象与d:/a.txt文件关联
    10. ois = new ObjectInputStream(new FileInputStream("d:/a.txt"));
    11. // 2.从输入流中读取一个对象并打印
    12. Object obj = ois.readObject();
    13. System.out.println("读取到的对象是:" + obj); // qidian 123456 13511258688 null
    14. } catch (IOException e) {
    15. e.printStackTrace();
    16. } catch (ClassNotFoundException e) {
    17. e.printStackTrace();
    18. } finally {
    19. // 3.关闭流对象并释放有关的资源
    20. if (null != ois) {
    21. try {
    22. ois.close();
    23. } catch (IOException e) {
    24. e.printStackTrace();
    25. }
    26. }
    27. }
    28. }
    29. }

    3)序列化版本号

    序列化机制是通过在运行时判断类的 serialVersionUID 来验证版本一致性的。在进行反序列化时,
    JVM 会把传来的字节流中的 serialVersionUID 与本地相应实体类的 serialVersionUID 进行比较,如
    果相同就认为是一致的,可以进行反序列化,否则就会出现序列化版本不一致的异常
    (InvalidCastException)
    4 transient 关键字
    transient Java 语言的关键字,用来表示一个域不是该对象串行化的一部分。当一个对象被串行
    化的时候, transient 型变量的值不包括在串行化的表示中,然而非 transient 型的变量是被包括进
    去的。
    5 )经验的分享
    当希望将多个对象写入文件时,通常建议将多个对象放入一个集合中,然后将集合这个整体看做一
    个对象写入输出流中,此时只需要调用一次 readObject 方法就可以将整个集合的数据读取出来,
    从而避免了通过返回值进行是否达到文件末尾的判断。
    17.4.18 RandomAccessFile
    1 )基本概念
    java.io.RandomAccessFile 类主要支持对随机访问文件的读写操作。
    2 )常用的方法
    方法声明
    功能介绍
    RandomAccessFile(String name, String
    mode)
    根据参数指定的名称和模式构造对象
    r: 以只读方式打开
    rw :打开以便读取和写入
    rwd: 打开以便读取和写入,同步文件内容的更新
    rws: 打开以便读取和写入,同步文件内容和元数据
    的更新
    int read()
    读取单个字节的数据
    void seek(long pos)
    用于设置从此文件的开头开始测量的文件指针偏移
    void write(int b)
    将参数指定的单个字节写入
    void close()
    用于关闭流并释放有关的资源
    1. package com.lagou.task17;
    2. import java.io.IOException;
    3. import java.io.RandomAccessFile;
    4. public class RandomAccessFileTest {
    5. public static void main(String[] args) {
    6. RandomAccessFile raf = null;
    7. try {
    8. // 1.创建RandomAccessFile类型的对象与d:/a.txt文件关联
    9. raf = new RandomAccessFile("d:/a.txt", "rw");
    10. // 2.对文件内容进行随机读写操作
    11. // 设置距离文件开头位置的偏移量,从文件开头位置向后偏移3个字节 aellhello
    12. raf.seek(3);
    13. int res = raf.read();
    14. System.out.println("读取到的单个字符是:" + (char)res); // a l
    15. res = raf.read();
    16. System.out.println("读取到的单个字符是:" + (char)res); // h 指向了e
    17. raf.write('2'); // 执行该行代码后覆盖了字符'e'
    18. System.out.println("写入数据成功!");
    19. } catch (IOException e) {
    20. e.printStackTrace();
    21. } finally {
    22. // 3.关闭流对象并释放有关的资源
    23. if (null != raf) {
    24. try {
    25. raf.close();
    26. } catch (IOException e) {
    27. e.printStackTrace();
    28. }
    29. }
    30. }
    31. }
    32. }

  • 相关阅读:
    Python之函数-局部变量和global
    前端成神之路-HTML
    Python | Leetcode Python题解之第42题接雨水
    Ai绘画行业又叒翻天了!Stable Diffusion 3.0开源!多图实测附安装包!
    关于linux系统can收发,以及jetson系列can收发的说明,以及SN65HVD230 CAN board和MCP2515和TJA1050的区别是什么?
    ABAP语法基础4
    一文详解头层反绒皮和二层反绒皮的区别,别再傻傻分不清了!
    如何避免内部竞争和排斥,建立正面的工作文化?
    Html和Markdown中的空格,       以及   ‌ ‍
    LeetCode 897. 递增顺序搜索树
  • 原文地址:https://blog.csdn.net/gaosong0623/article/details/126869177