FileChannel和MappedByteBuffer

來源:互聯網
上載者:User

標籤:nio filechannel filechannel


使用記憶體對應檔可以高效訪問檔案。下面代碼做了一個樣本,對比記憶體對應檔的操作和FileOutputStream寫檔案的效率差異。


import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.RandomAccessFile;

import java.nio.MappedByteBuffer;

import java.nio.channels.FileChannel;

import java.nio.channels.FileChannel.MapMode;


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


public class FileClient {

private FileChannel fileChannel;

private MappedByteBuffer mappedByteBuffer;

private File file;


private static int mapedFileSizeCommitLog = 1024 * 1024 * 1024;


private static final String fileName = "D:\\temp\\testfilecache\\a.dat";

private static final String fileName2 = "D:\\temp\\testfilecache\\b.dat";


private static final Logger logger = LoggerFactory.getLogger(FileClient.class.getName());


public static void main(String[] args) throws IOException {

FileClient fclient = new FileClient();

fclient.start();

fclient.start2();

}


@SuppressWarnings("resource")

public void start() throws IOException {

file = new File(fileName);

String testString = "hello";

boolean ok = false;

try {

this.fileChannel = new RandomAccessFile(this.file, "rw").getChannel();

this.mappedByteBuffer = this.fileChannel.map(MapMode.READ_WRITE, 0, mapedFileSizeCommitLog);

int curPos = 0;

long startTime = System.currentTimeMillis();

for (int i = 0; i < 1000000; i++) {

this.mappedByteBuffer.position(curPos);

this.mappedByteBuffer.put(testString.getBytes());

curPos += 6;

}

this.mappedByteBuffer.force();

this.fileChannel.close();

long stopTime = System.currentTimeMillis();

logger.debug("time :  " + (stopTime - startTime));

ok = true;

} catch (FileNotFoundException e) {

logger.error("create file channel " + fileName + " Failed. ", e);

throw e;

} catch (IOException e) {

logger.error("map file " + fileName + " Failed. ", e);

throw e;

} finally {

if (!ok && this.fileChannel != null) {

this.fileChannel.close();

}

}

}


public void start2() throws IOException {

FileOutputStream fos = new FileOutputStream(fileName2);

String testString = "hello";


long startTime = System.currentTimeMillis();

for (int i = 0; i < 1000000; i++) {

fos.write(testString.getBytes());

}

fos.close();

long stopTime = System.currentTimeMillis();

logger.debug("time2 :  " + (stopTime - startTime));


}


}


伺服器sata盤,寫入效能:

[[email protected] ~]# dd if=/dev/zero of=kwxgd bs=64k count=4k oflag=dsync

4096+0 records in

4096+0 records out

268435456 bytes (268 MB) copied, 8.93065 seconds, 30.1 MB/s

運行後,可以看到對比結果:

[[email protected] ~]# java -jar filechannel-test.jar 

17:56:46.987 [main] DEBUG o.l.filechannel_test.FileClient - time :  207

17:56:48.670 [main] DEBUG o.l.filechannel_test.FileClient - time2 :  1675

效能相差8倍


代碼可以從https://github.com/mfcliu/netty-learning下載。




本文出自 “勤似春芽” 部落格,請務必保留此出處http://leo01.blog.51cto.com/694903/1738057

FileChannel和MappedByteBuffer

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.