Java中的IO流

一、概述

IO流根据数据的流向可分为:输入流和输出流。

1、输入流就是把数据从其他设备上读取到的内存中的流。

2、输出流就是把数据从内存中写道其他设备上的流。

二、字节流

1、概述

一切文本数据(文本、图片、视频等)在存储时,都是以二进制数字的形式保存,都是一个一个的字节,那么传输时一样如此。所以,字节流可以传输任意文件数据。在操作流的时候,我们要时刻明确,无论使用什么样的流对象,底层传输的始终为二进制数据。

2、字节输出流(OutputStream)

2.1 字节输出流的基本概述

OutputStream(抽象类)是表示字节输出流的所有了的超类,将指定的字节信息写出到目的地。它定义了字节输出流的基本共性功能方法。

2.2 FileOutputStream类

FileOutputStream类是OutputStream抽象了的子类。

FileOutputStream类是文件输出流,用于将数据写出到文件。

2.3 构造方法

2.3.1 FileOutputStream(File file)

FileOutputStream(File file):创建文件输出流以写入由指定的File对象表示的文件。

以自定义的绝对路径创建文件及写入内容。

public class Demo_OutPut_01 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("D:/aa");
        File f2 = new File(f1,"a.txt");
        f1.mkdirs();
        f2.createNewFile();
        OutputStream out = new FileOutputStream(f2);
    }
}

2.3.2 FileOutputStream(String name)

FileOutputStream(String name):创建文件输出流以指定的名称写入文件。

通俗的讲:在使用工具项目包路径的父文件夹中创建文件及写入内容。

public class Demo_OutPut_01 {
    public static void main(String[] args) throws IOException {
        OutputStream out = new FileOutputStream("aa.txt");
    }
}

        相同点:当创建一个流对象时,必须传入一个文件路径。该路径下,如果没有这个文件,则会创建该文件。如果有该文件,则会清空这个文件的数据。

2.4 write()方法

2.4.1 write(int b)

官方讲解:将指定字节写入此文件输出流

自我理解:传入ASCII对应的解析数字,写入文件。一次传入一个字节进行解析。

public class Demo_OutPut_01 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("D:/aa");
        File f2 = new File(f1,"a.txt");
        f1.mkdirs();
        f2.createNewFile();
        OutputStream out = new FileOutputStream(f2);
        out.write(97);
    }
}

输出的结果为ASCII表中97对应的a。

2.4.2 write(byte[ ] b)

官方讲解:将 b.length 个字节从指定 byte 数组写入此文件输出流中。

自我理解:定义一串数组,传入文件输入流中。一次传入一个数组进行解析,效率高。

public class Demo_OutPut_01 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("D:/aa");
        File f2 = new File(f1,"a.txt");
        f1.mkdirs();
        f2.createNewFile();
        OutputStream out = new FileOutputStream(f2);
        byte[] bytes = {97,98,99,100,101,102,103};
        out.write(bytes);
    }
}

输出的结果为ASCII表中(97,98,99,100,101,102,103)对应的a,b,c,d,e,f,g。

2.4.3 write(byte[ ] b,int off,int len)

官方讲解:将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。

自我理解:将指定的byte数组中从索引值2开始的3个字节写入文件输出流中。

public class Demo_OutPut_01 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("D:/aa");
        File f2 = new File(f1,"a.txt");
        f1.mkdirs();
        f2.createNewFile();
        OutputStream out = new FileOutputStream(f2);
        byte[] bytes = {97,98,99,100,101,102,103};
        out.write(bytes,2,3);
    }
}

输出结果为:cde

3、字节输入流(InputStream)

3.1 字节输入流的基本概述

InputStream(抽象类)是表示字节输入流的所有类的超类,可以读取字节信息到内存中。它定义了字节流的基本共性功能方法。

3.2 FileInputStream类

FileInputStream类是InputStream抽象类的子类。

FileInputStream类是文件输入流,从文件中读取字节。

3.3 构造方法

3.3.1 FileInputStream(File file)

FileInputStream(File file):通过打开与实际文件的连接来创建一个FileInputStream,该文件由文件系统中的File对象file命名。

public class Demo_InPut_01 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("D:/a.txt");
        InputStream in = new FileInputStream(f1);
    }
}

3.3.2 FileInputStream(String name)

FileInputStream(String name):通过打开与实际文件的连接来创建一个FileInputStream,该文件由文件系统中的路径名name命名。

public class Demo_InPut_01 {
    public static void main(String[] args) throws IOException {
        InputStream in = new FileInputStream("a.txt");
    }
}

3.4 read()方法

3.4.1 read()

官方讲解:从此输入流中读取一个数据字节。

自我理解:每调用一次read方法读取一个数据字节。

例:我的a.txt文本里的内容为abcd

代码如下:

public class Demo_InPut_01 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("D:/a.txt");
        InputStream in = new FileInputStream(f1);
        int c1 = in.read();
        System.out.println(c1);

        int c2 = in.read();
        System.out.println(c2);

        int c3 = in.read();
        System.out.println(c3);

        int c4 = in.read();
        System.out.println(c4);
    }
}

