I/O流 进阶流

缓冲流

缓冲流也分为四种流,分别对应字节和字符的输入输出流,在前面加上Buffered

底层自带了长度为8192的缓冲区提高性能

字节缓冲流

单字节

java 复制代码
import java.io.*;

public class BufferedStreamDemo1 {
    public static void main(String[] args) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("rubish\\src\\file\\a.txt"));
        BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream("rubish\\src\\file\\b.txt"));
        int b;
        while ((b = bis.read()) != -1){
            bos.write(b);
        }
        bos.close();
        bis.close();
    }
}

多字节

java 复制代码
import java.io.*;

public class BufferedStreamDemo2 {
    public static void main(String[] args) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("rubish\\src\\file\\a.txt"));
        BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream("rubish\\src\\file\\b.txt"));
       byte[] bytes = new byte[1024];
       int len;
       while((len = bis.read(bytes)) != -1){
           bos.write(bytes,0,len);
       }
        bos.close();
        bis.close();
    }
}

字符缓冲流

字符缓冲流提升的效率并不明显

字符缓冲输入流

java 复制代码
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedStreamDemo3 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("rubish\\src\\file\\a.txt"));
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }
}

字符缓冲输出流

java 复制代码
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedStreamDemo4 {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("rubish\\src\\file\\a.txt"));
        bw.write("hello");
        bw.newLine();//字符特有功能
        bw.write("world");
        bw.newLine();
        bw.close();
    }
}

转换流

是字符流和字节流的桥梁

利用转换流按照指定的字符编码读取,替代方法

java 复制代码
import java.io.*;
import java.nio.charset.Charset;

public class ConvertStreamDemo1 {
    public static void main(String[] args) throws IOException {
/*
利用转换流按照指定的字符编码读取,替代方法
 */
   /* InputStreamReader isr = new InputStreamReader(new FileInputStream("rubish\\src\\file\\a.txt"),"UTF-8");
    int ch;
    while ((ch = isr.read()) != -1){
        System.out.print((char)ch);
        }
    isr.close();*/
        FileReader fr = new FileReader("rubish\\src\\file\\a.txt", Charset.forName("UTF-8"));
        int ch;
        while((ch = fr.read())!=-1){
            System.out.print((char)ch);
        }
    fr.close();
    }
}

利用转换流按照指定字符编码写出

java 复制代码
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;

public class ConvertStreamDemo2 {
    public static void main(String[] args) throws IOException {
            /*
            利用转换流按照指定字符编码写出
             */
     /*   OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("rubish\\src\\file\\a.txt"),"UTF-8");
        osw.write("rubish");
        osw.close();*/
        FileWriter fw = new FileWriter("rubish\\src\\file\\a.txt", Charset.forName("UTF-8"));
        fw.write("Hello World");
        fw.close();
    }
}

将本地文件中的GBK文件转成UTF-8

java 复制代码
import java.io.*;
import java.nio.charset.Charset;

public class ConvertStreamDemo3 {
    public static void main(String[] args) throws IOException {
    /*
    将本地文件中的GBK文件转成UTF-8
     */
//    jdk11以前的方案
       /* InputStreamReader isr = new InputStreamReader(new FileInputStream("rubish\\src\\file\\a.txt"), "GBK");
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("rubish\\src\\file\\b.txt"),"UTF-8");
    int b;
    while((b=isr.read())!=-1){
        osw.write(b);
    }
    osw.close();
    isr.close();*/
        FileReader fr = new FileReader("rubish\\src\\file\\a.txt", Charset.forName("GBK"));
        FileWriter fw = new FileWriter("rubish\\src\\file\\b.txt", Charset.forName("UTF-8"));
        int b;
        while((b = fr.read())!=-1){
            fw.write(b);
        }
        fw.close();
        fr.close();
    }
}

利用字节流读取文章,且不出现乱码

java 复制代码
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class ConvertStreamDemo4 {
    /*
    利用字节流读取文章,且不出现乱码
     */
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("rubish\\src\\file\\a.txt")));
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }
}

注:注释的代码为jdk11之前书写的方式

序列化流与反序列化流

java 复制代码
import java.io.Serializable;

public class Teacher implements Serializable {
    /*
    Serializable接口里面是没有抽象方法,标记型接口
    一旦实现了这个接口,那么就表示当前的Student类可以序列化
     */
    private static final long serialVersionUID = 1L;
    private String name;
    private int age;
//transient 瞬态关键字
//    作用:不会把当前属性序列化到本地文件当中
    public Teacher(){}
    public  Teacher(String name,int age){
        this.name=name;
        this.age=age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
public String toString(){
        return "Student{name = " + name + ", age =" + age + "}";
}

}

序列化

java 复制代码
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

public class ObjectStreamDemo1 {
    public static void main(String[] args) throws Exception {
        Teacher Teacher = new Teacher("Zhangsan",22);
//        使用对象输出流将对象保存到文件时会出现NoSerializableException
//        解决方法:需要让javabean类实现Serializable接口
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("rubish\\src\\file\\a.txt"));
        oos.writeObject(Teacher);
        oos.close();
    }
}

