JavaDay15-序列化流、打印流、压缩流

一、IO流体系

  • 字节流
    • InputStream

      • FileInputStream(基础流)
      • BufferedInputStream(高级流)
      • ObjectInputStream(反序列化流)(高级流)
      • 解压缩流
    • OutputStream

      • FileOutputStream(基础流)
      • BufferedOutputStream(高级流)
      • ObjectOutputStream(序列化流)(高级流)
      • PrintStream(字节打印流)(高级流)
      • 压缩流
  • 字符流
    • Reader
      • FileReader(基础流)
      • BufferedReader(高级流)
      • InputStreamReader(转换输入流)(高级流)
    • Writer
      • FileWriter(基础流)
      • BufferedWriter(高级流)
      • OutputStreamWriter(转换输出流)(高级流)
      • PrintWriter(字符打印流)(高级流)

二、序列化流、反序列化流

  • ObjectOutputStream(序列化流)
  • ObjectInputStream(反序列化流)
    注意:
    1、被序列化的对象需要继承Serializable接口,并生成版本号
    2、不希望被序列化的参数,使用transient修饰
    3、序列化多个对象,一般把对象放在ArrayList集合中,序列化整个集合,反序列化也是整个集合,最后遍历集合得到所有的对象
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
//ObjectOutputStream
public class ObjectStreamDemo01 {
    public static void main(String[] args) throws IOException {
        Student stu1=new Student("zhangsan",21,"南京");
        Student stu2=new Student("zhangsang",13,"南京");
        Student stu3=new Student("hangsan",44,"南京");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(""));
        oos.writeObject(stu1);
        oos.writeObject(stu2);
        oos.writeObject(stu3);
        oos.close();
    }
}
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
//ObjectInputStream
public class ObjectStreamDemo02 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(""));
        Student stu1 = (Student) ois.readObject();
        Student stu2 = (Student) ois.readObject();
        Student stu3 = (Student) ois.readObject();
        ois.close();
    }
}
//一次序列化多个对象
public class ObjectStreamDemo03 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ArrayList<Student> list = new ArrayList<>();
        Student stu1=new Student("zhangsan",21,"南京");
        Student stu2=new Student("zhangsang",13,"南京");
        Student stu3=new Student("hangsan",44,"南京");
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\src\\test.txt"));
        oos.writeObject(list);
        oos.close();
    }
}
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
//使用ArrayList读取全部的对象
public class ObjectStreamDemo04 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\src\\test.txt"));
        ArrayList<Student> list1 = (ArrayList<Student>) ois.readObject();
        for (Student student : list1) {
            System.out.println(student);
        }
        ois.close();
    }
}
import java.io.Serial;
import java.io.Serializable;
//Student对象
public class Student implements Serializable {
    @Serial
    private static final long serialVersionUID = 5738024112122835864L;
    //生成序列号后可以修改JavaBean类
    private String name;
    private int age;
    private transient String address;//transient修饰符使某个成员变量不被序列化

    public Student() {
    }

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

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 获取
     * @return address
     */
    public String getAddress() {
        return address;
    }

    /**
     * 设置
     * @param address
     */
    public void setAddress(String address) {
        this.address = address;
    }

    public String toString() {
        return "Student{name = " + name + ", age = " + age + ", address = " + address + "}";
    }
}

三、打印流

  • PrintStream(字节打印流)(高级流)
  • PrintWriter(字符打印流)(高级流)
    注意:
    1、只能操作文件目的地,不能操作数据源
    2、既能使用基本的写出方法(写入字符对应的ASCII值),也有特有的写出方法(数据原样写出)
    3、特有方法实现自动刷新、自动换行
    4、System.out.println就是开启了打印流并调用了打印流中的println()方法,默认指向控制台
  • 字节打印流的成员方法:(默认自动刷新)
    • public void write(int b)
    • public void print(Xxx xx)
    • public void println(Xxx xx)
    • public void printf(Xxx xx)
  • 字符打印流的方法:(需要手动开启自动刷新功能)
    • public void write(int b)
    • public void print(Xxx xx)
    • public void println(Xxx xx)
    • public void printf(Xxx xx)