输出结果如下:输出的是ASCII表中a,b,c,d对应的整数

 每次只能读取一个字节,使用起来特别麻烦。

3.4.2 read(byte[ ] b)

官方讲解:从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。

自我理解:定义一串数组定义数组长度,传入文件输入流中。一次传入10个数组长度进行解析,效率高。

public class Demo_InPut_02 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("D:/a.txt");
        InputStream in = new FileInputStream(f1);
        byte[] b = new byte[10];
        int n = in.read(b);
        System.out.println("读取的字节数:"+n);
        String s = new String(b);
        System.out.println(s);
    }
}

输出结果如下:

 3.4.3 read(byte[ ] b,int off, int len)

官方讲解:从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。

自我理解:从此输入流中将最多10个字节的数据读入一个byte数组中。

public class Demo_InPut_02 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("D:/a.txt");
        InputStream in = new FileInputStream(f1);
        byte[] b = new byte[10];
        int n = in.read(b);
        System.out.println("读取的字节数:"+n);
        String s = new String(b,0,n);
        System.out.println(s);
    }
}

输出结果如下:

 3.5 循环遍历输入流内容

public class Demo_InPut_02 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("D:/a.txt");
        InputStream in = new FileInputStream(f1);
        byte[] buffer = new byte[10];
        int n;
        while ((n=in.read(buffer))!=-1){
            String s = new String(buffer,0,n);
            System.out.println(s);
        }

    }
}

输出结果如下:

三、字符流

1、概述

在Java中,字节流处理文字除英文之外的其他文字都是无法处理的。因为字节流都是一个字节一个字节的读取,那么如果我们想处理一些汉语的话就可以使用字符流。

2、字符输入流(Rander)

2.1 FileRander类

2.1.1 FileRander类的父类是Rander。

2.1.2 FileRander类是字符输入流,从文件中读取字符。

2.2 构造方法

2.2.1 FileReader(File file):在给定从中读取数据的file的清空下创建一个新FileReader。

首先定义一个File类对象来映射文件再把File对象名传入FileReader中。

public class Demo_Reader_01 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("D:/aa/a.txt");
        //定义字符输入流
        Reader in = new FileReader(f1);
    }
}

2.2.2 FileReader(String fileName):在给定从中读取数据的文件名的情况下创建一个新的FileReader。直接创建FileReader对象里面传入文件路径。

public class Demo_Reader_01 {
    public static void main(String[] args) throws IOException {
        FileReader fileReader = new FileReader("D:/aa/a.txt");
    }
}

2.3 read()方法

2.3.1 在字符流中传入数组的是char类型,但是在字节流中传入的是byte类型。除了传入的数组类型其他使用方法基本一致。

2.3.2 read(char[ ] cbuf):将字符读入数组。

public class Demo_Reader_01 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("D:/aa/a.txt");

        //定义字符输入流
        Reader in = new FileReader(f1);

        //每次读取缓冲区
        char[] buffer = new char[10];
        int n = in.read(buffer);//每次至多读取数组长度个字符,把读取到字符存入数组中,返回的是实际读取到的字符数
    }
}

2.3.3 read(char[ ] cbu , int off , int len):将字符读入数组的某一部分。

cbuf:目标缓存区

off:开始存储字符处的偏移量

len:要读取的最多字符数

返回的是读取到的字符个数。

public class Demo_Reader_01 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("D:/aa/a.txt");

        //定义字符输入流
        Reader in = new FileReader(f1);

        //每次读取缓冲区
        char[] buffer = new char[100];
        int n = in.read(buffer);//每次至多读取数组长度个字符,把读取到字符存入数组中,返回的是实际读取到的字符数
        System.out.println(in.read(buffer,0,100));

        //关闭资源
        in.close();
    }
}

3. 字符输出流(writer)

3.1 FIleWriter类

3.1.1 FileWriter类是Writer类的子类

3.1.2 FileWiter类是字符输出流,用来写入字符文件。

3.2 构造方法

3.2.1 FileWriter(File File):根据给定的File对象钩爪一个File Writer对象。

public class Demo_Writer_01 {
    public static void main(String[] args) throws IOException {
        File f1 =new File("D:/aa/b.txt");
        //exists():判断文件是否存在,存在则返回true,反之返回false
        if (!f1.exists()){
            //文件不存在则创建文件
            f1.createNewFile();
        }
        //定义字符输出流
        Writer out = new FileWriter(f1);
    }
}

3.3 writer()方法

3.3.1 write(char[ ] cbuf):写入字符数组。

public class Demo_Writer_01 {
    public static void main(String[] args) throws IOException {
        File f1 =new File("D:/aa/b.txt");
        //exists():判断文件是否存在,存在则返回true,反之返回false
        if (!f1.exists()){
            //文件不存在则创建文件
            f1.createNewFile();
        }

        //定义字符输出流
        Writer out = new FileWriter(f1);

        char[] ch = {'豁','达','开','朗'};

        out.flush(); //字符流必须刷新缓冲区,才会真正写入磁盘文件中
        out.close(); //关闭资源
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值