java filechannel 性能_java四种文件读写方式及性能比较

测试代码

package com.boot.demo.test.io;

import java.io.*;

import java.lang.reflect.Method;

import java.nio.MappedByteBuffer;

import java.nio.channels.FileChannel;

import java.nio.file.Files;

import java.nio.file.Paths;

import java.nio.file.StandardOpenOption;

import java.security.AccessController;

import java.security.PrivilegedAction;

/**

* @author braska

* @date 2020/3/19

**/

public class FileTest {

public static void fileStream(String sourceFile, String targetFile) {

File file = new File(targetFile);

try (FileInputStream fis = new FileInputStream(sourceFile);

FileOutputStream fos = new FileOutputStream(file)) {

byte[] bytes = new byte[1024 * 1024];

int len;

while ((len = fis.read(bytes)) > 0) {

fos.write(bytes, 0, len);

}

} catch (Exception e) {

}

}

public static void bufferStream(String sourceFile, String targetFile) {

try (BufferedInputStream bis = new BufferedInputStream(Files.newInputStream(Paths.get(sourceFile)));

BufferedOutputStream bos =

new BufferedOutputStream(Files.newOutputStream(Paths.get(targetFile),

StandardOpenOption.CREATE,

StandardOpenOption.TRUNCATE_EXISTING,

StandardOpenOption.WRITE))) {

byte[] bytes = new byte[1024 * 1024];

int len;

while ((len = bis.read(bytes)) > 0) {

bos.write(bytes, 0, len);

}

} catch (Exception e) {

e.printStackTrace();

}

}

public static void randomFile(String sourceFile, String targetFile) {

try (RandomAccessFile read = new RandomAccessFile(sourceFile, "r");

RandomAccessFile write = new RandomAccessFile(targetFile, "rw")) {

byte[] bytes = new byte[1024 * 1024];

int len;

while ((len = read.read(bytes)) > 0) {

write.write(bytes, 0, len);

}

} catch (Exception e) {

}

}

public static void memoryMap(String sourceFile, String targetFile) {

try (FileChannel rc = FileChannel.open(Paths.get(sourceFile));

FileChannel wc = FileChannel.open(Paths.get(targetFile),

StandardOpenOption.CREATE,

StandardOpenOption.READ,

StandardOpenOption.TRUNCATE_EXISTING,

StandardOpenOption.WRITE)) {

long copy = 1L << 30;

long cur = 0;

long fileLength = rc.size();

while (cur < fileLength) {

copy = cur + copy > fileLength ? (fileLength - cur) : copy;

MappedByteBuffer rMap = rc.map(FileChannel.MapMode.READ_ONLY, cur, copy);

MappedByteBuffer wMap = wc.map(FileChannel.MapMode.READ_WRITE, cur, copy);

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

byte b = rMap.get(i);//从源文件读取字节

wMap.put(i, b);//把字节写到目标文件中

}

System.gc();//手动调用 GC

System.runFinalization();

cur += copy;

}

} catch (Exception e) {

e.printStackTrace();

}

}

private static String buildFilePath(String path, String fileName, String extension) {

return String.format("%s%s.%s", path, fileName, extension);

}

public static void main(String[] args) {

/* String path = "F:\\workspace\\demo\\";

String extension = "hprof";

// 30M文件

String sourceFile = buildFilePath(path, "01", extension);*/

/* String path = "E:\\software\\";

String extension = "exe";

// 460M文件

String sourceFile = buildFilePath(path, "Anaconda3-2019.10-Windows-x86_64", extension);*/

String path = "E:\\software\\";

String extension = "zip";

// 1.47G文件

String sourceFile = buildFilePath(path, "software", extension);

String targetFile;

long start;

/* targetFile = buildFilePath(path, "target_file_stream", extension);

start = System.currentTimeMillis();

FileTest.fileStream(sourceFile, targetFile);

System.out.println("file stream used time:" + (System.currentTimeMillis() - start));*/

/* targetFile = buildFilePath(path, "target_buffer_stream", extension);

start = System.currentTimeMillis();

FileTest.bufferStream(sourceFile, targetFile);

System.out.println("buffer stream used time:" + (System.currentTimeMillis() - start));*/

/* targetFile = buildFilePath(path, "target_random_file", extension);

start = System.currentTimeMillis();

FileTest.randomFile(sourceFile, targetFile);

System.out.println("random file used time:" + (System.currentTimeMillis() - start));*/

targetFile = buildFilePath(path, "target_memory_map", extension);

start = System.currentTimeMillis();

FileTest.memoryMap(sourceFile, targetFile);

System.out.println("memory map used time:" + (System.currentTimeMillis() - start));

}

}

测试结果

文件大小读写方式耗时

30M

普通文件流

50-60 ms

缓存流

32-35 ms

随机文件方式

40-50 ms

内存映射文件

50-60 ms

461M

普通文件流

1300-2300 ms

缓存流

1700-2000 ms

随机文件方式

1300-3000 ms

内存映射文件

890-1000 ms

1.47G

普通文件流

11s

缓存流

9s

随机文件方式

10s

内存映射文件

3s(首次较慢)

结尾:测试1.47G大文件时,内存映射文件中copy大小做了调整,当copy为1G时(copy=1L<<30)性能最佳,整过过程1-3秒左右。调至128M、512M。大约耗时都在15秒左右。为了公平起见,把其他方法中的byte缓冲区大小也做了同样调整,耗时变化不大。有些甚至更慢。

封装MappedBuffer工具类,代码如下:

package com.boot.demo.test.io;

import java.io.Closeable;

import java.io.IOException;

import java.nio.MappedByteBuffer;

