该用户从未签到
|
在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作,都将导致进程因频繁读写外存而降低速度.如下为一个对比实验。
# q; Q/ ~' V! ~( P* z6 N
, q% w4 ~5 t% ~. t9 o; I, A/ Ipackage test; ( I3 q' C4 D3 F' T4 R3 T
9 u5 b/ g' f& W& x0 z' ^9 `
import java.io.BufferedInputStream;
1 Y2 l1 M' q$ t9 p* v. fimport java.io.FileInputStream;
% B7 G( }+ A. G! h, p. T; Pimport java.io.FileNotFoundException;
( G t+ r; a3 _. }( }3 {import java.io.IOException; % B/ q9 V9 }# t; Q
import java.io.RandomAccessFile;
. j5 n6 m! g- {! P# g$ `; _import java.nio.MappedByteBuffer;
- A @1 P! ^0 x) u ?4 l O6 n& Wimport java.nio.channels.FileChannel; 2 V l+ Z. f1 E+ e1 f$ P" }9 c
4 Z# V+ t( Z0 i/ o( D7 bpublic class Test {
& j& G4 B/ M) e" y/ M7 F$ X, b$ {5 I5 Y7 R3 K* O
" F% C7 \: c8 Q8 |" S% l public static void main(String[] args) {
) H# m. P3 r$ r l; h try {
( K& J7 C# \- t9 _' T) _: R FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
6 K( _: I3 `2 O( k0 H- v int sum=0;
; Z$ v2 Y* Z0 [) z int n;
3 y$ L- }; t: w- Z long t1=System.currentTimeMillis(); ( @* U* J y, I `0 i5 i
try { 0 u' v0 e) ^% _" q' w
while((n=fis.read())>=0){ + E! I$ e$ t+ k# N0 z
sum+=n;
% ?! o# V! s1 [1 B' R0 n0 v } & w1 m2 { f# ^6 A+ l: L4 @& i
} catch (IOException e) {
4 h& ?$ W. ^ t# s3 V // TODO Auto-generated catch block
3 p7 }2 w, z0 T0 i e.printStackTrace(); ) o. s4 {2 F9 f1 y* _
}
1 P( h0 ]; o! ~' G9 k8 }" X! C0 u- M long t=System.currentTimeMillis()-t1; 6 L: @" g6 B6 f- u9 [ i. ]$ f+ Q
System.out.println("sum:"+sum+" time:"+t); 6 f/ i- n6 z4 U y8 [/ U
} catch (FileNotFoundException e) { ; b( ~, J7 ~: r/ z
// TODO Auto-generated catch block " L( `, D u' O; p" b
e.printStackTrace(); + W0 B0 t" H' y# D, J
}
7 s- m( E. }6 q& D8 ]8 u4 s2 E2 M9 [4 t6 B% p8 N6 B. V% B8 ?/ R w
try { ( b' a+ Z- T" A6 L2 I6 U2 S3 r
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); " J7 X. y, s8 V5 T6 _
BufferedInputStream bis=new BufferedInputStream(fis); 4 @! f& f$ k! Z
int sum=0;
9 b& t! D, f0 B& h1 T int n; # ]! }* W: `( p' r/ h. T( z2 E
long t1=System.currentTimeMillis();
- J- f7 a9 T2 B9 B3 }: F try { 2 h- s" s* @' S/ B7 g7 R
while((n=bis.read())>=0){ ' H! E8 I% ~- w/ t- G9 ~- \( @/ E
sum+=n;
" N% d! D$ T5 [ } ; T+ p& b' Z! M5 n
} catch (IOException e) {
1 ]6 H0 _* [1 z f$ R* ]3 d // TODO Auto-generated catch block 3 A# ?3 V8 E# k% P! d. ^
e.printStackTrace();
+ g( G( q( \4 z" o2 z } + D" c' q1 C: C `
long t=System.currentTimeMillis()-t1;
! n' `4 N7 N0 s2 w: k System.out.println("sum:"+sum+" time:"+t); " D5 E- P# _' @+ d: F
} catch (FileNotFoundException e) {
- g% t7 S+ Y% }5 E- h4 q z+ [/ d! ^ // TODO Auto-generated catch block
. n; r" O& j( \4 y$ ~1 r% ] e.printStackTrace();
! e) s6 L5 c0 _ }
! ?$ ?. [8 C/ X4 ^4 b# G* [% x/ G- v9 {" V' s; E4 i4 w
MappedByteBuffer buffer=null;
3 R" R+ f4 ]* w4 m2 x6 m5 Y try { * D/ z) ]& q% A
buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); , L/ j; T, b" Q& L% a* ~
int sum=0; ( j$ D* }( K. S
int n;
. E, {7 X! N3 ]1 @9 h long t1=System.currentTimeMillis(); & F: P8 S2 o; I( ]2 ?. p& G7 c
for(int i=0;i<1253244;i++){
' V: ~ V, R- R n=0x000000ff&buffer.get(i); : V+ I' D0 P6 x: }
sum+=n; % N# W) O6 S* f6 c9 i, h
} 9 d" E8 ^7 |1 K' l
long t=System.currentTimeMillis()-t1; 0 R9 \- ~ J; G0 X2 k: d
System.out.println("sum:"+sum+" time:"+t); j6 T$ F& B+ A( X" Z6 v& l& k
} catch (FileNotFoundException e) { * ?% N1 t3 | \6 N5 @
// TODO Auto-generated catch block ' b9 b4 s# b' f% S# c0 b- i. H
e.printStackTrace(); - [4 r# u% R) m, v: B2 ?& d
} catch (IOException e) {
( I5 }( g3 S0 N# l8 ?! U+ Y+ r // TODO Auto-generated catch block
7 U( u: r, ]; h e.printStackTrace();
! E- r: r3 D6 G/ b, O }
3 x- ^# l6 m! R! m+ V0 w) f2 ]* F
1 c2 p( D( b" Y* t; M; J } $ p+ ~/ `# {: j9 f. t: D7 R/ t
1 `, @4 w2 i$ g, J} 1 x. C5 O# v j; ]
测试文件为一个大小为1253244字节的文件。测试结果:
: a/ h5 x6 @. k4 [8 }- Y( c5 ]: g: j9 p$ v4 o
sum:220152087 time:1464
. @1 y* v: Z, ssum:220152087 time:72 4 U* q2 X3 b- S, `/ X
sum:220152087 time:25
7 s& K$ ` |% a9 o( T' l说明读数据无误。删去其中的数据处理部分。7 d5 W' V4 |' N
# G6 i& U3 T( ~- B( E1 t, f2 i2 h7 s4 d
package test;
9 Q( d; N0 a' N0 v( U' H) Y2 S8 v# Z+ Y% D+ P) A
import java.io.BufferedInputStream;
l* N) G4 i( M4 r) X% Kimport java.io.FileInputStream;
3 U5 A# D8 u! j2 Simport java.io.FileNotFoundException;
" q. V1 S/ {2 Eimport java.io.IOException; 2 J$ T$ O# T9 i
import java.io.RandomAccessFile; & w$ x- ~9 j2 b* T0 l" E B
import java.nio.MappedByteBuffer;
4 G1 X! T) o8 z; T- h. Rimport java.nio.channels.FileChannel; % b" p( @5 Z* I6 O/ u% K- l( _6 @- Z
: o% I# e7 I7 _& w- Kpublic class Test { : m2 v( b5 {- a6 k0 Z
4 u( _4 k& g+ [( R
" p) {% s& r- T! T) @. l% ^ public static void main(String[] args) {
6 x: X# \, I0 l+ x$ w6 _) l; W+ \ try {
6 h8 N( x" c$ m! K8 | FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
" [1 m3 ^3 s1 ~# J& I8 h/ P int sum=0; # H4 w+ Z$ y0 n8 N2 Q( P, w2 P, ?- \
int n;
6 y1 C7 `5 ~0 r; y long t1=System.currentTimeMillis(); * n( R6 `7 \# C4 j
try { 6 n! w! {2 `0 R9 ]
while((n=fis.read())>=0){ / h* S! F) w& t; F. d
//sum+=n;
- }1 W3 I1 J- k0 f8 ?$ r }
( h7 s8 a& V, {. B& z } catch (IOException e) { 4 K9 d. t4 V& _' z a
// TODO Auto-generated catch block : Q8 O& `/ C$ o% u4 U6 z
e.printStackTrace(); 9 G6 S! ?1 h4 D4 Z& j* I- d! s
}
0 e c; S) u% g4 Y7 j long t=System.currentTimeMillis()-t1; - ?4 X7 V! C$ I. u) v$ u
System.out.println("sum:"+sum+" time:"+t); % f" v" P% f! g
} catch (FileNotFoundException e) {
) I" A9 j/ [* a. p // TODO Auto-generated catch block : F$ ^, G; V+ d+ r1 ?
e.printStackTrace();
% F: c% j" ?# t, ] } 4 I' s$ P+ i7 W2 U4 ~( I, {
3 b) T' ~+ |; _- F* D6 W try {
2 ]; @7 z! a! R6 d6 l FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
* w/ p3 p; c5 Z, b) }/ d. A3 m BufferedInputStream bis=new BufferedInputStream(fis);
2 l- E8 e5 z; j& s( ^5 ?3 d0 p4 l int sum=0; $ t- ~- \, E4 n2 h. |. T3 J
int n;
! Q/ _- @" [' {/ r5 [8 Z, h long t1=System.currentTimeMillis();
! s: G- K9 w: t) O" b: U0 x8 E try {
* D* R% `- H, ]9 q while((n=bis.read())>=0){ t8 C" B! W, y% q7 {% d
//sum+=n; " `0 @2 e5 d% [8 e3 p$ Q. i
} - F C% V, d, ~& s# D
} catch (IOException e) {
{" J* Z X3 j // TODO Auto-generated catch block ) v( Q8 J1 g! W; A+ G" c# C
e.printStackTrace(); $ T- [ S- P+ F9 Y9 g9 w) z
}
3 \9 p: ?( L& {2 I: x( \" q4 @# z" U2 G long t=System.currentTimeMillis()-t1;
/ f5 _6 P# R- \$ M System.out.println("sum:"+sum+" time:"+t); Q2 X5 e( `0 F! @8 J' w
} catch (FileNotFoundException e) {
! Z. W# b( R) u& S+ C) L // TODO Auto-generated catch block
0 R9 i3 Z, d) y$ t7 v e.printStackTrace(); " n6 b [$ t/ C# A! E
}
& Q5 S0 d( X9 |- R* p3 j4 q( T% _5 X1 K' H7 b& B
MappedByteBuffer buffer=null; ( H- q, \7 C M" ?) x
try {
/ z7 O! m; F' P) S buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); 3 b; Y, D8 o3 c4 m/ P% w
int sum=0; 0 f& [0 L4 f, I. a0 `- x
int n; : D0 V5 r" q+ S4 K/ B, `& L
long t1=System.currentTimeMillis(); . r' Y; A- D- K {3 G
for(int i=0;i<1253244;i++){ 6 _, j8 U0 L# W9 s e) W
//n=0x000000ff&buffer.get(i); % i& M# ~ d, e* q* r0 ]2 H
//sum+=n;
H# b$ x$ E4 Y+ ^# r( m }
% }3 W: u' h' _ long t=System.currentTimeMillis()-t1; 8 G9 @( N, u" N$ m4 `
System.out.println("sum:"+sum+" time:"+t);
" [6 L1 M+ f! Q+ T: D } catch (FileNotFoundException e) { ) R$ n) B- ]0 J4 B4 \
// TODO Auto-generated catch block $ U8 C `- C& y9 V. m
e.printStackTrace();
- n, z% O/ [/ p } catch (IOException e) { " F5 z1 Q4 f5 V! C
// TODO Auto-generated catch block ' y, ]3 _$ n5 Q/ S* ]
e.printStackTrace();
4 _& j' t4 S1 i3 A4 l' A0 W } # O9 j' f$ j. k N" E$ B" u
1 a5 @% J" T6 q7 s
}
/ x& t0 e3 K/ U; H5 h0 o7 a+ r7 U7 R# u0 V, s) {
} : C2 U5 d+ K8 U7 j9 z4 [1 X4 d
测试结果:
7 b, ?' J! l; a* f" w7 u- h' O+ j4 D$ g* H0 s
sum:0 time:1458
7 U6 k. G5 u( [/ U5 W! d0 Osum:0 time:67
, q9 F$ C% l0 n# @6 V) C; j! N# S; Gsum:0 time:8 ( B/ F$ h3 K0 [0 m7 f
由此可见,将文件部分或者全部映射到内存后进行读写,速度将提高很多。
. ]2 p; M6 A8 {& H: @5 i$ F
0 X/ ]( X" g; G4 F- u9 X5 l这是因为内存映射文件首先将外存上的文件映射到内存中的一块连续区域,被当成一个字节数组进行处理,读写操作直接对内存进行操作,而后再将内存区域重新映射到外存文件,这就节省了中间频繁的对外存进行读写的时间,大大降低了读写时间。. J7 d3 _. Q. M1 I5 V
3 V8 H3 R6 ?' @: h |
|