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());
            }
        }
    }
}
相关推荐
愤豆2 小时前
08-Java语言核心-JVM原理-垃圾收集详解
java·开发语言·jvm
逸Y 仙X2 小时前
文章十四:ElasticSearch Reindex重建索引
java·大数据·数据库·elasticsearch·搜索引擎·全文检索
VelinX2 小时前
【个人学习||算法】贪心算法
学习·算法·贪心算法
wregjru2 小时前
【读书笔记】Effective C++ 条款8:别让异常逃离析构函数
java·开发语言
harder3212 小时前
Swift 面向协议编程的 RMP 模式
开发语言·ios·mvc·swift·策略模式
艾莉丝努力练剑2 小时前
【QT】QT快捷键整理
linux·运维·服务器·开发语言·图像处理·人工智能·qt
程序员_大白2 小时前
【2025版】最新Qt下载安装及配置教程(非常详细)零基础入门到精通,收藏这篇就够了
开发语言·qt
枫叶丹42 小时前
【HarmonyOS 6.0】ArkData 分布式数据对象新特性:资产传输进度监听与接续传输能力深度解析
开发语言·分布式·华为·wpf·harmonyos
冷血~多好2 小时前
mysql实现主从复制以及springboot实现读写分离
java·数据库·mysql·springboot