测试发现NIO性能没有FileWriter好!
我最近打算把应用里面写文件的性能提升一下。
一直以来都听说NIO能提升读写文件的性能,所以做了个测试。
但测试结果却发现NIO性能还不如传统的FileWriter。
方式一(传统方式):
FileWriter fw = new FileWriter("D:\\fileTest\\test.txt");
BufferedWriter bw = new BufferedWriter(fw);
long l = System.currentTimeMillis();
StringBuffer sub = null;
for (int i = 0; i < 200000; i++) {
sub = new StringBuffer();
for (int j = 0; j < 60; j++) {
sub.append("“很好,有些见地。”");
if (j < 39) {
sub.append(",");
}
}
bw.write(sub.toString());
bw.newLine();
}
bw.close();
System.out.println("执行时间:" + (System.currentTimeMillis() - l));
-----------------------------------------------------------
执行三次:
1.执行时间:1955
2.执行时间:1938
3.执行时间:1966
可以看出基本在2秒以内
方式二(NIO 缓冲区、通道方式):
FileOutputStream fos = new FileOutputStream(new File("D:\\fileTest\\test.txt"));
FileChannel fc = fos.getChannel();
ByteBuffer bb = ByteBuffer.allocate(2048);
long l = System.currentTimeMillis();
try {
StringBuffer sub = null;
for (int i = 0; i < 200000; i++) {
sub = new StringBuffer();
for (int j = 0; j < 60; j++) {
sub.append("“很好,有些见地。”");
if (j < 39) {
sub.append(",");
}
}
bb.put(sub.toString().getBytes());
bb.put("\n".toString().getBytes());
bb.flip();
fc.write(bb);
bb.clear();
}
fc.close();
fos.close();
System.out.println("执行时间:" + (System.currentTimeMillis() - l));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != fc) {
fc.close();
}
if (null != fos) {
fos.close();
}
}
-----------------------------------------------------------
执行三次:
执行时间:3255
执行时间:3022
执行时间:2996
从上面的测试结果看出,NIO的方式每次都需要3秒左右,性能更差。
是不是我的NIO代码写的有问题,请高手帮忙分析一下。
一直以来都听说NIO能提升读写文件的性能,所以做了个测试。
但测试结果却发现NIO性能还不如传统的FileWriter。
方式一(传统方式):
FileWriter fw = new FileWriter("D:\\fileTest\\test.txt");
BufferedWriter bw = new BufferedWriter(fw);
long l = System.currentTimeMillis();
StringBuffer sub = null;
for (int i = 0; i < 200000; i++) {
sub = new StringBuffer();
for (int j = 0; j < 60; j++) {
sub.append("“很好,有些见地。”");
if (j < 39) {
sub.append(",");
}
}
bw.write(sub.toString());
bw.newLine();
}
bw.close();
System.out.println("执行时间:" + (System.currentTimeMillis() - l));
-----------------------------------------------------------
执行三次:
1.执行时间:1955
2.执行时间:1938
3.执行时间:1966
可以看出基本在2秒以内
方式二(NIO 缓冲区、通道方式):
FileOutputStream fos = new FileOutputStream(new File("D:\\fileTest\\test.txt"));
FileChannel fc = fos.getChannel();
ByteBuffer bb = ByteBuffer.allocate(2048);
long l = System.currentTimeMillis();
try {
StringBuffer sub = null;
for (int i = 0; i < 200000; i++) {
sub = new StringBuffer();
for (int j = 0; j < 60; j++) {
sub.append("“很好,有些见地。”");
if (j < 39) {
sub.append(",");
}
}
bb.put(sub.toString().getBytes());
bb.put("\n".toString().getBytes());
bb.flip();
fc.write(bb);
bb.clear();
}
fc.close();
fos.close();
System.out.println("执行时间:" + (System.currentTimeMillis() - l));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != fc) {
fc.close();
}
if (null != fos) {
fos.close();
}
}
-----------------------------------------------------------
执行三次:
执行时间:3255
执行时间:3022
执行时间:2996
从上面的测试结果看出,NIO的方式每次都需要3秒左右,性能更差。
是不是我的NIO代码写的有问题,请高手帮忙分析一下。
...全文
相关推荐
PB中几种写文件方式性能测试,有兴趣的可以看看(PB125)
几种写文件方式性能测试,有兴趣的可以看看(PB125).rar
测试用例之java文件基本操作.zip
测试用例 测试用例之java文件基本操作
JAVA的I/O流对比示例测试
包括:字节流、字符流、BUFFER字节流、BUFFER字符流、以及NIO的测试示例代码
ocjp7:OCJP7 示例
JavaOCJP7 OCJP7 示例 使用 *Test.java 类测试功能并观察结果 IO - 文件 IO 基础 文件写入器和读取器的基本使用 NIO - 蔚来基础知识
java的各种io操作
java提供各种IO操作。深入浅出的讲解IO.适合初学者学习


Java SE
