io中转换流,数据流以及对象流(序列化和反序列化)

本文深入探讨Java IO流的使用,包括转换流、数据流、对象流的特性与操作方法。通过示例代码,讲解如何以字符流形式操作字节流,指定字符集,以及基本数据类型和对象的序列化与反序列化过程。

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

io中转换流
1:以字符流的形式操作字节流
2:可以指定字符集

package cn.io.com;
import java.io.*;
import java.net.URL;

public class TestConvertStream {
    public static void main(String[] args) {
        //以字符流的形式操作字节流(纯文本)
        //指定字符集
        //test01();
        //test02();
        //test03();
        convert();
    }
    public static void convert() {
        try(BufferedReader reader  = new BufferedReader(new InputStreamReader(System.in)) ;
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out))) {
            String str ="";
            while(!str.equals("end")) {
                str = reader.readLine(); //逐行读取
                writer.write(str); //逐行写出
                writer.newLine();
                writer.flush();
            }
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
    public static void test01() {
        try(InputStream is = new URL("http://www.baidu.com").openStream()) {
            int tmp;
            while((tmp =is.read()) != -1) {
                System.out.print((char) tmp); //发生乱码的原因是字节数不够
            }
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
    public static void test02() {
        try(InputStreamReader is = new InputStreamReader(new URL("http://www.baidu.com").openStream(),"UTF-8")) {
            int tmp;
            while((tmp = is.read()) != -1) {
                System.out.print((char) tmp);
            }
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
    public static void test03() {
        try(BufferedReader reader = new BufferedReader
                (new InputStreamReader(new URL("http://www.baidu.com").openStream(),"UTF-8"))) {
            String str = null;
            while((str = reader.readLine()) != null) {
                System.out.println(str);
            }
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
    public static void test04() {
        try(BufferedReader reader = new BufferedReader(new InputStreamReader
                (new URL("http://www.baidu.com").openStream(),"UTF-8"));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter
                    (new FileOutputStream("baidu.html"),"UTF-8"))) {
            String  str; //发生乱码的原因是没有指定的字符集
            while((str =reader.readLine()) != null) {
                writer.write(str);
                writer.newLine();
            }
            writer.flush();
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
}

io中的数据流(DataOutputStream()/DataInputstream()),操作的是基本数据类型以及字符串,任何数据都可以转换成字节数组,以方便求大小等,字节数组流不需要关闭

package cn.io.com;
import java.io.*;
public class TestDataStream {
    public static void main(String[] args) throws IOException {
        //写入
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(baos));
        //操作数据类型+数据 任何的数据都可以转成字节数组 转换成字节数组/占用了多少个字节
        //  不需要关闭流
        dos.writeUTF("我太难了");
        dos.writeInt(18);
        dos.writeBoolean(true);
        dos.writeChar('z');
        dos.flush();
        byte[] datas = baos.toByteArray();
        System.out.println(datas.length);
        //读取
        DataInputStream dis = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
        //顺序与写出一致
        String str = dis.readUTF();
        int age = dis.readInt();
        boolean flag = dis.readBoolean();
        char ch = dis.readChar();
        System.out.println(age);

    }
}

io中的对象流(序列化和反序列化),序列化就是对象输出流,反序列化就是对象输入流
1, 对象流 先写出,后读取
2 ,读取的顺序与写出的保持一致
3 ,不是所有的对象都可以序列化 必须实现Serializable接口
对象读取的时候需要用instanceof来判断左边是不是右边的实例,自己实现的类需要实现Serializable接口,某个属性不需要被别人看见使用transient隐藏

在这里插入图片描述

package cn.io.com;
import java.io.*;
import java.util.Date;

public class TestDataStream {
    public static void main(String[] args) {
        //1 对象流  先写出,后读取
        //2 读取的顺序与写出的保持一致
        //3 不是所有的对象都可以序列化 必须实现Sreializable接口
        //写出   序列化
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("zxc.jre"));
            oos.writeUTF("每一天都应该为了自己更加努力");
            oos.writeInt(33);
            oos.writeChar('n');
            oos.writeBoolean(true);
            oos.writeObject("找到一份好的工作");
            Employee employee = new Employee("小红",18);
            oos.writeObject(employee);

            ois = new ObjectInputStream(new FileInputStream("zxc.jre"));
            String str = ois.readUTF();
            System.out.println(str);
            int age = ois.readInt();
            System.out.println(age);
            char ch = ois.readChar();
            System.out.println(ch);
            boolean flag = ois.readBoolean();
            System.out.println(flag);
            try {
                Object str2 = ois.readObject();
                Object employee2 = ois.readObject();
                if (str2 instanceof String) {
                    String string = (String) str2;
                    System.out.println(string);
                }
                if (employee2 instanceof Employee) {
                    Employee employ = (Employee) employee2;
                    System.out.println(employ.getName()+"--->"+employ.getSalary());
                }

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }catch(IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (ois != null) {
                    ois.close();
                }
            }catch(IOException e) {
                e.printStackTrace();
            }
            try {
                if (oos != null) {
                    oos.close();
                }
            }catch(IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void testData() {
        //写出
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(baos));
        //操作数据类型+数据 任何的数据都可以转成字节数组 转换成字节数组/占用了多少个字节
        //  不需要关闭流
        try {
            dos.writeUTF("我太难了");
            dos.writeInt(18);
            dos.writeBoolean(true);
            dos.writeChar('z');
            dos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

        byte[] datas = baos.toByteArray();
        System.out.println(datas.length);
        //读取
        DataInputStream dis = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
        //顺序与写出一致
        try {
            String str = dis.readUTF();
            int age = dis.readInt();
            boolean flag = dis.readBoolean();
            char ch = dis.readChar();
            System.out.println(age);
        }catch(IOException e) {
            e.printStackTrace();
        }

    }
    public static void testSerializable() {
        //1 对象流  先写出,后读取
        //2 读取的顺序与写出的保持一致
        //3 不是所有的对象都可以序列化 必须实现Sreializable接口
        //写出   序列化
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeUTF("展示你的代码");
            oos.writeInt(16);
            oos.writeChar('N');
            oos.writeBoolean(false);
            oos.writeObject("少说话,多敲代码");
            oos.writeObject(new Date());
            Employee emp = new Employee("小明",18);
            oos.writeObject(emp);
            oos.flush();

            byte[] bytes = baos.toByteArray();
            //读取  反序列化
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);

            String str = ois.readUTF();
            int age = ois.readInt();
            char ch = ois.readChar();
            boolean flag = ois.readBoolean();
            //对象的数据还原
            Object str1 = ois.readObject();
            Object date = ois.readObject();
            Object employee = ois.readObject();
            if (str1 instanceof String) {
                String strStr1 = (String)str1;
                System.out.println(strStr1);
            }
            if (date instanceof Date) {
                Date dateDate = (Date)date;
                System.out.println(dateDate);
            }
            if (employee instanceof Employee) {
                Employee employee1 = (Employee) employee;
                System.out.println(employee1.getName()+"---->"+employee1.getSalary());
            }
        }catch(ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class Employee implements Serializable{
    private  String name; //该数据不需要序列化 transient
    private  double salary;

    public Employee() {

    }

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值