IO流的实现

本文深入探讨了Java中的IO流,包括输入流、输出流的基本概念,以及如何在后端开发中使用它们进行文件读写、网络数据传输等操作。通过实例解析了不同类型的流(字节流与字符流)及其组合使用,还涵盖了缓冲流和转换流的运用,为提升Java后端开发中的数据处理能力提供了关键理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package m1;

import jdk.nashorn.internal.ir.IfNode;

import java.io.*;
import java.util.Arrays;
import java.util.concurrent.TransferQueue;

/**
 * @author Yzt
 * @version 1.0
 * @date 2021/10/25 20:46
 */
public class IO1 {
    public static void main(String[] args) {
//        f1();//普通字节流读取
//        f2();//高效字节流读取
//        f3();//普通字符读取
//        f4();//高效字符读取
//        f5();//普通字节输出流
//        f6();//高效字节输出流
//        f7();//普通字符输出流
//        f8();//高效字符输出流
        /**
         * 序列化是指把程序中给的Java对象,永久保存的磁盘当中,相当于写出的过程 对应out   ObjectOutputStream
         * 反序列化与之相反  对应in   ObjectInputStream
         * 注意:反序列化时,最好是一次序列化一次反序列化,因为若是有改动的话会导致UID不一致导致报错,或在类中指定uid,这样就不会报错了*/
            f9();//测试序列化
            f10();//测试反序列化

    }

    private static void f10() {
        ObjectInputStream in=null;
        try {
            in=new ObjectInputStream(new FileInputStream("G:\\1.txt"));
            Object o = in.readObject();
            System.out.println(o);//student类需要tostring,要不然打印的是地址值
            System.out.println("反序列化成功");

        } catch (Exception e) {
            System.out.println("反序列化失败");
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void f9() {
        ObjectOutputStream out=null;
        try {
           out=new ObjectOutputStream(new FileOutputStream("G:\\1.txt"));
           //准备对象
            Student student = new Student("牛魔",13,"火焰山","男");
            out.writeObject(student);
            System.out.println("序列化成功");
        } catch (IOException e) {
            System.out.println("序列化失败");
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void f8() {
        BufferedWriter out=null;
        try {
            out=new BufferedWriter(new FileWriter("G:\\2.txt",true));
            out.write(123);
            System.out.println("输出成功");
            out.flush();//防止数据遗漏
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void f7() {
        Writer out=null;
        try {
            out=new FileWriter("G:\\2.txt",true);
            out.write(100);
            System.out.println("输出成功");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void f6() {
        BufferedOutputStream out=null;
        try {
            out=new BufferedOutputStream(new FileOutputStream("G:\\1.txt",true));
            out.write(99);
            System.out.println("输出成功");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void f5() {
        OutputStream out=null;
        try {
            out=new FileOutputStream("G:\\1.txt",true);//默认直接覆盖原数据,要开启append
            out.write(97);
            System.out.println("输出成功");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void f4() {
        BufferedReader in=null;
        try {
            in=new BufferedReader(new FileReader(new File("G:\\1.txt")));
            int b;
            while ((b=in.read())!=-1){
                System.out.println(b);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void f3() {
        //创建一个初始化reader对象
        Reader in=null;
        try {
            in=new FileReader(new File("G:\\1.txt"));
            int b;
            while ((b=in.read())!=-1){
                System.out.println(b);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void f2() {
        BufferedInputStream in=null;//默认容量为8192字节
        try {
            in=new BufferedInputStream(new FileInputStream("G:\\1.txt"));
            int b;
            while ((b=in.read())!=-1){
                System.out.println(b);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void f1()  {
        //创建字节输入流读取
        InputStream in=null;//若不定义此成员变量,则关流时无法引用in
        try {
//            InputStream in=new FileInputStream(new File("G:\\1.txt"));
            in=new FileInputStream("G:\\1.txt");
//            System.out.println(in.read());
//            System.out.println(in.read());
//            System.out.println(in.read());
//            System.out.println(in.read());//-1若以达到文件末尾,则返回-1
            int b;//定义变量接受数据,不定义b的话会跳着读
            while ((b=in.read())!=-1){
                System.out.println(b);
            }//循环遍历读取信息


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
//测试序列化与反序列化
class Student implements Serializable{//必须实现可序列化接口
    private static final long serialVersionUID = 3096210940031231086L;
    private String name;
    private int age;
    private String addr;
    private String gender;

    public Student() {
        System.out.println("我是student的无参构造");
    }

    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 getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Student(String name, int age, String addr, String gender) {
        System.out.println("我是student的全参构造");
        this.name = name;
        this.age = age;
        this.addr = addr;
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", addr='" + addr + '\'' +
                ", gender='" + gender + '\'' +
                '}';
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

N13China

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值