文件流和对象流(序列化)打印流

package _03;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class _3_25DataStream {
    public static void main(String[] args) throws IOException {
//        方便处理八的基本数据类型
//        直接获取,不需要我们以后强转了
//        File、ByteArray 属于底层的节点流

//        1.字节流 先写到字节内存中
        ByteArrayOutputStream bos = new ByteArrayOutputStream();  //不需要引流
        DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(bos));   //还可以再加缓冲流
        dos.writeUTF("Wei");
        dos.writeDouble(12.34);
        dos.writeBoolean(false);
        dos.flush();

//        2.读取
        byte[] datas = bos.toByteArray(); //模拟那块节点
        ByteArrayInputStream bis = new ByteArrayInputStream(datas);
        DataInputStream dis = new DataInputStream(bis);
        //读取
        String str = dis.readUTF();
        System.out.println(str);
    }
}

package _03;

import java.io.*;
import java.util.Date;

public class _3_25ObjectStream {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //除了八大基本类型,还有各种自定义类型的对象
        //序列化: Object-> ByteArray  Serialization   反序列化: ByteArray->Object DeSerialization
        //自己定义的类想序列化 必须继承Serializable接口 (通行证)
        // 输入和输出流中多了  read 和 writeObject() 方法

        // 1.字节流 先写到字节内存中
        ByteArrayOutputStream bos = new ByteArrayOutputStream();  //不需要引流
        ObjectOutputStream dos = new ObjectOutputStream(new BufferedOutputStream(bos));   //还可以再加缓冲流
        dos.writeObject(new Date());  //new Data 也实现了Serializable接口
        dos.writeUTF("Wei");
        dos.writeDouble(12.34);
        dos.writeBoolean(false);

        dos.flush();

//        2.读取
        byte[] datas = bos.toByteArray(); //模拟那块节点
        ByteArrayInputStream bis = new ByteArrayInputStream(datas);
        ObjectInputStream dis = new ObjectInputStream(bis);
        //读取  按照顺序 不需要也要读取
        Object ob = dis.readObject();
        if( ob instanceof  Date){
            Date ob1 = (Date)ob;
        }
        System.out.println(ob);

        //在自己写的对象中,某个属性不需要序列化  加transient 关键字
        //序列化另一个名称  持久化 存储

    }
}
package _03;

import java.io.*;

public class _3_26PrintStream {
    public static void main(String[] args) throws FileNotFoundException {
        //我们所用的System.out 就是这个流
        PrintStream  ps = System.out;
        ps.println("Process finished with exit");


        ps = new PrintStream(new BufferedOutputStream(new FileOutputStream("d:/test.txt")));
        ps.println("乌龙茶");
        ps.flush();

        //重定向输出端
        System.setOut(ps);
        System.out.println("瑞幸");   //输出到d:/test.txt中

        //重定向回来   标准的输入输出端
        ps = new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)),true);
        ps.close();


        //与PrintStream相对应的还有PrintWrite
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream("d:/test.txt")));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值