Java学习记录 Day18(流的补充)

Day 18

2019年5月19日。
这是我学习Java的第十八天。
这一天,我学到了以下的知识。

数据流

数据流,可以写和读基本数据类型

  • DataInputStream
    构造方法:public DataInputStream(InputStream in)使用指定的底层 InputStream 创建一个 DataInputStream
    成员方法
    public final int read(byte[] b) throws IOException从包含的输入流中读取一定数量的字节,并将它们存储到缓冲区数组 b 中
    public final int read(byte[] b, int off, int len) throws IOException从包含的输入流中将最多 len 个字节读入一个 byte 数组中

  • DataOutputStream
    构造方法:public DataOutputStream(OutputStream out)创建一个新的数据输出流,将数据写入指定基础输出流
    成员方法
    public void write(int b) throws IOException将指定字节(参数 b 的八个低位)写入基础输出流
    public void write(byte[] b, int off, int len) throws IOException将指定 byte 数组中从偏移量 off 开始的 len 个字节写入基础输出流

内存操作流

内存操作流,不关联文件,也不直接读写文件,就在内存中操作数据
特点:此流关闭无效,所以无需关闭

  • ByteArrayInputStream
    构造方法:public ByteArrayInputStream(byte[] buf)创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组
    public ByteArrayInputStream(byte[] buf, int offset, int length)创建 ByteArrayInputStream,使用 buf 作为其缓冲区数组
    成员方法
    public int read()从此输入流中读取下一个数据字节。返回一个 0 到 255 范围内的 int 字节值。如果因为到达流末尾而没有可用的字节,则返回值 -1
    public int read(byte[] b, int off, int len)将最多 len 个数据字节从此输入流读入 byte 数组

  • ByteArrayOutputStream
    构造方法:public ByteArrayOutputStream()创建一个新的 byte 数组输出流
    public ByteArrayOutputStream(int size)创建一个新的 byte 数组输出流,它具有指定大小的缓冲区容量(以字节为单位)
    成员方法
    public void write(int b)将指定的字节写入此 byte 数组输出流
    public void write(byte[] b, int off, int len)将此 byte 数组输出流的全部内容写入到指定的输出流参数中,这与使用 out.write(buf, 0, count) 调用该输出流的 write 方法效果一样

打印流

打印流,只能操作目的地,不能操作数据源(不能进行读取数据)
特点:可以操作任意数据类型的数据,调用print()方法可以写任意数据类型

  • PrintStream:字节打印流
    构造方法:public PrintStream(OutputStream out)创建新的打印流。此流将不会自动刷新
    成员方法
    public void flush()刷新该流的缓冲
    public void write(byte[] buf, int off, int len)将 len 字节从指定的初始偏移量为 off 的 byte 数组写入此流
    public void print(int b)打印值

  • PrintWriter:字符打印流
    构造方法:public PrintWriter(Writer out)创建不带自动行刷新的新 PrintWriter
    成员方法
    public void flush()刷新该流的缓冲
    public void write(char[] buf, int off, int len)写入字符数组的某一部分
    public void print(int b)打印值

随机访问流

随机访问流,是Object类的子类。但它融合了InputStream和OutputStream的功能,支持对随机访问文件的读取和写入,可以操作任意类型的数据。
特点:能读能写

  • RandomAccessFile
    构造方法:public RandomAccessFile(File file, String mode) throws FileNotFoundException创建从中读取和向其中写入(可选)的随机访问文件流,该文件由 File 参数指定
    成员方法
    public int read() throws IOException从此文件中读取一个数据字节,以整数形式返回此字节
    public void write(byte[] b, int off, int len) throws IOException将 len 个字节从指定 byte 数组写入到此文件,并从偏移量 off 处开始
    public void seek(long pos) throws IOException设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作
    public long getFilePointer() throws IOException返回此文件中的当前偏移量

序列化

序列化,就是把对象通过流的方式存储到文件中,注意:此对象要重写Serializable 接口才能被序列化
相对于序列化,反序列化,就是把文件中存储的对象以流的方式还原成对象

  • ObjectOutputStream:序列化流
    构造方法:public ObjectOutputStream(OutputStream out) throws IOException创建写入指定 OutputStream 的 ObjectOutputStream
    成员方法
    public final void writeObject(Object obj) throws IOException将指定的对象写入 ObjectOutputStream

  • ObjectInputStream:反序列化流
    构造方法:public ObjectInputStream(InputStream in) throws IOException创建从指定 InputStream 读取的 ObjectInputStream
    成员方法
    public final Object readObject() throws IOException, ClassNotFoundException从 ObjectInputStream 读取对象

注意:

transient:使用transient关键字,可以声明不需要序列化的成员变量,例如:private transient int age ;// 使用transient可以阻止成员变量的序列化

Properties类

Properties,表示了一个持久的属性集,可保存在流中或从流中加载,属性列表中每个键及其对应值都是一个字符串,其父类是Hashtable,属于双列集合。这个集合中的键和值都是字符串,且Properties不能指定泛型
构造方法:public Properties()创建一个无默认值的空属性列表
成员方法
public Object setProperty(String key, String value)调用 Hashtable 的方法 put
public String getProperty(String key)用指定的键在此属性列表中搜索属性
public void load(Reader reader)读取键值对数据把数据存储到Properties中
public void store(Writer writer, String comments)把Properties集合中的键值对数据写入到文件中, comments注释

顺序流

顺序流,表示其他输入流的逻辑串联
构造方法:public SequenceInputStream(InputStream s1, InputStream s2)通过记住这两个参数来初始化新创建的 SequenceInputStream(将按顺序读取这两个参数,先读取 s1,然后读取 s2),以提供从此 SequenceInputStream 读取的字节
成员方法
public int read(byte[] b, int off, int len) throws IOException将最多 len 个数据字节从此输入流读入 byte 数组
public void close() throws IOException关闭此输入流并释放与此流关联的所有系统资源

猜你喜欢

转载自blog.csdn.net/qq_41151659/article/details/90349033
今日推荐