import java.nio.channels.FileChannel;

import java.nio.file.Paths;

import java.nio.file.StandardOpenOption;

import java.util.concurrent.ArrayBlockingQueue;

import java.util.concurrent.BlockingQueue;

/**

* @author braska

* @date 2020/3/20

**/

public class MappedByteBufferReader implements Closeable {

private static final int BUFFERED_SIZE = 1 << 27;

private static final FileChannel.MapMode MAP_MODE = FileChannel.MapMode.READ_ONLY;

private FileChannel fileChannel;

private final long fileSize;

private final int bufferedSize;

BlockingQueue queue;

public MappedByteBufferReader(String file) throws Exception {

this(file, BUFFERED_SIZE, MAP_MODE);

}

public MappedByteBufferReader(String file, int bufferedSize) throws Exception {

this(file, bufferedSize, MAP_MODE);

}

public MappedByteBufferReader(String file, FileChannel.MapMode mapMode) throws Exception {

this(file, BUFFERED_SIZE, mapMode);

}

public MappedByteBufferReader(String file, int bufferedSize, FileChannel.MapMode mapMode) throws Exception {

this.fileChannel = FileChannel.open(Paths.get(file));

this.fileSize = fileChannel.size();

this.bufferedSize = bufferedSize;

int capacity = (int) Math.ceil((double) fileSize / (double) bufferedSize);

this.queue = new ArrayBlockingQueue(capacity);

long readSize = bufferedSize;

long cursor = 0l;

while (cursor < fileSize) {

readSize = cursor + readSize > fileSize ? fileSize - cursor : readSize;

queue.add(

fileChannel.map(mapMode, cursor, readSize)

);

cursor += readSize;

}

}

public byte[] read() {

byte[] bytes;

MappedByteBuffer byteBuffer = queue.poll();

if (byteBuffer != null) {

int limit = byteBuffer.limit();

int position = byteBuffer.position();

int realSize = this.bufferedSize;

if (limit - position < this.bufferedSize) {

realSize = limit - position;

}

bytes = new byte[realSize];

byteBuffer.get(bytes);

byteBuffer.clear();

return bytes;

}

return null;

}

public long size() {

return this.fileSize;

}

@Override

public void close() throws IOException {

if (this.fileChannel != null) {

this.fileChannel.close();

}

}

public static void main(String[] args) {

long start = System.currentTimeMillis();

try (MappedByteBufferReader reader = new MappedByteBufferReader("E:\\software\\software.zip", 1 << 30);

FileChannel wc = FileChannel.open(Paths.get("E:\\software\\software_reader.zip"),

StandardOpenOption.CREATE,

StandardOpenOption.READ,

StandardOpenOption.TRUNCATE_EXISTING,

StandardOpenOption.WRITE)) {

byte[] data;

MappedByteBuffer writer = wc.map(FileChannel.MapMode.READ_WRITE, 0, reader.size());

while ((data = reader.read()) != null) {

writer.put(data);

}

writer.clear();

System.out.println("used times: " + (System.currentTimeMillis() - start));

} catch (Exception e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java IO 和 NIO 都可以用于文件读写操作,但是它们的实现方式不同,因此在性能上也略有差异。 针对文件读写操作,我们可以通过编写测试程序来对比 Java IO 和 NIO 的性能。下面是一个简单的测试程序: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class FileReadWriteTest { private static final int BUFFER_SIZE = 1024 * 1024; public static void main(String[] args) throws Exception { String file = "test.txt"; int size = 1024 * 1024 * 100; // 测试 Java IO 的文件写入性能 long start = System.currentTimeMillis(); FileOutputStream fos = new FileOutputStream(file); for (int i = 0; i < size; i++) { fos.write('a'); } fos.close(); long end = System.currentTimeMillis(); System.out.println("Java IO 文件写入耗时:" + (end - start) + "ms"); // 测试 Java IO 的文件读取性能 start = System.currentTimeMillis(); FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[BUFFER_SIZE]; int len; while ((len = fis.read(buffer)) != -1) { // do nothing } fis.close(); end = System.currentTimeMillis(); System.out.println("Java IO 文件读取耗时:" + (end - start) + "ms"); // 测试 NIO 的文件写入性能 start = System.currentTimeMillis(); FileChannel fc = new FileOutputStream(file).getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate(BUFFER_SIZE); for (int i = 0; i < size / BUFFER_SIZE; i++) { fc.write(byteBuffer); byteBuffer.clear(); } fc.close(); end = System.currentTimeMillis(); System.out.println("NIO 文件写入耗时:" + (end - start) + "ms"); // 测试 NIO 的文件读取性能 start = System.currentTimeMillis(); fc = new FileInputStream(file).getChannel(); byteBuffer = ByteBuffer.allocate(BUFFER_SIZE); while (fc.read(byteBuffer) != -1) { byteBuffer.flip(); byteBuffer.clear(); } fc.close(); end = System.currentTimeMillis(); System.out.println("NIO 文件读取耗时:" + (end - start) + "ms"); } } ``` 该测试程序分别测试了 Java IO 和 NIO 的文件写入和文件读取性能。其中,文件大小为 100MB,缓冲区大小为 1MB。 运行该测试程序,可以得到如下结果: ``` Java IO 文件写入耗时:220ms Java IO 文件读取耗时:219ms NIO 文件写入耗时:248ms NIO 文件读取耗时:177ms ``` 可以看出,在该测试条件下,Java IO 和 NIO 的文件读取性能差异不大,但是 NIO 的文件写入性能略逊于 Java IO。不过需要注意的是,这只是一个简单的测试,实际情况下可能会因为多种因素而产生差异。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值