IO——四种基类InputStream/OutputStream/Reader/Writer操作方法介绍

回顾上一个文章:初学IO流

其中讲述了流的划分,提出了流的四种基类:InputStream/OutputStream/Reader/Writer
重点:
字节流的基类是InputStream/OutputStream
字符流的基类是Reader/Writer
输入流的基类是InputStream/Reader
输出流的基类是OutputStream/Writer
在这里插入图片描述
下面介绍一下这四种基类的实现步骤和应用场景:

一、字节输入流的基类:InputStream

  • 作用:经常用于读取文件中的内容
  • 数据读取流程:
    在这里插入图片描述
  • 输入流读操作步骤
    1. 调用实现类FileInputStream,打开特定类型的流 FileInputStream fileInputStream = new FileInputStream(path);
    2. 读取数据 read()->此方法可能会抛出IOException
    注意:当read()返回的值为-1时,表示读到文件内容结尾处
    3. 关闭流 close()
  • 输入流读操作步骤代码展示:
public class ByteInput316 {
    public static void main(String[] args) throws IOException {
        String path = "C:\\Users\\50579\\Desktop\\tt.txt";    //定义一个路径
        //步骤1.调用实现类FileInputStream,打开特定类型的流
        FileInputStream fileInputStream = new FileInputStream(path);    //文件输入流(可能会抛出异常)
        //步骤2.读取数据
       int read = fileInputStream.read();  //方法一:读取单个值
       System.out.print((char) read);    // 一个字节一个字节的读,read为读到的int类型的值,这里需要强转成char类型的字符
        
        byte[] bytes = new byte[100];   //方法二:批量读取数据(读到一个新建的byte类型的数组里)
        fileInputStream.read(bytes);  //将数据读到数组里
        System.out.println(Arrays.toString(bytes)); //将byte类型的数组的内容利用Arrays工具类打印出来
           
        fileInputStream.read(bytes, 4, 9);  //方法三:bytes表示存储读取数据的数组,off:4为数组中偏移的位置,从数组的第4个位置开始存读取的数据,len:9为读取文件内容的长度
        
         int read = 0;  
        while (( (read = fileInputStream.read(bytes)) != -1)) {   //返回-1表示读取到文件末尾
            System.out.println(new String(bytes,0,read));  //打印数组
        }
        //步骤3.关闭流
        fileInputStream.close();
        }
    }
  • 常用方法
    1. int available(); 返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取(或跳过)的估计剩余字节数
    2. skip(long n); 从输入流中跳过并丢弃 n 个字节的数据(从内核的第n个数据开始读取)。
    3. boolean markSupported(); 判断当前操作系统是否支持标记mark
    4. **mark(2);**标记读的位置,可以重复的读当前位置

二、字节输出流的基类:OutputStream

  • 作用:经常用于写或者修改文件中的内容
  • 输出流写操作步骤
    1. 调用实现类FileOutputStream,打开特定类型的流 FileOutputStream fileOutputStream = new FileOutputStream(path); (当路径中的目录不合法时,会抛出FileNotFound异常)
    2. 写操作()->此方法可能会抛出IOException异常
    3. 关闭流 close()->此方法可能会抛出IOEx异常
  • 输入流操作步骤代码展示:
public class ByteOutput319 {
    //写操作介绍
    public static void write() {
        /*
        * 步骤:
        *     1.打开字节输出流(可能会抛出FileNotFound异常)
        *     2.写操作(可能会抛出IO异常)
        *     3.关闭流(可能会抛出IO异常)
        * */
            //路径表示中的目录不合法时,抛出FileNotFound异常
        String path = "C:\\Users\\50579\\Desktop\\tt.txt" ;
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(path);
            /*
             * 写操作研究
             * void write(int b)  每次写入一个字节
             * void write(byte[] b) 批量写入,写入b数组
             * void write(byte b[], int off, int len) 在b数组的固定位置写入,
             * */
            fileOutputStream.write(97);   //写操作,可能会抛出IOException异常 (将97表示的字符a写入到tt.txt中)

            byte[] bytes = {'a','b','c'};
            fileOutputStream.write(bytes);    //批量写入操作,写入类型为byte数组,将数组的内容写入tt.txt文件中

            fileOutputStream.write("hello".getBytes()); //直接写入字符串

            //上三种写入操作都是拼接型写入
            //覆盖写
            fileOutputStream.write("hellohello".getBytes());

            //给定位置写
            fileOutputStream.write(bytes,0,2);  //off(应该小于数组长度)从0位置开始,左闭右开,len为写入长度:只将数组中的0和1位置写入tt.txt中(写入a和b)

            //刷新
            /*刷新此输出流并强制写出所有缓冲的输出字节。 flush 的常规协定是:如果此输出流的实现已经缓冲了以前写入的任何字节,则调用此方法指示应  将这些字节立即写入它们预期的目标。如果此流的预期目标是由基础操作系统提供的一个抽象(如一个文件),则刷新此流只能保证将以前写入到流的字节传递给操作系统进行写入,但不保证能将这些字节实际写入到物理设备(如磁盘驱动器)
            */
            fileOutputStream.flush();
            //关闭流
            fileOutputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        write();
    }
}

