BufferedOutputStream 和 BufferedInputStream

BufferedOutputStream and BufferedInputStream
FileOutputStream and FileInputStream have a performance problem. Each file output stream
write() method call and file input stream read() method call results in a call to one of the underlying

platform’s native methods, and these native calls slow down I/O.

FileOutputStream和 FileInputStream有一个性能问题。每个文件输出流write()方法调用和文件输入流read()方法调用

导致调用底层平台的一个本地方法,这些本地调用减缓I / O。

Note A native method is an underlying platform API function that Java connects to an application via the
Java Native Interface (JNI). Java supplies reserved word native to identify a native method. For example,
the RandomAccessFile class declares a private native void open(String name, int mode)
method. When a RandomAccessFile constructor calls this method, Java asks the underlying platform (via
the JNI) to open the specified file in the specified mode on Java’s behalf. I discuss the JNI in Chapter 16.
The concrete BufferedOutputStream and BufferedInputStream filter stream classes improve
performance by minimizing underlying output stream write() and underlying input stream read()
method calls. Instead, calls to BufferedOutputStream’s write() and BufferedInputStream’s read()
methods take Java buffers into account.
When a write buffer is full,    write() calls the underlying output stream write()
method to empty the buffer. Subsequent calls to BufferedOutputStream’s
write() methods store bytes in this buffer until it’s once again full.
When the read buffer is empty,    read() calls the underlying input stream read()
method to fill the buffer. Subsequent calls to BufferedInputStream’s read()
methods return bytes from this buffer until it’s once again empty.
BufferedOutputStream declares the following constructors:
 BufferedOutputStream(OutputStream out) creates a buffered output stream that
streams its output to out. An internal buffer is created to store bytes written to out.
 BufferedOutputStream(OutputStream out, int size) creates a buffered output
stream that streams its output to out. An internal buffer of length size is created

to store bytes written to out.

The following example chains a BufferedOutputStream instance to a FileOutputStream instance.
Subsequent write() method calls on the BufferedOutputStream instance buffer bytes and
occasionally result in internal write() method calls on the encapsulated FileOutputStream instance.
FileOutputStream fos = new FileOutputStream("employee.dat");
BufferedOutputStream bos = new BufferedOutputStream(fos); // Chain bos to fos.
bos.write(0); // Write to employee.dat through the buffer.
// Additional write() method calls.
bos.close(); // This method call internally calls fos's close() method.
BufferedInputStream declares the following constructors:
 BufferedInputStream(InputStream in) creates a buffered input stream that
streams its input from in. An internal buffer is created to store bytes read from in.
 BufferedInputStream(InputStream in, int size) creates a buffered input
stream that streams its input from in. An internal buffer of length size is created
to store bytes read from in.
The following example chains a BufferedInputStream instance to a FileInputStream instance.
Subsequent read() method calls on the BufferedInputStream instance unbuffer bytes and
occasionally result in internal read() method calls on the encapsulated FileInputStream instance.
FileInputStream fis = new FileInputStream("employee.dat");
BufferedInputStream bis = new BufferedInputStream(fis); // Chain bis to fis.
int ch = bis.read(); // Read employee.dat through the buffer.
// Additional read() method calls.
bis.close(); // This method call internally calls fis's close() method.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值