javaSE.I/O-字节流

流的概念和分类

概念:内存与存储设备之间传输数据的通道
按方向分

  • 输入流:将存储设备中的数据读入到内存(程序)中;
  • 输出流:将内存中的数据写入到存储设备中;
    按单位分
    字节流:以字节为单位,可以读写所有数据;
    字符流:以字符为单位,只能读写文本数据;
    按功能分:
    节点流:具有实际传输数据的读写功能;
    过滤流:在节点流的基础之上增强功能;

字节流

字节流的父类(抽象类)

  • InputStream
    int read()
    int read(byte[] b)
    int read(byte[] b,int off,int len)
  • OutputStream
    void write(int n)
    void write(byte[] b)
    void write(byte[] b,int off,int len)

文件字节流(实现类)

  • FileInputStream
    int read(byte[] b) //从输入流中读取多个字节,将读到的数据存入数组b,返回实际督导的字节数;如果到达文件末,则返回-1;
  • FileOutputStream
    void write(byte[] b) //一次写多个字节,将数组b中的所有字节,写到输出流;
/**
 * 演示FileInputStream、FileOutputStream使用
 * 文件字节输入流
 */
public class FileStreamDemo {
    public static void main(String[] args) throws Exception {
        //1.创建文件输入流FileInputStream,并指定文件路径
        FileInputStream fis = new FileInputStream("d:\\a.txt");
        //2.1 读取文件fis.read()
        int data=0;
        while ((data=fis.read())!=-1){
            System.out.print((char)data);
        }
        System.out.println();
        //2.2 读取多个字节fis.read(byte[],b)
        byte[] buf=new byte[3];
        int count=0;
        while ((count=fis.read(buf))!=-1){
            System.out.println(new String(buf,0,count));
        }
        System.out.println("count= "+count);//-1
        //3.关闭
        fis.close();
        System.out.println();
        System.out.println("执行完毕");

        //1.创建文件输出流对象FileOutputStream
        FileOutputStream fos = new FileOutputStream("d:\\b.txt");
        //2.写入到文件中
        fos.write(97);
        fos.write('b');
        String str = "hello word";
        fos.write(str.getBytes());
        //关闭
        fos.close();
        System.out.println("执行完毕");
    }
}
/**
 * 使用文件字节流实现文件复制
 */
public class CopyFile {
    public static void main(String[] args) throws Exception {
        //1.创建流
        FileInputStream fis = new FileInputStream("d:\\butterfly.jpg");
        FileOutputStream fos = new FileOutputStream("d:\\butterfly1.jpg");
        //2.边读边写
        byte[] buf=new byte[1024];
        int count=0;
        while((count=fis.read(buf))!=-1){
            fos.write(buf,0,count);//有多少复制多少
        }
        //3.关闭流
        fis.close();
        fos.close();
        System.out.println("复制完毕");
    }
}

字节缓冲流

  • 提高IO效率,减少访问磁盘的次数;
  • 数据存储在缓冲区中,flush时将缓存区的内容写入文件中,也可以直接close;
    BufferedInputStream/BufferedOutputStream
/**
 * 演示字节缓冲流BufferedInputStream、BufferedOutputStream
 */
public class BufferedStreamDemo {
    public static void main(String[] args) throws Exception {
        //1.创建BufferedInputStream
        FileInputStream fis = new FileInputStream("d:\\a.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        //2.读取字节数据
        int data=0;
        while((data=bis.read())!=-1){
            System.out.print((char) data);
        }
        //3.关闭流
        bis.close();
        
        //1.创建BufferedOutputStream
        FileOutputStream fos = new FileOutputStream("d:\\b.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //2.写入文件
        for (int i = 0; i <10 ; i++) {
            bos.write("hello\r\n".getBytes());//先写入8K缓存区
            bos.flush();//从缓存区写到文件
        }
        //3.关闭(内部调用fos.close)
        bos.close();
    }
}

对象字节流

  • 增强缓存区功能;
  • 增强了读写8中基本数据类型和字符串功能;
  • 增强了读写对象的功能:readObject() / writeObject(Object obj);
  • 使用流传输对象的过程称为序列化、反序列化;
    【注意】序列化类必须实现Serializable接口;
    序列化类中的对象属性也要求实现Serializable接口;
    序列化id确保是同一个类;
    使用transient修饰不被序列化的属性,不被保存;
    静态属性不能序列化;
    序列化多个对象,可用集合方式
    ObjectInputStream/ObjectOutputStream
/**
 * 演示对象字节流ObjectInputStream、ObjectOutputStream
 */
public class ObjectStream{
    public static void main(String[] args) throws Exception {
        //1.创建对象流
        FileOutputStream fos = new FileOutputStream("d:\\stu.bin");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        //2.1 写出单个对象到文件(序列化)
        Student tom = new Student("tom", 100);
        oos.writeObject(tom);
        //2.2 写出多个对象,采用集合方式
        Student s1=new Student("stu1",101);
        Student s2=new Student("stu2",102);
        Student s3=new Student("stu3",103);
        ArrayList<Student> students = new ArrayList<>();
        students.add(s1);
        students.add(s2);
        students.add(s3);
        oos.writeObject(students);
        //3.关闭流
        oos.close();
        System.out.println("序列化完毕");

        //1.创建对象流
        FileInputStream fis = new FileInputStream("d:\\stu.bin");
        ObjectInputStream ois = new ObjectInputStream(fis);
        //2.1 读入到内存(反序列化)
        Student s=(Student) ois.readObject();
        //2.2 多个对象的反序列化,采用集合实现
        ArrayList<Student> list=(ArrayList<Student>) ois.readObject();
        //3.关闭流
        ois.close();
        System.out.println("反序列化完毕");
        System.out.println("读入的对象是:"+s.toString());
        System.out.println("读入的对象是:"+list.toString());
    }
}

字符流

File类

posted @ 2022-03-11 10:10  老李学Java  阅读(39)  评论(0)    收藏  举报