Java高级Day34-流补充

97.对象处理流

对象流-ObjectInputStream和ObjectOutputStream

具体说明:

以前 int n = 100; 是在文件内保存了100,但现在的需求是要在文件内保存 int 100

即保存值和数据类型

这样的保存叫做 -> 序列化,把其重新恢复就叫 -> 反序列化

注意:需要让某个对象支持序列化机制,则必须让其类是可序列化的,为了让某个类是可序列化的,类必须实现Serializable接口(标记接口,没有方法)或者Externalizable接口

ObjectOutputStream 提供 序列化功能

//使用ObjectOutputStream 序列化 基本数据类型和一个Dog对象,并保存到data.dat文件中
public class HelloJava {
    public static void main(String[] args) throws Exception {
        //序列化后,保存的文件格式,不是纯文本,而是按照它的格式来保存
        String filePath = "d:\\data.dat";
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
        //序列化数据到 d:\data.dat
        oos.writeInt(100);//int -> Integer(实现了Serializable)
        oos.writeBoolean(true);//boolean -> Boolean(实现了Serializable)
        oos.writeChar('a');//char -> Character(实现了Serializable)
        oos.writeDouble(9.5);//double -> Double(实现了Serializable)
        oos.writeUTF("Hello,World");//String(实现了Serializable)
        //保存一个Dog对象
        oos.writeObject(new Dog("旺财",10));
        oos.close();
        System.out.println("数据保存完毕(序列化形式)");
    }
}
​
//如果需要序列化某个类的对象,需要实现Serializable接口
class Dog implements Serializable {
    private String name;
    private int age;
​
    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

ObjectInputStream 提供 反序列化功能

//使用ObjectInputStream 读取 data.dat 并反序列化数据
public class HelloJava {
    public static void main(String[] args) throws Exception {
        //指定反序列化的文件
        String filePath = "d:\\data.dat";
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
        //读取
        //注意:读取的顺序要和保存的顺序一致,否则会报异常
        System.out.println(ois.read());
        System.out.println(ois.readBoolean());
        System.out.println(ois.readChar());
        System.out.println(ois.readDouble());
        System.out.println(ois.readUTF());
        System.out.println(ois.readObject());//底层 Object -> Dog
​
        //如果我们希望调用Dog的方法,需要向下转型
        Dog dog2 = (Dog)dog;
        System.out.println(dog2.getName());
​
        //关闭流,关闭外层流即可,底层会关闭 FileInputStream 流
        ois.close();
    }
}

98.对象处理流使用细节

  1. 读写顺序要一致

  2. 要求序列化或反序列化对象,需要 实现Serializable

  3. 序列化的类中建议添加SerialVersionUID,为了提高版本的兼容性

  4. 序列化对象时,默认将里面所有属性都进行序列化,但除了static或transient修饰的成员

  5. 序列化对象时,要求里面属性的类型也需要实现序列化接口

