JAVA IO流

什么是IO流

在这里插入图片描述

  • IO就是输入与输出首字母的简写,Java通过IO流可以对文件进行读写。

IO流的分类方式

  1. 按照流的方向进行分类,以内存为参照物
  • 往内存中去叫做输入(Input)。或者叫做读(Read)。
  • 从内存中出来,叫做输出(Output)。或者叫做写(Write)。
  1. 按照读取数据方式不同进行分类
  • 有的流是按照字节的方式读取数据,一次读取1个字节byte,等同于一次读取8个二进制,这种流是万能的,什么类型的文件都可以读。
  • 有的流是按照字符的方式读取数据的,一次读取一个字符,这种流是方便读取普通文本文件而存在的。

综上所述:流的分类大致为四种,输入流,输出流,字节流,字符流。

Java IO流的四大家族

  • java.io.InputStream 字节输入流
  • java.io.OutputStream 字节输出流
  • java.io.Reader 字符输出流
  • java.io.Writer 字符输出流
  1. 所有的流都实现了java.io.Closeable接口,都是可关闭的,都有close()方法。流毕竟是一个管道,这个是内存和硬盘之间的通道,用完之后一定要关闭,不然会占用很多资源。
  2. 所有的输出流都实现了java.io.Flushable接口,都是可刷新的,都有flush()方法,如果没有使用flush()方法可能会导致数据丢失。
    注意:在java中只要“类名”以Stream结尾的都是字节流。以“Reader/Writer”结尾的都是字符流。

java.io包下需要掌握的流:
文件专属:

  • java.io.FileInputStream
