现在的位置: 首页 > 编程语言 > 正文

java高效文件流读写操作详解

2020年02月14日 编程语言 ⁄ 共 2063字 ⁄ 字号 评论关闭

导语

防止自己以后忘记,记录一些文件流的性能对比。

平常经常会操作到文件读写,java当中提供了许多操作文件的类,一般来说,文件操作也叫流操作,可以按照以下方式分类:

按照功能分类,字节流和字符流。 按照节点流和过滤流,节点流直接操作文件,过滤流包装了节点流和过滤流。如FileInputStream和BufferedFileInputStream就是分别是节点流和过滤流。

文件流比较

下面重点比较我们经常用的几个流

(1) DataInputStream+FileInputStream

(2) DataInputStream+BufferedInputStream

(3) BufferedReader+FileReader(缓冲流大小不同性能也不同,如1K和8K)

(4) BufferedFileReader

先贴结论:

耗时对比为:

(4)优于(3)—8096优于(3)-1024优于(2)<(1)

另外,BufferedFileReader如果涉及到读取文件的每一行内容还可以在优化,传统的代码如下所示:

BufferedFileReader in = new BufferedFileReader(); for (int i=0; i < nargs; i++) { try { in.open(args[i]); while ((line = in.readLine()) != null) { nlines++; } in.close(); } catch (Exception e) { System.out.println(" BFRTest: exception:" + e ); }

代码中第5行,BufferedReader首先生成一个StringBuffer,然后转成一个String返回,这里涉及到两次字符拷贝。假设读取10000行,会有10000个临时的String对象生成,内存比较耗费,由于BufferedReader的缓冲流是私有的,因此这里的优化点是可以自己管理缓冲流。最佳的读文件的方法代码如下:

FileReader fr; int nlines = 0; char buffer[] = new char[8192 + 1]; int maxLineLength = 128; //assumes no line is longer than this char lineBuf[] = new char[maxLineLength]; for (int i = 0; i < nargs; i++) { try { fr = new FileReader(args[i]); int nChars = 0; int nextChar = 0; int startChar = 0; boolean eol = false; int lineLength = 0; char c = 0; int n; int j; while (true) { if (nextChar >= nChars) { n = fr.read(buffer, 0, 8192); if (n == -1) { // EOF break; } nChars = n; startChar = 0; nextChar = 0; } for (j = nextChar; j < nChars; j++) { c = buffer[j]; if ((c == '\n') || (c == '\r')) { eol = true; break; } } nextChar = j; int len = nextChar - startChar; if (eol) { nextChar++; if ((lineLength + len) > maxLineLength) { // error } else { System.arraycopy(buffer, startChar, lineBuf, lineLength, len); } lineLength += len; // // Process line here // nlines++; if (c == '\r') { if (nextChar >= nChars) { n = fr.read(buffer, 0, 8192); if (n != -1) { nextChar = 0; nChars = n; } } if ((nextChar < nChars) && (buffer[nextChar] == '\n')) nextChar++; } startChar = nextChar; lineLength = 0; continue; } if ((lineLength + len) > maxLineLength) { // error } else { System.arraycopy(buffer, startChar, lineBuf, lineLength, len); } lineLength += len; } fr.close(); } catch (Exception e) { System.out.println("exception: " + e); }

NIO文件流

FileChanel介绍

通道,之前流是单向的,而通道是双向的,既可以读,也可以写。读取文件效率参考这里。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: java高效文件流读写操作详解

以上就上有关java高效文件流读写操作详解的全部内容,学步园全面介绍编程技术、操作系统、数据库、web前端技术等内容。

抱歉!评论已关闭.