首页 > 技术文章 > Java.IO note

StevenL 2015-11-29 20:45 原文

Java.IO

File Class:

Path Interface and Files Class is part of NIO System.

AutoCloseable, Closeable, Flushable Interface

Closeable, Flushable is part of java.io

AutoCloseable is part of java.lang

Only Class implemented AutoCloseable Interface can deal with try  statement

I/O Exception:

IOException

SecurityException (most on applet)

Close Stream:

close()

try-with-resources:

Must implement AutoCloseable Interface

Resource in try statement is declared as final

Use “;” can deal multi-resource

ByteStream

过滤的字节流:

这些流一般是通过接收通用流的方法访问的,通用流是过滤流的超类。

BufferedInputStream/BufferedOutputStream

PushbackInputStream:

void unread(int b)

void unread(byte buf[])

void unread(byte buffer, iint offset, iint numBytes)

PushbackInputStream Object will make InputStream Object be  Unsupported to mark() and reset(). For any Stream using mark()  and reset(), should use markSupported() method first to check.

SequenceInputStream

PrintStream:

Implemented Interface Appendable, AutoCloseable, Closeable,  Flushable

printf() / format()

DataInputStream / DataOutputStream:

Write fundamental data type into Stream

Implemented Interface Appendable, AutoCloseable, Closeable,  Flushable

RandomAccessFile:

Implemented Interface DataInput and DataOutput, AutoCloseable  and Closeable.

void seek(long newPos) throws IOException

void setLength(long len) throws IOException

CharStream

Reader:

Implemented Interface AutoCloseable, Closeable, Readable

Writer:

Implemented Interface AutoCloseable, Closeable, Flushable,  Appendable

Console

In java.io

No constructor method, use System.console() obtain the Object

Method throws IOError: String readLine(), char[] readPassword.

IOError can not be catched, so it means a disaster to System.

串行化

串行化是将对象的状态写入字节流的过程。

Interface Seralizable

只有实现了此借口的类才能通过串行化功能进行板寸和恢复。此借口没有定义成员,只是用于指示可串行化。如果一个类是可串行化的,它的子类也是。声明为transient的变量不能通过串行化功能进行保存,也不能保存static变量。

Interface Externalizable

ObjectOutput implements DataOutput, AutoCloseable

ObjectOutputStream

ObjectInput implements DataInput, AutoCloseable

ObjectInputStream implements ObjectInput

 

 

推荐阅读