/*
* java.io.FileInputStream:
*    1.文件字节输入流,万能的,任何类型的文件都可以采用这个流来读。
*    2.字节的的方式,完成输入的操作,完成读的操作(从硬盘---->内存)
* */
package com.company.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInputStreamTest01 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            //创建文件字节输入流对象
            //文件路径:E:\Maven\apache-maven-3.6.3\README(IDEA会自动把\编译为\\,因为java中\表示转义)
            //一下采用了绝对路径的方式
            //FileInputStream fis = new FileInputStream("E:\\Maven\\apache-maven-3.6.3\\README.txt");
            //写成/也是可以的
            fis = new FileInputStream("E:/Maven/apache-maven-3.6.3/README.txt");
            //准备一个byte数组,每次读30个
            byte[] bytes = new byte[30];
            /*while (true){
                //读 read()方法,每次只读一个字节,并返回相对应的码表数字,当读到末尾没有数据是,返回-1
                int read = fis.read(bytes);
                if (read == -1){
                    break;
                }
                //把byte数组转成字符串,读到多少个转换多少个。
                System.out.print(new String(bytes,0,read));
            }*/
            //改良版
            int read = 0;
            while ((read = fis.read(bytes)) != -1){
                System.out.println(new String(bytes,0,read));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //在finally语句块中确保流一定要关闭
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

  • java.io.FileOutputStream
public class FileOutputStreamTest01 {
    /*
    * 文件字节输出流,负责写
    * 从内存到硬盘
    * */
    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {
            //myfile文件不存在时,会自动新建,如果原文件有内容,会将它清空,再写入,后面跟着true就会再原文件后面追加写入
            fos = new FileOutputStream("myfile",true);
            byte[] bytes = {97,98,99,100,101,102};
            //将byte数组全部写出
            fos.write(bytes);
            fos.write(bytes,0,2);
            //字符串
            String s = "我是一个中国人,我很骄傲!!!";
            //将字符串转换为byte数组
            byte[] bys = s.getBytes();
            fos.write(bys);
            //写完之后一定要刷新
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  • java.io.FileReader
public class FileReaderTest {
    /*
    * FileReader:
    * 文件字符输入流,只能读取普通文本
    * 读取文本内容时方便快捷
    * */
    public static void main(String[] args) {
        FileReader fr = null;
        try {
            fr = new FileReader("E:/Maven/apache-maven-3.6.3/README.txt");
            char[] chars = new char[30];
            int read = 0;
            while ((read = fr.read(chars)) != -1){
                System.out.println(new String(chars,0,read));
            }
            System.out.println(fr.read(chars));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  • java.io.FileWriter
public class FileWriterTest {
    /*
    * FileWriter:
    * 文件字符输出流,写
    * 只能输出普通文本
    * */
    public static void main(String[] args) {
        FileWriter fw = null;
        try {
            //创建文件字符输出流对象
            fw = new FileWriter("file",true);
            char[] chars = {'我','系','中','国','人'};
            fw.write(chars);
            fw.write(chars,2,3);
            fw.write("我是谁???");
            fw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

转换流(将字节流转换成字符流)

  • java.io.InputStreamReader
public class BufferedReadereTest02 {
    public static void main(String[] args) throws IOException {
        //字节流
        FileInputStream fileInputStream = new FileInputStream("E:/Maven/apache-maven-3.6.3/README.txt");

        //通过转换流转换(将字节流转换成字符流)
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);

        //这个构造方法只能传字符流不能传字节流
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        String line = null;
        while ((line = bufferedReader.readLine()) != null){
            System.out.println(line);
        }
        bufferedReader.close();
    }
}
  • java.io.OutputStreamWriter
public class BufferedWriterTest01 {

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("copy",true)));
        bufferedWriter.write("阿斯顿你哈四点");
        bufferedWriter.flush();
        bufferedWriter.close();
    }
}

缓冲流专属:

  • java.io.BufferedReader
public class BufferedReaderTest01 {
    /*
    * BufferedReader:
    * 带有缓冲区得字符输入流
    * 使用这个流得时候不需要自定义char数组,或者说不需要自定义byte数组,自带缓冲
    * */
    public static void main(String[] args) throws IOException {
        FileReader fileReader = new FileReader("E:/Maven/apache-maven-3.6.3/README.txt");
        //当一个流的构造方法中需要一个流的时候,这个被传进来的流叫做:节点流。
        //外部负责包装的流叫包装流,也叫处理流
        //这个程序中,FileReader就是个节点流,BufferedReader就是个包装流
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        /*String s = bufferedReader.readLine();
        System.out.println(s);

        String s1 = bufferedReader.readLine();
        System.out.println(s1);*/

        String s = null;
        //readLine()方法读取一个文本行,不带换行符
        while ((s = bufferedReader.readLine()) != null){
            System.out.println(s);
        }


        //关闭流,只需要关闭最外层的流就行
        bufferedReader.close();
    }
}
  • java.io.BufferedWriter
public class BufferedWriterTest01 {

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("copy",true)));
        bufferedWriter.write("阿斯顿你哈四点");
        bufferedWriter.flush();
        bufferedWriter.close();
    }
}
  • java.io.BufferedInputStream
  • java.io.BufferedOutputStream
    数据流专属:
  • java.io.DataInputStream
public class DataInputStreamTest01 {
    /*
    * DataInputStream:数据字节输入流
    *  DataOutputStream写的文件,只能用DataInputStream来读,并且读的时候要知道它写入的顺序
    * */
    public static void main(String[] args) throws IOException {
        DataInputStream data = new DataInputStream(new FileInputStream("data"));
        byte b = data.readByte();
        short s = data.readShort();
        int i = data.readInt();
        long l = data.readLong();
        float f = data.readFloat();
        double d = data.readDouble();
        boolean sex = data.readBoolean();
        char c = data.readChar();

        System.out.println(b);
        System.out.println(s);
        System.out.println(i);
        System.out.println(l);
        System.out.println(f);
        System.out.println(d);
        System.out.println(sex);
        System.out.println(c);
        data.close();
    }
}
  • java.io.DataOutputStream
public class DataOutPutStreamTest01 {
    /*
    * DataOutPutStream
    * 数据字节输出流,这个流可以将数据连同数据类型一并写入文件
    * 这个文件不是普通文件,不能用记事本打开
    * */
    public static void main(String[] args) throws IOException {
        //创建数据专属的字节输出流
        DataOutputStream stream = new DataOutputStream(new FileOutputStream("data"));
        //写数据
        byte b = 100;
        short s = 200;
        int i = 300;
        long l = 400L;
        float f = 3.14F;
        double d = 3.25;
        boolean sex = false;
        char c = '中';

        //写
        stream.writeByte(b);
        stream.writeShort(s);
        stream.writeInt(i);
        stream.writeLong(l);
        stream.writeFloat(f);
        stream.writeDouble(d);
        stream.writeBoolean(sex);
        stream.writeChar(c);
        
        stream.flush();
        stream.close();
    }
}

对象专属流:

  • java.io.ObjectInputStream
public class ObjectInputStreamTest01 {
    public static void main(String[] args) throws Exception {
        //反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("students"));
        Object obj = ois.readObject();
        System.out.println(obj);
        ois.close();
    }
}
  • java.io.ObjectOutputStream
public class Student implements Serializable {
    /*
    * Java虚拟机看到serializable接口之后,会自动生成一个序列化版本号
    * 这里没有手动写出来,java虚拟机会默认提供这个序列化版本号
    * */
    private int no;
    private String name;

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Student(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}
public class ObjectOutputStreamTest01 {
    public static void main(String[] args) throws IOException {
        //创建JAVA对象
        Student student = new Student(111, "sadasd");
        //序列化
        ObjectOutputStream students = new ObjectOutputStream(new FileOutputStream("students"));

        //序列化对象
        students.writeObject(student);

        students.flush();
        students.close();
    }
}

标准输出流:

  • java.io.PrintWriter
  • java.io.PrintStream
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值