Java IO,
分享于 点击 36822 次 点评:269
Java IO,
字节流(FileInputStream、DataInputStream、BufferedInputStream、 FileOutputSteam、DataOutputStream、BufferedOutputStream)
字符流 (BufferedReader、InputStreamReader、FileReader、BufferedWriter、 OutputStreamWriter、PrintWriter、FileWriter)
字符流和字节流的区别:
- 字符流按字符读取,字节流按字节读取
- 字符流一般读写文本,字节流一般进行与文本内容无关的操作。
源码分析:
public
class BufferedOutputStream extends FilterOutputStream
public BufferedOutputStream(OutputStream out) {
this(out, 8192);
}
装饰模式
同步
public synchronized void write(byte b[], int off, int len) throws IOException {
if (len >= buf.length) {
flushBuffer();
out.write(b, off, len);
return;
}
if (len > buf.length - count) {
flushBuffer();
}
System.arraycopy(b, off, buf, count, len);
count += len;
}
public void write(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
for (int i = 0 ; i < len ; i++) {
write(b[off + i]);
}
}
public synchronized void write(int b) throws IOException {
if (count >= buf.length) {
flushBuffer();
}
buf[count++] = (byte)b;
}
private void flushBuffer() throws IOException {
if (count > 0) {
out.write(buf, 0, count);
count = 0;
}
}
写入缓存,再写入文件
相关文章
- 暂无相关文章
用户点评