四、压缩流、解压缩流

1、在Java中,压缩包里的每一个文件都是一个zipEntry对象

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
//解压缩
public class ZipStreamDemo01 {
    public static void main(String[] args) throws IOException {
        //1、定义源压缩文件夹
        File src = new File("");
        //2、定义目的文件夹
        File dest = new File("");
        //6、调用解压缩方法
        unzip(src,dest);

    }
    //3、定义一个解压缩的方法
    public static void unzip(File src,File dest) throws IOException {
        //4、定义一个解压缩流对象
        ZipInputStream zis = new ZipInputStream(new FileInputStream(""));
        //5、获取压缩包中的zipEntry对象
        ZipEntry zipEntry;
        while ((zipEntry=zis.getNextEntry())!=null){
            //情况一:zipEntry对象==文件夹--在相同的路径下创建文件夹
            if (zipEntry.isDirectory()){
                File file = new File(dest,zipEntry.toString());
                file.mkdirs();
            //情况二:zipEntry对象==文件--读取文件内容
            }else {
                FileOutputStream fos = new FileOutputStream(new File(dest, zipEntry.toString()));
                int ch;
                //通过zis对象读取文件内容
                while ((ch=zis.read())!=-1) {
                    fos.write(ch);
                }
                fos.close();
                //表示一个zipEntry对象处理完了
                zis.closeEntry();
            }
        }
        zis.close();
    }
}
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

//压缩单个文件
public class ZipStreamDemo02 {
    public static void main(String[] args) throws IOException {
        //1、源文件
        File src = new File("D:\\a.txt");
        //2、目的文件
        File dest = new File("D:\\");
        //7、调用压缩方法
        toZip(src,dest);
    }
    public static void toZip(File src,File dest) throws IOException {
        //3、创建压缩流对象
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(dest, "a.txt")));
        //4、把要压缩的文件放在ZipEntry对象中
        ZipEntry zipEntry=new ZipEntry("a.txt");
        //5、把zipEntry放在压缩包中
        zos.putNextEntry(zipEntry);
        //6、把src文件中的数据写到压缩包中
        FileInputStream fis = new FileInputStream(src);
        int ch;
        while ((ch=fis.read())!=-1){
            zos.write(ch);
        }
        fis.close();
        zos.closeEntry();
        zos.close();
    }
}
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
//压缩文件夹
public class ZipStreamDemo03 {
    public static void main(String[] args) throws IOException {
        //1、源文件夹
        File src = new File("D:\\aaa");//src==D:\aaa
        File destParent = src.getParentFile();//destParent==D:\
        //2、目的文件夹
        File dest = new File(destParent,src.getName()+".zip");//dest==D:\aaa.zip
        //3、创建压缩流对象
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(dest));
        //4、调用压缩方法
        toZip(src,zos,src.getName());//第三个参数是aaa
        //5、关流
        zos.close();
    }
    //定义压缩方法,参数一:源文件夹;参数二:压缩流;参数三:源文件夹的名字
    public static void toZip(File src,ZipOutputStream zos,String name) throws IOException {//String name表示压缩包里面的文件路径
        //循环读取文件夹中的内容
        File[] files = src.listFiles();
        for (File file : files) {
            //情况一:读到的是文件
            if(file.isFile()){
                //把要压缩的文件放在ZipEntry对象中
                ZipEntry zipEntry = new ZipEntry(name+"\\"+file.getName());//aaa+读取到的file名
                //把ZipEntry对象放在压缩包中
                zos.putNextEntry(zipEntry);
                //创建字节输入流--读取文件内容
                FileInputStream fis = new FileInputStream(file);
                int ch;
                while ((ch=fis.read())!=-1){
                    zos.write(ch);
                }
                fis.close();
                zos.closeEntry();
            //情况二:读到的是文件夹--递归
            }else {
                toZip(file,zos,name+"\\"+file.getName());//注意第三个参数是file,不是src
            }
        }
    }
}
posted @ 2023-04-13 00:07  小园初来乍到  阅读(48)  评论(0)    收藏  举报