三、字符输入流的基类:Reader

  • 作用:经常用于读取文件中的内容
  • 输入流读操作步骤
    1. 调用实现类FileReader,打开字符写操作流 FileReader fileReader = new FileReader(path); (可能会抛出IOException异常)
    2. 读操作()->此方法可能会抛出IOException异常
    3. 关闭读操作流 close()->此方法可能会抛出IOEx异常
  • 输入流操作步骤代码展示:
 public static void read() throws IOException {
        String path = "C:\\Users\\50579\\Desktop\\tt.txt";
        //Reader为字符流的读操作基类,读取文件。FileReader
        /*
        * 读操作介绍
        * 1.打开字符读操作流。可能会抛出FileNotFoundException异常
        * 2.读操作,会抛出IO异常
        * 3.关闭读操作流
        * */
        FileReader fileReader = new FileReader(path);
        int i= fileReader.read();
        System.out.println((char) i);  // c
        fileReader.close();

        //单个值读操作
        fileReader.read();
        //批量读,读到char类型的数组里,返回值表示读取有效数据的个数
        char[] by = new char[100];
        fileReader.read(by);
        //批量读,从固定位置开始读,规定读取长度
        fileReader.read(by,0,4);

        //读取数据(较难,但是更加规范,之后会在博客里详细介绍)
        CharBuffer allocate = CharBuffer.allocate(1024); //NIO下提供的缓存类CharBuffer。
        fileReader.read(allocate);
        allocate.flip(); //进行读写切换
        char[] chars = new char[allocate.remaining()];
        allocate.get(chars);//读到chars数组里
        String s = new String(chars);
        System.out.println(s);
    }

四、字符输出流的基类:Writer

  • 作用:经常用于写或者修改文件中的内容
  • 输出流写操作步骤
    1. 调用实现类FileWriter,打开特定类型的流 FileWriter fileWriter = new FileWriter(path); (可能会抛出IOException异常)
    2. 写操作()->此方法可能会抛出IOException异常
    3. 关闭流 close()->此方法可能会抛出IO异常(关闭之后再进行操作时就会抛出异常)
  • 输入流操作步骤代码展示:
 public static void write() throws IOException{
        String path = "C:\\Users\\50579\\Desktop\\tt.txt";  //定义一个路径
        /*
        * 字符写操作步骤:
        * 1.打开字符写操作流,可能会抛出IOException异常
        * 2.写操作
        * 3.关闭写操作流
        * */
            FileWriter fileWriter = new FileWriter(path,true);
            fileWriter.write(99);  //参数:int c
            fileWriter.close();
        /*
        * 介绍一系列写操作
        */
        //写入单个字符void write(int c)
           fileWriter.write(98);
        //批量写入字符void write(char cbuf[]),写入类型为char类型的数组
           char c[] = {'a','v','i'};
           fileWriter.write(c);
        //固定位置写入void write(char cbuf[], int off, int len)
           fileWriter.write(c,0,2);
        //直接写入字符串void write(String str)
           fileWriter.write("hello");
        //写入字符串
           fileWriter.write("hello",0,4);
        //追加数据,追加数据的流必须和前期写入的流是同一个打开的流实例。
        //构造函数中FileWriter(File file, boolean append)
        //appened默认为false,为覆盖写,如果为true的话,则是追加写
           fileWriter.append("hehe");  
        //刷新缓存
           fileWriter.flush();

        //关闭流
        fileWriter.close();  
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值