Java常用读写类的速度比较

来源:互联网 发布:sql文件太大打不开 编辑:程序博客网 时间:2024/06/05 18:58


本文读写的内容为简单的字符串,主要比较了BufferedWriter(BufferedReader)、BufferedOutputStream(BufferedInputStream)、ByteBuffer(write&read)和MappedByteBuffer(write&read)的读写速度差异。测试时都是先写入文件5000000个字符串“Java is one of the best computer languages!\n”,然后再将所有数据读取到内存中。程序分别记录了上述四种方法的读写速度。

测试程序为:

import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.RandomAccessFile;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;public class IOStudy {public static void main(String[] args) {test1();//BufferedWriter(BufferedReader)test2();//BufferedOutputStream(BufferedInputStream)test3();//ByteBuffer(write&read)test4();//MappedByteBuffer(write&read)}public static void test1() {BufferedWriter bw = null;BufferedReader br = null;long time;try {time = System.currentTimeMillis();bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(".//data//test1.txt"), "utf-8"));for(int i = 0; i < 5000000; i++) {bw.write("Java is one of the best computer languages!\n");}bw.flush();System.out.println("BufferedWriter: " + (System.currentTimeMillis() - time) + "ms");time = System.currentTimeMillis();br = new BufferedReader(new InputStreamReader(new FileInputStream(".//data//test1.txt"), "utf-8"));//要慢一点605ms,但是使用比较方便//String inputline = br.readLine();//while(inputline != null) {//inputline = br.readLine();//}//要快一点char[] data = new char[5000000 * 44];br.read(data);System.out.println("BufferedReader: " + (System.currentTimeMillis() - time) + "ms");} catch (IOException e) {e.printStackTrace();} finally {try {if(bw != null)bw.close();if(br != null)br.close();} catch (IOException e) {e.printStackTrace();}}}public static void test2() {BufferedOutputStream bos = null;BufferedInputStream bis = null;long time;try {time = System.currentTimeMillis();bos = new BufferedOutputStream(new FileOutputStream(".//data//test2.txt"));for(int i = 0; i < 5000000; i++) {bos.write("Java is one of the best computer languages!\n".getBytes());}bos.flush();System.out.println("BufferedOutputStream: " + (System.currentTimeMillis() - time) + "ms");time = System.currentTimeMillis();bis = new BufferedInputStream(new FileInputStream(".//data//test2.txt"));byte[] data = new byte[5000000 * 44];bis.read(data);System.out.println("BufferedInputStream: " + (System.currentTimeMillis() - time) + "ms");} catch (IOException e) {e.printStackTrace();} finally {try {if(bos != null)bos.close();if(bis != null)bis.close();} catch (IOException e) {e.printStackTrace();}}}public static void test3() {FileOutputStream fos = null;FileChannel fc1 = null;FileInputStream fis = null;FileChannel fc2 = null;long time;try {time = System.currentTimeMillis();fos = new FileOutputStream(".//data//test3.txt");fc1 = fos.getChannel();ByteBuffer b = ByteBuffer.wrap("Java is one of the best computer languages!\n".getBytes());for(int i = 0; i < 5000000; i++) {fc1.write(b);b.rewind();}System.out.println("ByteBuffer(write): " + (System.currentTimeMillis() - time) + "ms");time = System.currentTimeMillis();fis = new FileInputStream(".//data//test3.txt");fc2 = fis.getChannel();ByteBuffer data = ByteBuffer.allocate(5000000 * 44);fc2.read(data);System.out.println("ByteBuffer(read): " + (System.currentTimeMillis() - time) + "ms");} catch (IOException e) {e.printStackTrace();} finally {try {if(fc1 != null)fc1.close();if(fc2 != null)fc1.close();if(fos != null) fos.close();if(fis != null) fis.close();} catch (IOException e) {e.printStackTrace();}}}public static void test4() {FileChannel fc1 = null;FileChannel fc2 = null;long time;try {time = System.currentTimeMillis();fc1 = new RandomAccessFile(".//data//test4.txt", "rw").getChannel();ByteBuffer b = fc1.map(FileChannel.MapMode.READ_WRITE, 0, 5000000 * 44);for(int i = 0; i < 5000000; i++) {b.put("Java is one of the best computer languages!\n".getBytes());}System.out.println("MappedByteBuffer(write): " + (System.currentTimeMillis() - time) + "ms");time = System.currentTimeMillis();fc2 = new FileInputStream(".//data//test4.txt").getChannel();b = fc2.map(FileChannel.MapMode.READ_ONLY, 0, fc2.size());byte[] data = new byte[5000000 * 44];b.get(data);System.out.println("MappedByteBuffer(read): " + (System.currentTimeMillis() - time) + "ms");} catch (IOException e) {e.printStackTrace();} finally {try {if(fc1 != null) fc1.close();if(fc2 != null) fc2.close();} catch (IOException e) {e.printStackTrace();}}}}



测试结果为:



可以看出来,BufferedWriter的写速度是最快的,MappedByteBuffer的读速度是最快的,但是一般BufferedReader操作方便,并且其速度也不是特别慢。所以一般情况下建议字符串读写用BufferedWriter&BufferedReader;对于全文读速度要求高的话可以用MappedByteBuffer。

1 0
原创粉丝点击