没有合适的资源?快使用搜索试试~ 我知道了~
首页Java IO 性能优化
资源详情
资源推荐
41
CHAPTER 4
I/O Performance
B
OTH
client and server programs often use the java.io package. While this
package is designed with ease of use in mind, developers new to the platform
often make mistakes that can lead to poor I/O performance. Fortunately, a better
understanding of this package can lead to major improvements in I/O
performance.
Section 4.1 provides a brief overview of the
java.io package, identifies the
most common cause of poor I/O performance, and describes several different
approaches to solving this problem. Section 4.2 focuses on object serialization, a
key part of many of the newest features in the Java platform.
4.1 Basic I/O
Two abstract classes shape the architecture of the java.io package: InputStream
and OutputStream. These interfaces define the key abstractions for I/O opera-
tions. Concrete implementations of
InputStream and OutputStream provide
access to different types of data sources such as disks and network connections.
The
java.io package also provides several filter streams that don’t point to a
specific data source and are meant to be stacked on top of other streams. These
filter streams are at the heart of the
java.io architecture. From a performance
perspective, the most interesting filter streams are buffered streams. Figure 4-1
shows a simplified class hierarchy that includes the buffered streams.
4.1.1 Buffered Streams
The leading cause of poor I/O performance is failing to buffer I/O operations.
Hard disks are very good at reading and writing sizable chunks of data, but they
are much less efficient when working with small blocks of data. To maximize I/O
performance, you should batch read and write operations. This is exactly what
buffered streams are designed for.
By default, most of the streams provided in the Java libraries write one byte of
data at a time. Instead of putting buffering behavior into each individual stream
JPIOPerformance.fm Page 41 Monday, May 1, 2000 7:39 PM
42
Chapter 4 I/O Performance
type, the buffering behavior is implemented in dedicated BufferedInputStream
and BufferedOutputStream classes. This is very different from C, where the
basic
stdio operations are buffered by default. To better understand the effects of
buffering streams, look at Listing 4-1. This example copies a file from one loca-
tion to another.
public static void copy(String from, String to) throws IOException{
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(from);
out = new FileOutputStream(to);
while (true) {
int data = in.read();
if (data == -1) {
break;
}
out.write(data);
}
in.close();
out.close();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
Listing 4-1 Simple file copy
Figure 4-1 Simplified
java.io
class hierarchy
FilterInputStream
InputStream
FileInputStream
OutputStream
FilterOutputStream
FileOutputStream
BufferedInputStream
BufferedOutputStream
JPIOPerformance.fm Page 42 Monday, May 1, 2000 7:39 PM
4.1 Basic I/O
43
The copy method opens a FileInputStream and a FileOutputStream and
copies the contents of one directly into the other. Since the
read and write meth-
ods work on individual bytes, this means an actual disk read and write occurs for
each byte copied. Figure 4-2 illustrates how the data moves from one file to the
other.
When the code in Listing 4-1 is run on our test configuration to copy a 370K
JPEG test image, it takes almost 11 seconds to execute. Listing 4-2 shows a
slightly modified version of the same code that uses buffered streams to improve
performance. This code stacks buffered streams on top of the bare file streams.
The buffered streams save up read and write requests and then execute them all at
once—usually batching several thousand tiny requests into one larger request.
When the code in Listing 4-2 is used to copy the same JPEG test file, it executes in
a mere 130 milliseconds—almost 100 times faster!
public static void copy(String from, String to) throws IOException{
InputStream in = null;
OutputStream out = null;
try {
InputStream inFile = new FileInputStream(from);
in = new BufferedInputStream(inFile);
OutputStream outFile = new FileOutputStream(to);
out = new BufferedOutputStream(outFile);
while (true) {
int data = in.read();
if (data == -1) {
break;
}
out.write(data);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
Listing 4-2 Faster file copy
Figure 4-2 Copying with basic streams
Source File New File
FileInputStream FileOutputStream
JPIOPerformance.fm Page 43 Monday, May 1, 2000 7:39 PM
剩余11页未读,继续阅读
hancel
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功