释放双眼,带上耳机,听听看~!
NIO将文件映射到内存进行读写的性能会比普通的IO读写快很多!
下面就写个测试的demo,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 1package utils.nio;
2
3import java.io.*;
4import java.nio.ByteBuffer;
5import java.nio.CharBuffer;
6import java.nio.channels.FileChannel;
7
8/**
9 * 文件名称: FileReadWriteTest.java
10 * 编写人: yh.zeng
11 * 编写时间: 17-6-8 下午9:18
12 * 文件描述: IO和NIO文件读写性能对比
13 */
14public class FileReadWriteTest
15{
16
17 public static void main(String args[]) throws Exception{
18 FileReadWriteTest test = new FileReadWriteTest();
19 long start = System.currentTimeMillis();
20 //for(int i = 0; i <= 10; i++){
21 test.ioCopyFile("F://file.txt","F://filecopy.txt");
22 //}
23 System.out.println("IO使用缓冲区耗时:" + (System.currentTimeMillis() - start) + "ms");
24 start = System.currentTimeMillis();
25 //for(int i = 0; i <= 10; i++){
26 test.nioCopyFile("F://file.txt", "F://filecopy.txt");
27 //}
28 System.out.println("NIO使用缓冲区读写耗时:" + (System.currentTimeMillis() - start) + "ms");
29
30 start = System.currentTimeMillis();
31 //for(int i = 0; i <= 10; i++){
32 test.nioCopyFile2("F://file.txt", "F://filecopy.txt");
33 //}
34 System.out.println("NIO映射到内存读写文件耗时:" + (System.currentTimeMillis() - start) + "ms");
35
36 }
37
38
39 /**
40 * NIO读写通道(双通道)进行文件的读写操作,数据使用一个缓冲区
41 * @param source 源文件
42 * @param target 目标文件
43 * @throws Exception
44 */
45 public void nioCopyFile(String source, String target) throws Exception{
46 FileInputStream fis = new FileInputStream(source);
47 FileOutputStream fos = new FileOutputStream(target);
48 FileChannel readChannel = fis.getChannel(); //读文件通道
49 FileChannel writeChannel = fos.getChannel(); //写文件通道
50 ByteBuffer buffer = ByteBuffer.allocate(1024*8); //缓冲区
51 buffer.clear();
52 while (readChannel.read(buffer) != -1){
53 buffer.flip();
54 writeChannel.write(buffer);
55 buffer.clear();
56 }
57 readChannel.close();
58 writeChannel.close();
59 fis.close();
60 fos.close();
61 }
62
63 /**
64 * NIO 将文件映射到内存进行读和写
65 * @param source 源文件
66 * @param target 目标文件
67 * @throws Exception
68 */
69 public void nioCopyFile2(String source, String target) throws Exception{
70 FileInputStream fis = new FileInputStream(source);
71 FileChannel readChannel = fis.getChannel(); //读文件通道
72 FileChannel writeChannel = new RandomAccessFile(target,"rw").getChannel(); //写文件通道
73 int fileLen = fis.available();
74 CharBuffer charReadBuffer = readChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileLen).asCharBuffer();
75 CharBuffer charWriteBuffer = writeChannel.map(FileChannel.MapMode.READ_WRITE, 0, fileLen).asCharBuffer();
76 charWriteBuffer.put(charReadBuffer);
77 readChannel.close();
78 fis.close();
79 writeChannel.close();
80
81 }
82
83 /**
84 * IO读写文件,使用了缓冲区
85 * @param source 源文件
86 * @param target 目标文件
87 * @throws Exception
88 */
89 public void ioCopyFile(String source, String target) throws Exception{
90 FileInputStream fis = new FileInputStream(source);
91 BufferedInputStream bis = new BufferedInputStream(fis);
92 FileOutputStream fos = new FileOutputStream(target);
93 BufferedOutputStream bos = new BufferedOutputStream(fos);
94 byte[] buffer = new byte[1024*8]; //缓冲区
95 int lenth = 0;
96 while ((lenth = bis.read(buffer)) != -1){
97 bos.write(buffer, 0 , lenth);
98 bos.flush();
99 }
100 fis.close();
101 bis.close();
102 fos.close();
103 bos.close();
104 }
105
106}
107
108
上述代码运行结果(demo使用的文件file.txt的大小为5M):
1
2
3
4
5
6
7 1IO使用缓冲区耗时:25ms
2NIO使用缓冲区读写耗时:38ms
3NIO映射到内存读写文件耗时:10ms
4
5Process finished with exit code 0
6
7