打印流&压缩流

《一》 打印流

1. 概述

1.1 打印流只能写不能读

在这里插入图片描述

2. 打印流

  • 分类 : 打印流一般是指 : PrintStreamPrintWriter两个类
  • 特点1 : 打印流只操作文件目的地,不操作数据源
  • 特点2 : 特有的写出方法可以实现,数据原样写出
  •    例如 : 打印 : 97    文件中 : 97
  •       打印: true    文件中:true
  • 特点3 : 特有的写出方法,可以实现自动刷新,自动换行
  •    打印一次数据 =写出+ 换+刷新
2.1 字节打印流
  1. 字节打印流的构造方法
    在这里插入图片描述
  2. 字节打印流的成员方法
    在这里插入图片描述
  3. 相关实例代码
    package com.str.box.printstream;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.PrintStream;
    import java.nio.charset.Charset;
    import java.nio.charset.StandardCharsets;
    
    public class demo01 {
        public static void main(String[] args) throws FileNotFoundException {
            // 1.创建字节打印流的对象
            PrintStream ps = new PrintStream(new FileOutputStream("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\b.txt"),true, StandardCharsets.UTF_8);
            // 2.写出数据
            ps.println(97);// 写出+自动刷新+自动换行
            ps.print(true);
            ps.println();
            ps.printf("%s拍了拍%s","对方","你");
    
            ps.close();
        }
    }
    /*结果:
        97
        true
        对方拍了拍你
    */
    
2.2 字符打印流
  1. 构造方法在这里插入图片描述
  2. 成员方法
    在这里插入图片描述
  3. 相关实例代码
    package com.str.box.printstream;
    
    import java.io.*;
    import java.nio.charset.StandardCharsets;
    
    public class demo02 {
        public static void main(String[] args) throws IOException {
            PrintWriter pw = new PrintWriter(new FileWriter("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\b.txt"),true);
            pw.println(97);
            pw.print("hello");
            pw.printf("%s拍了拍%s","对方","你");
            pw.close();
        }
    }
    

3. 打印流的应用场景

  1. 打印流有几种?各有什么特点?
  • 有 字节打印流 和 字符打印流 两种
  • 打印流不操作数据源,只能操作目的地
  • 字节打印流:默认自动刷新,特有的println自动换行
  • 字符打印流:自动刷新需要开启,特有的println自动换行
  • 获取打印流的对象,此打印流在虚拟机启动的时候,由虚拟机创建,默认指向控制台
  • 特殊的打印流,系统中的标准输出流,是不能关闭,在系统中是唯一的
      PrintStream ps = System.out;

《二》解压缩流/压缩流

2.1 压缩流系统架构

在这里插入图片描述

2.2 解压缩流

解压本质: 把每一个ZipEntry按照层级拷贝到本地另一个文件夹中

  1. 案例
package com.str.box.ZipStream;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class demo01 {
    public static void main(String[] args) throws IOException {

        // 1.创建一个 File 表示要解压的压缩包
        File src = new File("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\aaa1.zip");
        // 2.创建一个 File 表示解压的目的地
        File dest = new File("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\");
        // 3.进行解压
        unzip(src,dest);

    }

    // 定义一个方法用来解压
    public static void unzip(File src,File dest) throws IOException {
        // 解压的本质:把压缩包里面的每一个文件或者文件夹读取出来,按照层级拷贝到目的地当中
        // 创建一个解压缩流用来读取压缩包中的数据
        ZipInputStream zip = new ZipInputStream(new FileInputStream(src));

        // 要先获取到压缩包里面的每一个zipEntry对象
        //ZipEntry entry = zip.getNextEntry();
        // 表示当前在压缩包中获取到的文件或者文件夹
        ZipEntry entry;
        while ((entry = zip.getNextEntry()) != null){
            System.out.println(entry);
            if (entry.isDirectory()){
                // 文件夹:需要在目的地 dest处创建一个同样的文件夹
                File file = new File(dest, entry.toString());
                file.mkdir();
            }else {
                // 文件:需要读取到压缩包中的文件,并把他存放到目的地dest文件夹中(按照层级目录进行存放)
                FileOutputStream fos = new FileOutputStream(new File(dest, entry.toString()));
                int b;
                while ((b=zip.read()) != -1){
                    // 写到目的地
                    fos.write(b);
                }
                fos.close();
                //表示在压缩包中的一个文件处理完成了
                zip.closeEntry();
            }
        }
        zip.close();
    }
}
  1. 运行结果:
    在这里插入图片描述
    在这里插入图片描述

2.2 压缩流

压缩本质 : 把每一个(文件/文件夹)看成ZipEntry对象放到压缩包中

package com.str.box.ZipStream;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class demo02 {
    public static void main(String[] args) throws IOException {
        /*
        * 压缩本质:把每一个(文件/文件夹)看成ZipEntry对象放到压缩包中
        * */

        // 1.创建File对象表示要压缩的文件
        File src = new File("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\1.png");
        // 2.创建File对象表示压缩包的位置
        File dest = new File("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\");
        // 3.调用方法用来压缩
        toZip(src,dest);
    }

    /*
    * 作用:压缩
        参数一:表示要压缩的文件
        参数二:表示压缩包的位置
    **/
    private static void toZip(File src, File dest) throws IOException {
        //1.创建压缩流关联压缩包
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(dest, "a.zip")));
        //2.创建zipEntry对象,表示压缩包里面的每一个文件和文件夹
        ZipEntry entry = new ZipEntry(src.toString());
        //3.把zipEntry对象放到压缩包当中
        zos.putNextEntry(entry);
        // 4.把src文件中的数据写到压缩包当中
        FileInputStream fis = new FileInputStream(src);
        int b;
        while ((b=fis.read()) != -1){
            zos.write(b);
        }
        zos.closeEntry();
        zos.close();
    }
}
package com.str.box.ZipStream;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class demo03 {
    public static void main(String[] args) throws IOException {
        /*
        * 压缩流
        * 需求:
        * 把D:\\aaa文件夹压缩成一个压缩包
        * */

        // 1.创建File对象表示要压缩的文件
        File src = new File("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\aaa");
        // 2.创建File对象表示压缩包放在哪里?(压缩包的父级路径)
        File destParent = src.getParentFile();
        // 3.创建File对象表示压缩包的路径
        File dest = new File(destParent,src.getName()+".zip");
        /*System.out.println(dest);*/
        //4.创建压缩流关联压缩包
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(dest));
        //5.获取src里面的每一个文件,变成zipEntry对象,放入到压缩包当中
        toZip(src,zos,src.getName());//aaa
        //6.释放资源
        zos.close();
    }

    /*
    *   作用:获取src里面的每一个文件,变成zipEntry对象,放入到压缩包当中
    *   参数一:数据源
    *   参数二:压缩流
    *   参数三:压缩包内部的路径
    **/
    private static void toZip(File src,ZipOutputStream zos,String name) throws IOException {
        // 1.进入src文件
        File[] files = src.listFiles();
        // 2.遍历数组
        for (File file : files) {
            if (file.isFile()){
                // 3.判断-文件;变成ZipEntry对象,放入到压缩包当中
                ZipEntry entry = new ZipEntry(name + "\\" + file.getName());
                zos.putNextEntry(entry);
                // 读取文件中的数据,写到压缩包中
                FileInputStream fis = new FileInputStream(file);
                int b;
                while ((b=fis.read()) != -1){
                    zos.write(b);
                }
                fis.close();
                zos.closeEntry();
            }else {
                // 4.判断-文件夹,递归
                toZip(file,zos,name+"\\"+file.getName());
            }
        }

    }

}

运作结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值