Java IO流详解

Java IO原理

  • I/O是Input/Output的缩写, I/O技术是非常实用的技术,用于处理设备之间的数据传输。如读/写文件,网络通讯等。
  • Java程序中,对于数据的输入/输出操作以“流(stream)” 的方式进行。
  • java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据。
    JavaIO流原理

流的分类

  • 按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
  • 按数据流的流向不同分为:输入流,输出流
  • 按流的角色的不同分为:节点流,处理流
抽象基类字节流----------字符流
输入流InputStream----------Reader
输出流OutputStream----------Writer

流的分类

IO流体系

IO流体系

节点流和处理流

  • 节点流:直接从数据源或目的地读写数据
    在这里插入图片描述
  • 处理流:不直接连接到数据源或目的地,而是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供强大的读写功能。
    在这里插入图片描述

InputStream & Reader

  • InputStream 和 Reader 是所有输入流的基类。
  • InputStream(典型实现:FileInputStream)
    int read()
    int read(byte[] b)
    int read(byte[] b, int off, int len)
  • Reader(典型实现:FileReader)
    int read()
    int read(char [] c)
    int read(char [] c, int off, int len)
  • 程序中打开的文件 IO 资源不属于内存里的资源,垃圾回收机制无法回收该资源,所以应该显式关闭文件 IO 资源。
  • FileInputStream 从文件系统中的某个文件中获得输入字节。FileInputStream 用于读取非文本数据之类的原始字节流。要读取字符流,需要使用 FileReader

InputStream

  • int read()
    从输入流中读取数据的下一个字节。返回 0 到 255 范围内的 int 字节值。如果因为已经到达流末尾而没有可用的字节,则返回值 -1。
  • int read(byte[] b)
    从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。如果因为已经到达流末尾而没有可用的字节,则返回值 -1。否则以整数形式返回实际读取的字节数。
  • int read(byte[] b, int off,int len)
    将输入流中最多 len 个数据字节读入 byte 数组。尝试读取 len 个字节,但读取的字节也可能小于该值。以整数形式返回实际读取的字节数。如果因为流位于文件末尾而没有可用的字节,则返回值 -1。
  • public void close() throws IOException
    关闭此输入流并释放与该流关联的所有系统资源。

OutputStream & Writer

  • OutputStream 和 Writer 也非常相似:
     void write(int b/int c);
     void write(byte[] b/char[] cbuf);
     void write(byte[] b/char[] buff, int off, int len);
     void flush();
     void close(); 需要先刷新,再关闭此流
  • 因为字符流直接以字符作为操作单位,所以 Writer 可以用字符串来替换字符数组,即以 String 对象作为参数
     void write(String str);
     void write(String str, int off, int len);
  • FileOutputStream 从文件系统中的某个文件中获得输出字节。FileOutputStream 用于写出非文本数据之类的原始字节流。要写出字符流,需要使用 FileWriter

OutputStream

  • void write(int b)
    将指定的字节写入此输出流。write 的常规协定是:向输出流写入一个字节。要写入的字节是参数 b 的八个低位。b 的 24 个高位将被忽略。 即写入0~255范围的。
  • void write(byte[] b)
    将 b.length 个字节从指定的 byte 数组写入此输出流。write(b) 的常规协定是:应该与调用write(b, 0, b.length) 的效果完全相同。
  • void write(byte[] b,int off,int len)
    将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。
  • public void flush()throws IOException
    刷新此输出流并强制写出所有缓冲的输出字节,调用此方法指示应将这些字节立即写入它们预期的目标。
  • public void close() throws IOException
    关闭此输出流并释放与该流关联的所有系统资源。

缓冲流

  • 为了提高数据读写的速度,Java API提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组,缺省使用8192个字节(8Kb)的缓冲区。
  • 缓冲流要“套接”在相应的节点流之上,根据数据操作单位可以把缓冲流分为:
    BufferedInputStream 和 BufferedOutputStream
    BufferedReader 和 BufferedWriter
    1 当读取数据时,数据按块读入缓冲区,其后的读操作则直接访问缓冲区
    2 当使用BufferedInputStream读取字节文件时,BufferedInputStream会一次性从文件中读8192个(8Kb),存在缓冲区中,直到缓冲区装满了,才重新从文件中读取下一个8192个字节数组。
    3 向流中写入字节时,不会直接写到文件,先写到缓冲区中直到缓冲区写满,BufferedOutputStream才会把缓冲区中的数据一次性写到文件里。使用方法flush()可以强制将缓冲区的内容全部写入输出流
    4 关闭流的顺序和打开流的顺序相反。只要关闭最外层流即可,关闭最外层流也会相应关闭内层节点流
    5 flush()方法的使用:手动将buffer中内容写入文件
    6 如果是带缓冲区的流对象的close()方法,不但会关闭流,还会在关闭流之前刷新缓冲区,关闭后不能再写出
    在这里插入图片描述
流的分类:
1 操作数据单位:字节流,字符流
2 数据的流向:输入流,输出流
3 流的角色: 节点流,处理流

二,流的体系结构
抽象基类         节点流(或文件流)       缓冲流(处理流的一种)
InputStream         FileInputStream     BufferedInputStream
OutputStream        FileOutputStream    BufferedOutputStream
Reader              FileReader          BufferedReader
Writer              FileWriter          BufferedWriter