反序列化

java 复制代码
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.ObjectInputStream;

public class ObjectStreamDemo2 {
    public static void main(String[] args) throws Exception {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("rubish\\src\\file\\a.txt"));
        Object o = ois.readObject();
        System.out.println(o);
        ois.close();
    }
}

打印流

打印流只有两种,字符打印流,字节打印流

字符打印流

java 复制代码
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;

public class PrintStreamDemo1 {
    public static void main(String[] args) throws Exception{
//        创建字节打印流的对象
        PrintStream ps = new PrintStream(new FileOutputStream("rubish\\src\\file\\a.txt"),true, Charset.forName("UTF-8"));
        ps.println(97);
        ps.print(true);
        ps.println();
        ps.printf("%s 爱上了 %s","阿珍","阿强");
        ps.close();
    }
}

字节打印流

java 复制代码
import java.io.FileWriter;
import java.io.PrintWriter;

public class PrintStreamDemo2 {
    public static void main(String[] args) throws Exception{
        PrintWriter pw = new PrintWriter(new FileWriter("rubish\\src\\file\\a.txt"),true);
        pw.println("1234567890");
        pw.print("hello");
        pw.printf("%s 爱上了 %s","阿珍","阿强");
        pw.close();
    }
}

解压缩流和压缩流

解压缩文件

java 复制代码
{
    public static void main(String[] args) throws IOException {
        File src = new File("rubish\\src\\file\\c.zip");
        File dest = new File("rubish\\src\\file");
        unzip(src,dest);
    }
    public static void unzip(File src, File dest) throws IOException {
        ZipInputStream zip = new ZipInputStream(new FileInputStream(src));
        ZipEntry entry;
        while ((entry = zip.getNextEntry()) != null) {
            System.out.println(entry);
            if (entry.isDirectory()) {
//            文件夹:需要在目的地dest除创建一个同样的文件夹
                File file = new File(dest, entry.toString());
                file.mkdirs();
            }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();
    }
}

压缩流

压缩文件

java 复制代码
import java.io.File;

public static class ZipStreamDemo2 {
//    压缩单个文件
    public static void main(String[] args) throws IOException {
        File src = new File("rubish\\src\\file\\a.txt");
        File dest = new File("rubish\\src\\file");
        toZip(src,dest);
    }
}

public static void toZip(File src,File dest) throws IOException {
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(dest,"a.zip")));
    ZipEntry entry = new ZipEntry("a.txt");
    zos.putNextEntry(entry);
    FileInputStream fis = new FileInputStream(src);
    int b;
    while ((b = fis.read()) != -1) {
        zos.write(b);
    }
    zos.closeEntry();
    zos.close();
}

压缩文件夹

java 复制代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipStreamDemo3 {
//    压缩文件夹
    public static void main(String[] args) throws IOException {
        File src = new File("rubish\\src\\file");
//        创建压缩包父级路径
        File destParent = src.getParentFile();
        File dest = new File(destParent,src.getName()+".zip");
//        创建压缩流关联压缩包
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(dest));
        zos.close();
    }
    public static void toZip(File src,ZipOutputStream zos,String name) throws IOException {
        File[]  files = src.listFiles();
        for (File file : files) {
            if (file.isFile()){
            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 {
                toZip(file,zos,name+"\\"+file.getName());
            }
        }
    }
}
相关推荐
来杯@Java10 小时前
图书管理系统(基于springboot+vue前后端分离的项目)计算机毕业设计java
java·spring boot·spring·vue·毕业设计·mybatis·课程设计
卷毛的技术笔记11 小时前
告别硬编码!Spring AI Alibaba 实现 AI Agent 智能工具调用(Tool Calling)
java·人工智能·后端·python·spring·ai编程
编程大师哥11 小时前
匿名函数 lambda + 高阶函数
java·python·算法
isyangli_blog11 小时前
OpenDayLight (Carbon 版本) 启动与组件安装
开发语言·php
vb20081111 小时前
FastAPI APIRouter
开发语言·python
Benszen11 小时前
KVM虚拟化解决方案
开发语言·perl
会编程的土豆11 小时前
Go 语言反射(Reflection)详解
开发语言·后端·golang
東雪木11 小时前
多线程与并发编程 专属复习笔记
java·开发语言·笔记·java面试
GHL28427109011 小时前
换脸工作流学习
学习·ai
adrninistrat0r11 小时前
Java调用链MCP分析工具
java·python·ai编程