IO流——字节流

【key】字节流体系

9b65bc93cd0c41ec82571bffb74502f5.png

 

一、OutputStream类

OutputStream是一个抽象类,是表示字节输出流的所有类的超类。操作的数据都是字节,定义了输出字节流的基本共性功能方法。

常见方法:

b7d4bd7551694f07a1f1fe6ebe229c07.jpeg

 二、FileOutputStream类

FileOutputStream类通过字节的方式写数据到文件,适合所有类型文件(图像、视频、文本文件等)

构造方法:

e39bf468cb4444c48010eb7172ce9217.png

【示例】写入数据到文件

public class FileOutputStreamDemo {

   public static void main(String[] args) {

     // 创建存储数据的文件

      File file = new File("test.txt");

      OutputStream os = null;

      try {

         // 创建一个字节输出流,明确需要操作的文件

         // 如果文件不存在,则创建;如果文件存在,则覆盖!

         os = new FileOutputStream(file);

         // 调用父类的方法存数据。立刻完成存储,不需要强制刷新存入

         os.write("haha".getBytes());

      } catch (FileNotFoundException e) {

         e.printStackTrace();

      } catch (IOException e) {

         e.printStackTrace();

      } finally {

         // 一定要判断 os 是否为 null,只有不为null时,才可以关闭资源

         if(os != null) {

            try {

               os.close();

            } catch (IOException e) {

               e.printStackTrace();

            }

         }

      } 

}

}

 给文件续写:

我们直接 new FileOutputStream(file)这样创建对象,写入数据,会覆盖原有的文件。那么我们想在原有的文件中续写内容怎么办呢?

在 FileOutputStream 的构造函数中,可以接受一个 boolean类型的值,如果值 true,就会在文件末位继续添加(默认是false)。

df912b0c608f40a5a7380160d2c0168d.jpeg

 【示例】将数据写入文件

public class FileOutputStreamDemo {

   public static void main(String[] args) {

      // 创建存储数据的文件

      File file = new File("test.txt");

      OutputStream os = null;

      try {

         // 创建一个字节输出流,明确需要操作的文件

         os = new FileOutputStream(file, true);

         // 调用父类的方法存数据。

         os.write("haha".getBytes());

// 添加换行

         os.write("\r\n".getBytes());

         os.write("haha".getBytes());

      } catch (FileNotFoundException e) {

         e.printStackTrace();

      } catch (IOException e) {

         e.printStackTrace();

      } finally {

         // 一定要判断 os 是否为null,只有不为null时,才可以关闭资源

         if(os != null) {

            try {

               os.close();

            } catch (IOException e) {

               e.printStackTrace();

            }

         }       

      }

   }

}

三、InputStream类 

InputSteam是一个抽象类,是表示字节输入流的所有类的超类。操作的数据都是字节,定义了字节输入流的基本共性功能方法。

InputStream类中常见的方法:

ee92d94bcd384771bd9245ea23b12cdf.jpeg

四、FileInputStream

InputStream 有很多子类,其中子类 FileInputStream 可用来写入数据到文件。

FileInputStream通过字节的方式读取文件,适合读取所有类型文件(图像、视频、文本文件等)。

构造方法:

035f9ec2e3984cd0aab4e8b9f59a1956.png

 读取数据read()方法:

在读取文件中的数据时,调用 read 方法一次读一个字节,从而实现从文件中读取数据。

【示例】读取数据read()方法

public class FileInputStreamDemo {

   public static void main(String[] args) {

      FileInputStream fs = null;

      try {

         // 创建一个字节输入流对象,必须明确数据源

            // 如果文件不存在,则抛出FileNotFoundException异常!

         fs = new FileInputStream("input.txt");

         // 一次读一个字节

         int len = 0;

         // 当返回值为-1,则数据读取完毕

         while((len = fs.read()) != -1) {

            System.out.println((char)len);

         }

      } catch (FileNotFoundException e) {

         e.printStackTrace();

      } catch (IOException e) {

         e.printStackTrace();

      } finally {

         if(fs != null) {

            try {

               fs.close();

            } catch (IOException e) {

               e.printStackTrace();

            }

         }

      }

   }

}

读取数据read(byte[])方法

在读取文件中的数据时,调用 read 方法,每次只能读取一个字节,太麻烦了,于是我们可以定义数组作为临时的存储容器,这时可以调用重载的 read 方法,一次可以读取多个字节。

【示例】读取数据read(byte[])方法

public class FileInputStreamDemo {

   public static void main(String[] args) {

      FileInputStream fs = null;

      try {

         // 创建一个字节输入流对象,必须明确数据源

         fs = new FileInputStream("test.txt");

         StringBuilder sb = new StringBuilder();

         byte[] by = new byte[1024];

        int len = 0;//保存获取到字节的长度

         // 当返回值为-1,则数据读取完毕

         while((len = fs.read(by)) != -1) {

            sb.append(new String(by, 0, len));

         }

         // 输出内容

         System.out.println(sb);

      } catch (FileNotFoundException e) {

         e.printStackTrace();

      } catch (IOException e) {

         e.printStackTrace();

      } finally {

         if(fs != null) {

            try {

               fs.close();

            } catch (IOException e) {

               e.printStackTrace();

            }

         }

      }

   }

}

一次性读全部数据

使用FileInputStream类的available()方法,可以获取文件的所有字节个数。

但如果遇到文件数据量很大,容易造成内存溢出。

【示例】一次性读取文件全部数据

public class FileInputStreamDemo {

   public static void main(String[] args) {

      FileInputStream fs = null;

      try {

         // 创建一个字节输入流对象,必须明确数据源

         fs = new FileInputStream("test.txt");

         int len = 0;

         // 获取文件字节个数

         int available = fs.available();

         byte[] by = new byte[available];

         // 一次性获取文件所有数据

         fs.read(by);

         System.out.println(new String(by));

      } catch (FileNotFoundException e) {

         e.printStackTrace();

      } catch (IOException e) {

         e.printStackTrace();

      } finally {

         if(fs != null) {

            try {

               fs.close();

            } catch (IOException e) {

               e.printStackTrace();

            }

         }

      }

   }

}

五、文件流的拷贝案例

【示例】字节流文件的拷贝案例

public class FileCopyDemo {

   public static void main(String[] args) throws IOException {

      // 明确字节流,输入流和源相关联,输出流和目的关联。

      FileInputStream fis = new FileInputStream("E://movie.mp4"); // 输入流

      FileOutputStream fos = new FileOutputStream("F://111.mp4"); // 输出流

      // 定义一个数组,用于缓存取出来的数据

      byte[] by = new byte[1024];

      int len = 0;

      // 输入流,读取数据

      while ((len = fis.read(by)) != -1) {

         // 输出流,保存数据

         fos.write(by, 0, len);

      }

      // 关闭流

      fis.close();

      fos.close();

   }

}

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

深山老林.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值