package BYSSSExer2;

import org.junit.Test;

import java.io.*;

/**
 * 缓冲流的使用
 * 1 BufferedInputStream
 * 2 BufferedOutputStream
 * 3 BufferedReader
 * 4 BufferedWriter
 *
 * 作用:提高流额读取写入的速度
 * 提高读写速度的原因:内部提供了一个缓冲区
 *
 * 处理流,就是"套接"在已有的流的基础上
 * @author Baiysmart
 * @create 2020-03-27 20:40
 */
public class BufferedTest {
    //一 :实现非文本文件的复制
    @Test
    public void BufferedStreamTest() throws IOException {
        long l = System.currentTimeMillis();
        //1 造文件
        File srcFile = new File("cxy.jpg");
        File destFile = new File("cxy3.jpg");
        //2 造流
        //2.1 造节点流(文件流)
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);
        //2.2 造缓冲流
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //3 复制的细节:读取,写入
        byte[] buffer = new byte[10];
        int len;
        while ((len = bis.read(buffer))!=-1){
            bos.write(buffer,0,len);
        }
        //4 资源关闭
        //要求:先关闭外层的流,再关闭内层的流
        bos.close();
        bis.close();
        //说明:关闭外层流的同时,内层流也会自动的进行关闭,关于内层流的关闭,可以省略不写
        fos.close();
        fis.close();

        long l1 = System.currentTimeMillis();
        System.out.println(l1-l);
    }

    //实现文件复制的方法
    public void copyFileWithBuffered(String srcPath,String destpath) throws IOException {
        //1 造文件
        File srcFile = new File(srcPath);
        File destFile = new File(destpath);
        //2 造流
        //2.1 造节点流(文件流)
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);
        //2.2 造缓冲流
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //3 复制的细节:读取,写入
        byte[] buffer = new byte[10];
        int len;
        while ((len = bis.read(buffer))!=-1){
            bos.write(buffer,0,len);
        }
        //4 资源关闭
        //要求:先关闭外层的流,再关闭内层的流
        bos.close();
        bis.close();
        //说明:关闭外层流的同时,内层流也会自动的进行关闭,关于内层流的关闭,可以省略不写
        fos.close();
        fis.close();
    }

    @Test
    public void testcopyFileWithBUfferd() throws IOException {
        String srcPath = "cxy.jpg";
        String destPath = "cxy4.jpg";
        copyFileWithBuffered(srcPath,destPath);

    }
    /*
    使用Bufferedreader和BufferedWriter实现文本文件的复制
     */
    @Test
    public void testBufferedReaderBufferedWriter() throws IOException {
        //1 造文件 方式一
        File srcFile1  = new File("hello.txt");
        File destFile1 = new File("hello4.txt");
        //2 造流
        //2.1 造字节流
        FileReader fr = new FileReader(srcFile1);
        FileWriter fw = new FileWriter(destFile1);
        //2.2 造处理流
        BufferedReader br = new BufferedReader(fr);
        BufferedWriter bw = new BufferedWriter(fw);
        //方式二
        BufferedReader br = new BufferedReader(new FileReader(new File("hello.txt")));
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File("hello4.txt")));
        //3 读数据
        //方式一
        char[] cbuf =new char[5];
        int len;
        while ((len=br.read(cbuf))!=-1){
            bw.write(cbuf,0,len);
//            bw.flush();   系统已经自动flush();故可以不写(自动忽略)
        }
        //方式二
        String data;
        while ((data = br.readLine())!=null){
            bw.write(data);//data中不包含换行符
            //解决方法一
            bw.write(data+"\n");
            //解决方法二
            bw.write(data);
            bw.newLine();
        }
        //4 资源关闭
        bw.close();
        br.close();
    }
}

package BYSSSExer2;

import org.junit.Test;

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

/**
 * @author Baiysmart
 * @create 2020-03-27 16:18
 */

/*
    测试FileInputStream和FileOutputStream的使用

    结论:
    对于文本文件(.txt,.java,.class,.c),使用字符流处理
    对于非文本文件(.jpg,.mp3,...),使用字节流处理
 */
public class FileInputOutputStreamTest {
    //使用字节流FIleInputStream处理文本文件,可能出现乱码
    @Test
    public void testFileInputStream() throws IOException {
        // 1 造文件
        File file = new File("hello.txt");
        //2 造流
        FileInputStream fis = new FileInputStream(file);
        //3 读入数据
        byte[] buffer = new byte[5];
        int len;//记录每次读取字节的个数
        while ((len = fis.read(buffer))!=-1){
            String str = new String(buffer,0,len);
            System.out.print(str);
        }
        //4 资源关闭
        fis.close();
    }
    //实现图片的复制
    @Test
    public void testFileInputOutputStream() throws IOException {
        // 1 实例化File文件
        File srcfile  = new File("cxy.jpg");
        File destfile = new File("cxy1.jpg");
        //2 实例化流
        FileInputStream fis = new FileInputStream(srcfile);
        FileOutputStream fos = new FileOutputStream(destfile);
        //3 读数据操作
        byte[] buffer = new byte[5];
        int len;
        while ((len = fis.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }

        //4 资源的关闭
        fis.close();
        fos.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值