读源码之旅 java.io包
对于下图,研究了一下常用的InputStream,ByteArrayInputStream,BufferedInputStream,FileIputStream,ObjectInputStream,DataInputStream 以及相对应的OutStream类。

看完还是有一些收获的: 1、对其整体结构更清晰了一些,基本上在什么场合想用哪一个流心里都比较有数了。
2、明白了为什么一些类在write后,还要flush一下。举一个例子,看一下BufferedOutputStream里面的几个方法就明白
public synchronized void write(int b) throws IOException {
if (count >= buf.length) {
flushBuffer();
}
buf[count++] = (byte)b; //这里的write作用是把数组放入buf数组,作当缓冲
}
public synchronized void flush() throws IOException {
flushBuffer(); //写入数据
out.flush(); //调用包装的类的flush方法
}
private void flushBuffer() throws IOException {
if (count > 0) {
out.write(buf, 0, count);
count = 0;
}
}
3、有一些Stream关闭不关闭都是一样的,例如像 ByteArrayInputStream
4、ByteArrayInputStream类有如下方法:
public synchronized int read() {
return (pos < count) ? (buf[pos++] & 0xff) : -1;
}
注意这里的 0xff,搜索一些资料
0xFF is hexadecimal, you can Wikipedia that part. FF is a representation of 00000000 00000000 00000000 11111111 (a 32-bit integer) & means bit-wise "and", and so when you use it on two ints, each pair of bits from those two ints is and-ed and the result is placed in the resultant int: Example (showing 16 bits only)
0101 1100 1010 1100 &0000 0000 1111 1111
=0000 0000 1010 1100
Bytes are signed in Java. In binary 0x00 is 0, 0x01 is 1 and so on but all 1s (ie 0xFF) is -1, oxFE is -2 and so on. See Two's complement, which is the binary encoding mechanism used. http://en.wikipedia.org/wiki/Two's_complement
5、比较复杂的类:ObjectOutputStream,要花多一些时间去看
6、研究RandomAccessFile类,可以复习一下按位运算符。 运算符 符号 按位与 & 按位左移 << 按位取反 ~ 按位或 | 按位右移 >> 按位异或 ^ 无符号右移 >>>