修改了下代码:
<pre name="code" class="java">package ringBuffer; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileChannel.MapMode; public class PerformanceWriteTest { /** * @param args */ public static void main(String[] args) { String outputFile = "F:\\test\\ioTest.txt"; Long length = 0L ; Long totalTime = 0L; try { raf = new RandomAccessFile("F:\\test\\ioTest.txt", "rw"); FileChannel fc = raf.getChannel(); mbb = fc.map(MapMode.READ_WRITE, 0, 85*1024*1024); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } for (int j = 0; j < 5; j++) { // for (int j = 0; j < 5; j++) { StringBuffer sb = new StringBuffer(); for (Integer i = 0; i < 1000000; i++) { // for (Integer i = 0; i < 100000; i++) { sb.append(j+i.toString() + "V"); } sb.append("S"); // byte[] msgs = sb.toString().getBytes(); length = (long) sb.toString().length() ; long start = System.currentTimeMillis() ; appendFileTest(outputFile,sb.toString()); totalTime = totalTime + (System.currentTimeMillis() - start) ; } System.out.println(" Total Data is : " + length*5/1000 + " Kbytes! ") ; System.out.println(" Total Time is : " + totalTime) ; System.out.println(" Averge Speed is :" + length*5/(totalTime*1000) + " Kbytes"); } private static void appendFileTest(String outputFile, String msgs) { // append1(outputFile, msgs) ; //FileOutputStream // append2(outputFile, msgs) ; //FileWriter // append3(outputFile, msgs) ; //RandomAccessFile append4(outputFile, msgs) ; //RandomAccessFile } private static void append1(String outputFile, String msgs) { BufferedWriter out = null; try { out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(outputFile, true))); out.append(msgs) ; } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } private static void append2(String outputFile, String msgs) { try { FileWriter writer = new FileWriter(outputFile, true); // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 writer.write(msgs); writer.close(); } catch (IOException e) { e.printStackTrace(); } } private static void append3(String outputFile, String msgs) { try { RandomAccessFile randomFile = new RandomAccessFile(outputFile, "rw"); // 打开一个随机访问文件流,按读写方式 long fileLength = randomFile.length(); // 文件长度,字节数 randomFile.seek(fileLength); // 将写文件指针移到文件尾 randomFile.writeBytes(msgs); randomFile.close(); } catch (IOException e) { e.printStackTrace(); } } private static void append4(String outputFile, String msgs) { try { mbb.position(pos) ; mbb.put(msgs.getBytes()); pos = pos + msgs.getBytes().length ; raf.close(); } catch (IOException e) { e.printStackTrace(); } } static RandomAccessFile raf ; static MappedByteBuffer mbb ; static Integer pos = 0 ; }
测试时为了增加内存开销,增加了 -XX:+PrintGC 选项,如果大家知道更好的测试内存消耗的方法,请告诉我哈。
</pre><pre code_snippet_id="600488" snippet_file_name="blog_20150208_4_5933734" name="code" class="java">[GC 32704K->2872K(124992 K), 0.0015308 secs] [GC 35576K->5152K(157696K), 0.0018430 secs] [GC 70560K->9768K(157696K), 0.0032342 secs] [GC 75176K->28240K(223104K), 0.0056030 secs] [GC 159056K->28888K(223104K), 0.0009104 secs] [GC 159704K->37464K(353856K), 0.0033345 secs] [GC 299096K->42152K(354048K), 0.0022436 secs] [GC 303784K->39848K(615 744K), 0.0009303 secs] [GC 563112K->55968K(616 192K), 0.0087540 secs] Total Data is : 39444 Kbytes! Total Time is : 159 Averge Speed is :248 Kbytes 2.826G [GC 32704K->2872K(124992K), 0.0136790 secs] [GC 35576K->5184K(157696K), 0.0019457 secs] [GC 70592K->9792K(157696K), 0.0031822 secs] [GC 75200K->28240K(223104K), 0.0056500 secs] [GC 152655K->43624K(223104K), 0.0051765 secs] [GC 174440K->52848K(353856K), 0.0034980 secs] [GC 314480K->55276K(354176K), 0.0013858 secs] [GC 314139K->71372K(615488K), 0.0058115 secs] [Full GC 71372K->18926K(610816K), 0.0094205 secs] [GC 542190K->43678K(610880K), 0.0038294 secs] Total Data is : 39444 Kbytes! Total Time is : 153 Averge Speed is :257 Kbytesa 3.43G [GC 32704K->2872K(124992K), 0.0012018 secs] [GC 35576K->5184K(124992K), 0.0021390 secs] [GC 37888K->9800K(124992K), 0.0031359 secs] [GC 42504K->9824K(157696K), 0.0005724 secs] [GC 75232K->28208K(157696K), 0.0059463 secs] [GC 93616K->28224K(223040K), 0.0007707 secs] [GC 159040K->32852K(223360K), 0.0095536 secs] [GC 163668K->46740K(354880K), 0.0055367 secs] [GC 308372K->55892K(355008K), 0.0032216 secs] [GC 317524K->60484K(511168K), 0.0017500 secs] [GC 478468K->65092K(511808K), 0.0033206 secs] Total Data is : 39444 Kbytes! Total Time is : 68 Averge Speed is :580 Kbytes 2.87G
[GC 32704K->2880K(124992K), 0.0132009 secs] [GC 35584K->5192K(157696K), 0.0017013 secs] [GC 70600K->9800K(157696K), 0.0035649 secs] [GC 75208K->28232K(223104K), 0.0057867 secs] [GC 151138K->43680K(223104K), 0.0055066 secs] [GC 174496K->48256K(353344K), 0.0020662 secs] [GC 298339K->59048K(354496K), 0.0054248 secs] [GC 319528K->77512K(614592K), 0.0058665 secs] [Full GC 77512K->18933K(612224K), 0.0097774 secs] [GC 521588K->34405K(612352K), 0.0027320 secs] Total Data is : 39444 Kbytes! Total Time is : 137 Averge Speed is :287 Kbytes 3.4G
这里很奇怪,理论上Mbb的性能应该远大于这个,可能是我的测试数据量太小的缘故.
综上可见,RandomAccessFile 性价比最高。