我目前正在使用一个缓冲流来读写一些文件。在这中间,我做了一些数学处理,其中一个符号是一个字节。
阅读:
InputStream input = new FileInputStream(outputname)
input.read(byte[] b,int off,int len)要编写:
OutputStream output = new BufferedOutputStream(
new FileOutputStream(outputname),
OUTPUTBUFFERSIZE
)
output.write((byte)byteinsideaint);现在我需要添加一些标题数据,并支持短符号。我想使用DataInputStream和DataOutputStream来避免将其他类型转换为字节,我想知道它们的性能如何。
我需要使用
OutputStream output = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(outputname),
OUTPUTBUFFERSIZE
)
);为了保留数据缓冲的优点,或者它足够好地使用
OutputStream output = new DataOutputStream(
new FileOutputStream(outputname)
)发布于 2012-03-05 22:19:58
您应该在两者之间添加BufferedOutputStream。DataOutputStream没有实现任何缓存(这很好:关注点分离),如果不缓存底层的OutputStream,它的性能将非常差。即使是最简单的方法,如writeInt(),也可能导致四次独立的磁盘写入。
据我所知,只有[write(byte[], int, int)](http://docs.oracle.com/javase/7/docs/api/java/io/DataOutputStream.html#write(byte[],%20int,%20int%29)和writeUTF(String)在一个byte[]块中写入数据。其他的则逐字节写入原始值(如int或double)。
发布于 2012-03-05 22:26:42
您绝对需要将BufferedOutputStream放在中间。
感谢您对性能的关注,我有两个建议:
https://stackoverflow.com/questions/9568072
复制相似问题