JAVA字节流、字符流、缓冲流、转换流、内存流、字符编码( 五 )

 
字节流、缓冲流性能测试比较:
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();}}为什么只有字节转字符流 , 却没有字符转字节流呢?原因也很简单:
  • 字节流可以操作一切文件 , 包括纯文本文件、二进制文件;
  • 字符流是对字节流的增强 , 是用来操作纯文本使用的;
字节流和字符流之间并不对等 , 无法实现转换 , 就比如C++程序可以运行C程序 , 但C程序却不能运行C++程序 。
数组流/内存流面向字节的数组流/内存流:ByteArrayInputStream和ByteArrayOutputStream;该“流”会把数据暂存到内存中 , 以便实时读写 , 读写时使用的字节类型(byte[]);


推荐阅读