字节流、缓冲流性能测试比较:
import java.io.*;/** * 字节流、缓冲流性能测试 */public class BufferedDemo {public static void main(String[] args) {File src = https://www.isolves.com/it/cxkf/yy/JAVA/2020-05-03/new File("stream.txt");File target = new File("stream_copy.txt");try {// 字节流性能测试testSingleByteStream(src, target);testSingleBufferedStream(src, target);// 缓冲流性能测试testMultiByteStream(src, target);testMultiBufferedStream(src, target);} catch (Exception e) {e.printStackTrace();}}/*** 字节流性能测试* 每次读写都是一个字节**/private static void testSingleByteStream(File src, File target) throws Exception {long begin = System.currentTimeMillis();InputStream in = new FileInputStream(src);OutputStream out = new FileOutputStream(target);int len = -1;while ((len = in.read()) != -1) {out.write(len);}in.close();out.close();long end = System.currentTimeMillis();System.out.println("SingleByteStream 耗费:" + (end - begin) + "ms");}/*** 缓冲流性能测试* 每次读写都是一个字节**/private static void testSingleBufferedStream(File src, File target) throws Exception {long begin = System.currentTimeMillis();// 创建文件流InputStream srcStream = new FileInputStream(src);OutputStream targetStream = new FileOutputStream(target);// 穿甲缓冲流InputStream in = new BufferedInputStream(srcStream);OutputStream out = new BufferedOutputStream(targetStream);int len = -1;while ((len = in.read()) != -1) {out.write(len);}// 关闭流in.close();out.close();long end = System.currentTimeMillis();System.out.println("SingleBufferedStream 耗费:" + (end - begin) + "ms");}/*** 字节流性能测试* 使用多字节读写**/private static void testMultiByteStream(File src, File target) throws Exception {long begin = System.currentTimeMillis();InputStream in = new FileInputStream(src);OutputStream out = new FileOutputStream(target);int len = -1;byte[] buffer = new byte[1024];while ((len = in.read(buffer)) != -1) {out.write(buffer, 0, len);}in.close();out.close();long end = System.currentTimeMillis();System.out.println("MultiByteStream 耗费:" + (end - begin) + "ms");}/*** 缓冲流性能测试* 使用多字节读写**/private static void testMultiBufferedStream(File src, File target) throws Exception {long begin = System.currentTimeMillis();// 创建文件流InputStream srcStream = new FileInputStream(src);OutputStream targetStream = new FileOutputStream(target);// 穿甲缓冲流InputStream in = new BufferedInputStream(srcStream);OutputStream out = new BufferedOutputStream(targetStream);int len = -1;byte[] buffer = new byte[1024];while ((len = in.read(buffer)) != -1) {out.write(buffer, 0, len);}// 关闭流in.close();out.close();long end = System.currentTimeMillis();System.out.println("MultiBufferedStream 耗费:" + (end - begin) + "ms");}}
上述测试运行结果如下:
SingleByteStream 耗费:3610msSingleBufferedStream 耗费:31msMultiByteStream 耗费:16msMultiBufferedStream 耗费:0ms
性能孰高孰低 , 测试结果已经很能说明问题了 。
转换流由于字节流和字符流同时存在 , 有时候会把二者结合起来使用 , 甚至在二者之间实现转换;为了实现这样的功能 , Java IO中提供了字节流和字符流的适配器:
- InputStreamReader:可以把InputStream转换为Reader;
- OutputStreamWriter:可以把OutputStream转换为Writer;
import java.io.*;/** * 转换流案例 */public class StreamConvertDemo {public static void main(String[] args) throws Exception {// 创建文件对象File src = https://www.isolves.com/it/cxkf/yy/JAVA/2020-05-03/new File("stream.txt");File target = new File("stream_copy.txt");// 创建文件流InputStream in = new FileInputStream(src);OutputStream out = new FileOutputStream(target);// 创建字节/字符转换流Reader reader = new InputStreamReader(in, "UTF-8");Writer writer = new OutputStreamWriter(out, "UTF-8");// 操作字符流完成文件读写char[] buffer = new char[1024];int len = -1;while ((len = reader.read(buffer)) != -1) {writer.write(buffer, 0, len);}// 关闭流in.close();out.close();reader.close();writer.close();}}
为什么只有字节转字符流 , 却没有字符转字节流呢?原因也很简单:- 字节流可以操作一切文件 , 包括纯文本文件、二进制文件;
- 字符流是对字节流的增强 , 是用来操作纯文本使用的;
数组流/内存流面向字节的数组流/内存流:ByteArrayInputStream和ByteArrayOutputStream;该“流”会把数据暂存到内存中 , 以便实时读写 , 读写时使用的字节类型(byte[]);
推荐阅读
- 爱国的6字名言 爱国名言短句5~6字
- 鲁滨孙漂流记读后感 鲁滨孙漂流记读后感400字
- 北京的老字号,个个都藏着一道名菜
- 奥迪尾部的“数字+TFSI”代表什么意思?
- 试用期工作总结800字范文 实习期个人工作总结
- Java 如何利用钩子函数实现优雅停服?刨根问底
- 五分钟看穿Java并发相关概念,并发原来如此简单
- JavaScript、Ajax、jQuery全部知识点,1分钟速懂!
- 身份证号码18位数字都代表什么含义?
- 细黑发亮的红茶叫什么名字?[红茶]