  6. 序列化具备可继承性,也就是如果某类已经实现了序列化,则它的所有子类也默认实现了序列化

99.标准输入输出流

System.in 标准输入 InputStream 键盘

System.out标准输出 PrintStream 显示器

public class InputAndOutput {
    public static void main(String[] args){
        //System 类 的 public final static InputStream in = null
        //System.in 编译类型  InputStream
        //System.in 运行类型  BufferedInputStream
        System.out.println(System.in.getClass());
        
        //System.out public final static PrintStream out = null
        //编译类型 PrintStream
        //运行类型 PrintStream
        System.out.println(System.out.getClass());
    }
}

100.乱码引出转换流

当处理纯文本数据时,如果使用字符流效率更高,并且可以有效解决中文问题,所有建议将字节流转换成字符流

public class HelloJava {
    public static void main(String[] args) throws Exception {
        //读取e:\\a.txt 文件到程序
        //1.创建字符输入流 BufferedReader [处理流]
        //2.使用 BufferedReader 对象读取a.txt
        //3.默认情况下,读取文件是按 utf-8 编码
​
        String filePath = "e;\\a.txt";
        BufferedReader br = new BufferedReader(new FileReader(filePath));
        String s = br.readLine();
        System.out.println("读取到的内容:" + s);
        br.close();
    }
}
//若文件不是utf-8编码,则读取出来会乱码
转换流-InputStreamReader
public class HelloJava {
    public static void main(String[] args) throws Exception {
        String filePath = ":\\a.txt";
        //new FileInputStream(filePath) 转成 InputStreamReader
        //指定编码 gbk
        InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath),"gbk");
        //把 InputStreamReader 传入 BufferedReader
        BufferedReader br = new BufferedReader(isr);
        //读取
        String s = br.readLine();
        System.out.println("读取内容=" + s);
        //关闭外层流
        br.close();
    }
}
​
==================================================================================
​
//开发中可能会这么写
public class HelloJava {
    public static void main(String[] args) throws Exception {
        String filePath = "d:\\a.txt";
        //1.把 FileInputStream(filePath) 转成 InputStreamReader
        //2.指定编码 gbk
//        InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath),"gbk");
        //3.把 InputStreamReader 传入 BufferedReader
//        BufferedReader br = new BufferedReader(isr);
​
        //将2和3合在一起写
        BufferedReader br = new BufferedReader(
                             new InputStreamReader(new FileInputStream(filePath),"gbk"));
        //4.读取
        String s = br.readLine();
        System.out.println("读取内容=" + s);
        //5.关闭外层流
        br.close();
    }
}
转换流-OutputStreamWriter
//编程将 字节流 FileOutputStream 包装成(转换成)字符流OutputStreamWriter对文件进行写入
public class HelloJava {
    public static void main(String[] args) throws Exception {
        String filePath = "d:\\java.txt";
        String charSet = "gbk";
        OutputStreamWriter osw = new OutputStreamWriter(
                                  new FileOutputStream(filePath), "gbk");
        osw.write("hi,你好");
        osw.close();
        System.out.println("按照" + charSet + "保存文件");
    }
}

101.打印流-PrintStream 和 PrintWriter

打印流只有输出流,没有输入流

PrintStream

public class HelloJava {
    public static void main(String[] args) throws Exception {
        PrintStream out = System.out;
        //在默认情况下 PrintStream 输出数据的位置是 标准输出,即显示器
        out.print("join,hello");
        //因为print底层使用的是write,所以我们可以直接调用write进行打印/输出
        out.write("你好".getBytes());
        out.close();
​
        //我们可以去修改打印流的输出位置
        //"hello,你好" 就会输出到 e;\f1.txt
        System.setOut(new PrintStream("e:\\f1.txt"));
        System.out.println("hello,你好");
    }
}

PrintWriter

public class HelloJava {
    public static void main(String[] args) throws Exception {
        //PrintWriter printWriter = new PrintWriter(System.out);
        PrintWriter printWriter = new PrintWriter(new FileWriter("e:\\f2.txt"));
        printWriter.print("hi,北京你好~");
        printWriter.close();
    }
}
  • 19
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中,我们可以使用 Jackson 库来实现 LocalDateTime 类型的序列化和反序列化。下面是全局配置的示例代码: ```java import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new JavaTimeModule()); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); ``` 这里,我们创建了 ObjectMapper 对象,并注册了 JavaTimeModule,该模块支持 Java 8 中的日期和时间 API。然后,我们禁用了将日期序列化为时间戳的选项。 接下来,我们可以使用这个 ObjectMapper 对象来进行 LocalDateTime 类型的序列化和反序列化。例如,如果我们要将一个 LocalDateTime 对象序列化为 JSON 字符串,可以这样做: ```java LocalDateTime dateTime = LocalDateTime.now(); String json = objectMapper.writeValueAsString(dateTime); ``` 如果我们要将一个表示 LocalDateTime 的 JSON 字符串反序列化为 LocalDateTime 对象,可以这样做: ```java String json = "{\"year\":2021,\"month\":\"JULY\",\"dayOfMonth\":12,\"hour\":14,\"minute\":30,\"second\":0,\"nano\":0}"; LocalDateTime dateTime = objectMapper.readValue(json, LocalDateTime.class); ``` 这里,我们使用了 readValue 方法来将 JSON 字符串反序列化为 LocalDateTime 对象。注意,我们需要指定要反序列化的目标类型,即 LocalDateTime.class。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值