zoukankan      html  css  js  c++  java
  • 序列化(对象)流、打印流

    序列化(对象)流

    定义

    Java 提供了一种对象序列化的机制。用一个字节序列可以表示一个对象,该字节序列包含该对象的数据对象的类型对象中存储的属性等信息。字节序列写出到文件之后,相当于文件中持久保存了一个对象的信息。

    反之,该字节序列还可以从文件中读取回来,重构对象,对它进行反序列化对象的数据对象的类型对象中存储的数据信息,都可以用来在内存中创建对象。

    序列化实现条件

    1. 一个对象要想序列化,必须满足两个条件:
    • 该类必须实现java.io.Serializable 接口,Serializable 是一个标记接口,不实现此接口的类将不会使任何状态序列化或反序列化,会抛出NotSerializableException
    • 该类的所有属性必须是可序列化的。如果有一个属性不需要可序列化的,则该属性必须注明是瞬态的,使用transient 关键字修饰。

    static修饰的属性无法进行序列化

    序列化输入流

    public class ObjectInputStream extends InputStream implements ObjectInput, ObjectStreamConstants:继承了InputStream类。

    常用方法

    • public final Object readObject()throws IOException,ClassNotFoundException:获取文件中的对象;
    • public void close() throws IOException:关闭流对象。

    序列化输出流

    public class ObjectOutputStream extends OutputStream implements ObjectOutput, ObjectStreamConstants:继承了OutputStream类。

    常用方法

    • public final void writeObject(Object obj) throws IOException:输出对象;
    • public void close() throws IOException:关闭流对象。

    打印流

    定义:

    java.io.PrintStream,一般使用它在控制台进行输出,但是它也可以输出到我们制定的路径中。

    构造方法

    • public final static PrintStream out:这个是System类中底层用其他语言进行实现。能在控制台打印,能写入到文件中。
    • new PrintStream("ps.txt"):这个流是创建需要输出到哪个路径的流,不能在控制台打印,只能写入到文件中。

    常用方法

    • public void println(String x):输出数据,能在控制台打印,能写入到文件中。
    • public void write(int b):不能在控制台打印,只能写入到文件中。
    • public static void setOut(PrintStream out):位于System类中,设置输出方向。
    XFS
  • 相关阅读:
    D-Bus,kdbus和Binder
    Android init system replaced with systemd & binder replaced by kdbus
    在 Android 上 chroot 一个 ArchLinux
    An experiment in porting the Android init system to GNU/Linux
    Android init
    Linux和RISC-V基金会宣合作,打造开源CPU!
    How does systemd use /etc/init.d scripts?
    SysV, Upstart and systemd init script coexistence
    Purism Shows Off Latest GNOME Mobile Shell Mockups For The Librem 5
    One Widget to Adapt Them All and to The Librem 5 Port Them
  • 原文地址:https://www.cnblogs.com/xiaofengshan/p/14732481.html
Copyright © 2011-2022 走看看