在處理大文件時,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 來進行頻繁的讀寫操作,都將導致進程因頻繁讀寫外存而降低速度.如下為一個對比實驗。
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
|
package test; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class Test { public static void main(String[] args) { try { FileInputStream fis= new FileInputStream( "/home/tobacco/test/res.txt" ); int sum= 0 ; int n; long t1=System.currentTimeMillis(); try { while ((n=fis.read())>= 0 ){ sum+=n; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } long t=System.currentTimeMillis()-t1; System.out.println( "sum:" +sum+ " time:" +t); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { FileInputStream fis= new FileInputStream( "/home/tobacco/test/res.txt" ); BufferedInputStream bis= new BufferedInputStream(fis); int sum= 0 ; int n; long t1=System.currentTimeMillis(); try { while ((n=bis.read())>= 0 ){ sum+=n; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } long t=System.currentTimeMillis()-t1; System.out.println( "sum:" +sum+ " time:" +t); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } MappedByteBuffer buffer= null ; try { buffer= new RandomAccessFile( "/home/tobacco/test/res.txt" , "rw" ).getChannel().map(FileChannel.MapMode.READ_WRITE, 0 , 1253244 ); int sum= 0 ; int n; long t1=System.currentTimeMillis(); for ( int i= 0 ;i< 1253244 ;i++){ n= 0x000000ff &buffer.get(i); sum+=n; } long t=System.currentTimeMillis()-t1; System.out.println( "sum:" +sum+ " time:" +t); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
測試文件為一個大小為1253244字節的文件。測試結果:
sum:220152087 time:1464
sum:220152087 time:72
sum:220152087 time:25
說明讀數據無誤。刪去其中的數據處理部分。
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
|
package test; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class Test { public static void main(String[] args) { try { FileInputStream fis= new FileInputStream( "/home/tobacco/test/res.txt" ); int sum= 0 ; int n; long t1=System.currentTimeMillis(); try { while ((n=fis.read())>= 0 ){ //sum+=n; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } long t=System.currentTimeMillis()-t1; System.out.println( "sum:" +sum+ " time:" +t); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { FileInputStream fis= new FileInputStream( "/home/tobacco/test/res.txt" ); BufferedInputStream bis= new BufferedInputStream(fis); int sum= 0 ; int n; long t1=System.currentTimeMillis(); try { while ((n=bis.read())>= 0 ){ //sum+=n; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } long t=System.currentTimeMillis()-t1; System.out.println( "sum:" +sum+ " time:" +t); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } MappedByteBuffer buffer= null ; try { buffer= new RandomAccessFile( "/home/tobacco/test/res.txt" , "rw" ).getChannel().map(FileChannel.MapMode.READ_WRITE, 0 , 1253244 ); int sum= 0 ; int n; long t1=System.currentTimeMillis(); for ( int i= 0 ;i< 1253244 ;i++){ //n=0x000000ff&buffer.get(i); //sum+=n; } long t=System.currentTimeMillis()-t1; System.out.println( "sum:" +sum+ " time:" +t); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
測試結果:
sum:0 time:1458
sum:0 time:67
sum:0 time:8
由此可見,將文件部分或者全部映射到內存后進行讀寫,速度將提高很多。
這是因為內存映射文件首先將外存上的文件映射到內存中的一塊連續區域,被當成一個字節數組進行處理,讀寫操作直接對內存進行操作,而后再將內存區域重新映射到外存文件,這就節省了中間頻繁的對外存進行讀寫的時間,大大降低了讀寫時間。
以上這篇Java中用內存映射處理大文件的實現代碼就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。