该用户从未签到
|
在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作,都将导致进程因频繁读写外存而降低速度.如下为一个对比实验。2 }6 Y, o9 N1 g7 S$ V. p5 [
% ^: s: Y( O- w3 O- G+ j( W
package test;
( I A4 C8 T% o6 q" }6 E
2 }! p8 e5 A; Wimport java.io.BufferedInputStream; t& E. G v U* @8 ?0 S- `
import java.io.FileInputStream; , u, k& H+ ]: X! r. c
import java.io.FileNotFoundException;
, M4 ]( U; c% X, a) E6 Aimport java.io.IOException; & |. S2 y9 y5 X! |' w" ]+ u6 y1 o
import java.io.RandomAccessFile; 6 @, k, T8 J; W1 [- @+ D1 K
import java.nio.MappedByteBuffer;
9 a- o$ T. V, w5 I, Z2 Ximport java.nio.channels.FileChannel; , Z# ~1 Q2 o! C! ]1 e
: I7 E+ u) r) C; ~4 q) f- S6 W8 Ppublic class Test { 9 o! ^, r/ x9 s& I1 t; g
/ c0 I# S' t; o# ?* U9 \# j! S8 ^0 Q
9 j+ {+ y; d4 ]% ^' Y public static void main(String[] args) { 5 _' ~! G, u/ Y/ _& c& w
try { ' C& t" @5 U! }
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
7 C% ]3 G K, c6 g" U int sum=0;
5 w- a8 q5 T& k2 ^' }' V0 g int n;
' r" ?$ f4 G* ?$ M- s* K" l8 @' ? long t1=System.currentTimeMillis();
2 }3 W+ `9 ?8 f; X0 C) {3 |! u; H& Z try { d- d. N" c% @
while((n=fis.read())>=0){
* }/ s2 ~ U3 |/ R' @- W sum+=n;
$ L M+ l% }, {" G2 Z }
7 _4 z8 [/ L/ q$ u6 U) N } catch (IOException e) { : X- P( q- a# k3 Q
// TODO Auto-generated catch block
: b I& z" H+ _7 r' e e.printStackTrace();
) @, k4 P- \9 D, K$ S- d; A: `, F3 D. k } ) [3 g, d! `5 g, l* I$ j
long t=System.currentTimeMillis()-t1;
8 [" i" a0 n- T9 z5 T8 y System.out.println("sum:"+sum+" time:"+t); : y8 h4 [+ S4 @. H/ ?# f$ f
} catch (FileNotFoundException e) { ) A# Y: W) a$ ~! P4 O) r' _. Y. R# A" F
// TODO Auto-generated catch block
j# h Z5 V( c8 D6 a1 S' M& q e.printStackTrace(); / f; D! Y/ [5 D3 c
}
& T0 T( @! n& r3 p
4 S) V, X7 P- }! F1 l+ N6 c6 Z8 M) x try {
1 T5 g" _5 @/ m) j: R) E FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
3 I9 p* {$ V9 n! I4 ?9 u7 Y BufferedInputStream bis=new BufferedInputStream(fis);
1 `" O1 _9 Y1 P+ Y! g" ` int sum=0; 9 d! C6 w/ P' a- X! D9 m
int n; ) N4 i8 [8 O4 \4 `7 o
long t1=System.currentTimeMillis(); ' ~; e1 ~+ ?: }5 b: K
try { 2 |; ^! j1 Q4 U3 _2 ~
while((n=bis.read())>=0){ 7 ]8 z: n( ?! e- s8 O5 h# ?5 u$ Y
sum+=n;
3 S) r$ t$ K- ]3 q: D }
9 ]& P/ ~: {' [+ }! B8 K) Z7 W6 C5 n } catch (IOException e) {
7 z) n' [/ e/ d) ?! o& B6 q3 P2 l // TODO Auto-generated catch block
4 s) m2 @; H& q$ ` e.printStackTrace(); . r; F( a) I5 ?+ o$ Q5 u) | `
}
" J( ]$ ?9 T; A. [ long t=System.currentTimeMillis()-t1;
1 d" ^$ }/ P+ p% s3 R System.out.println("sum:"+sum+" time:"+t); 7 |& A4 f8 g+ `" _' l0 }/ i& }
} catch (FileNotFoundException e) {
9 |0 t7 d' [4 i4 e // TODO Auto-generated catch block
9 a5 i9 w/ [ P+ N5 c e.printStackTrace();
5 |% m, d9 v# Y7 R7 t7 n }
* _' X: X/ w- T) y' z6 |9 D( l* U8 b' Z5 H
MappedByteBuffer buffer=null; : x5 V4 n5 t/ s8 Z
try { ! y/ w e! P! n3 ~0 o* `% }0 E
buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);
+ ]7 P7 O+ z0 P8 m& A* z int sum=0;
# _0 D0 a9 a2 c- y! n0 Z int n;
?9 F5 l: s5 k1 f/ B; S" N, H* Y long t1=System.currentTimeMillis();
6 N2 n! [7 @( R& w for(int i=0;i<1253244;i++){ 9 `( f, t. ~2 J9 z
n=0x000000ff&buffer.get(i); + ]" k* L3 n1 ^: y A
sum+=n; 4 m+ D: c0 S' ^' K: s7 c8 ^+ L* X! M
}
. t3 q( ?+ u6 a: z& W9 W5 l5 } long t=System.currentTimeMillis()-t1; 7 K: U3 @( t, b" t# t
System.out.println("sum:"+sum+" time:"+t); ; Q6 B* f/ E" ~% M5 d
} catch (FileNotFoundException e) {
V; u& b. L1 {) e, c; D) r" Z) o9 d // TODO Auto-generated catch block
1 k) C1 I$ I4 T+ c' L e.printStackTrace();
; ?5 ~* ~: H R8 T3 a: ?. p3 Y } catch (IOException e) { 0 l# H0 J `# L" G- |
// TODO Auto-generated catch block
' C& J6 m* X! u0 d5 D e.printStackTrace(); ) v- S# ]) r; V$ n0 K5 {
} % Z$ x, ?; B: C
7 I J8 t* ~+ h$ _6 {
} : k4 M. J# N# ^, I# U
' L1 @6 d( B- u& Y9 h# B
} 3 d% v. p. a2 M! L" I
测试文件为一个大小为1253244字节的文件。测试结果:8 Q8 L( l1 l7 f- D5 T
6 E3 q8 Z7 y$ k5 \" H( hsum:220152087 time:1464 6 f! ^9 c4 U9 _- v9 h* |, S
sum:220152087 time:72
1 e2 l, O& D5 Lsum:220152087 time:25
+ R1 G A, _6 {说明读数据无误。删去其中的数据处理部分。, Z" \. l' z2 _) {- U! v' i
% k, s( ]- k2 [: E# n1 @! R6 Mpackage test; 5 e, F7 g" `, I: O0 D1 H8 T
% L7 }5 t! H" b5 O, _. Yimport java.io.BufferedInputStream; u$ J3 s/ l7 E9 M) n
import java.io.FileInputStream; 0 u# F1 M8 R( v% [
import java.io.FileNotFoundException; 6 x e4 W! \9 v' @/ B6 X
import java.io.IOException;
. E$ p' Q; i- ], |* A6 N' \- ^import java.io.RandomAccessFile;
: J A, K% I' d) F( ]8 F4 ]8 Bimport java.nio.MappedByteBuffer;
1 d1 ^( _- k6 [ @3 b p0 ]$ N8 Wimport java.nio.channels.FileChannel; 8 k4 x3 T! g1 T
! o% l2 F/ j6 ?# ?! A' U. |* {) lpublic class Test {
! C* [2 E$ |3 t8 c3 z; {- ?5 ~# z( f
- ^* z" W" ?5 @' D" x! u- E' F public static void main(String[] args) { ! s; f# m" ~7 D( `
try { + \8 B* e, O- K; D, @
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
; K/ |2 `# h p int sum=0; 2 e$ l6 a* d6 k5 H9 f: {& ]( l, f
int n;
+ y# z: U) I P& ]5 Q# \' S* W long t1=System.currentTimeMillis();
' P& P0 [. u1 i2 a3 c! t) j7 j+ _ try { 5 l2 s/ P- b; z! Z' c1 q2 n
while((n=fis.read())>=0){
4 P9 X# P+ D5 ]+ r) K- ^ //sum+=n; 7 b* A; t; N3 R7 Z/ g$ Z( N
} 2 r7 b q& B8 G% @9 ] l+ ?2 Z6 |
} catch (IOException e) { 9 o$ d7 Q) G- D! r- F
// TODO Auto-generated catch block 4 J) }; k& S5 D* s! Q5 v, J7 M7 S
e.printStackTrace();
# X/ V1 o# Y/ [0 U( l }
% s9 V$ p+ g. ~, `/ B& `5 v long t=System.currentTimeMillis()-t1; 9 B ~/ L8 f3 ^5 A
System.out.println("sum:"+sum+" time:"+t); ' z. |# i4 L, o9 q5 A9 j
} catch (FileNotFoundException e) { % U6 h) k6 D* v$ C
// TODO Auto-generated catch block ( ^6 [! z0 X4 s
e.printStackTrace();
$ i, R e3 P$ \: y } - x) s# o: O2 t% p/ ]+ o
) ]# K; H! C. d% a$ K$ l2 q# C try {
6 Z+ U+ ~: Y) v6 }: z! _ FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); t1 `# P4 g3 |! e
BufferedInputStream bis=new BufferedInputStream(fis); ' | ^( |) D9 G- }& x7 W" b
int sum=0;
1 ~# Y* d4 U* l) f8 V1 b int n; 5 p8 Y2 `+ i7 I' {3 R; A2 B
long t1=System.currentTimeMillis(); 3 y% W4 a, @: u9 j0 t$ e1 G
try {
+ n) z, H9 S, e4 F( V while((n=bis.read())>=0){ 5 \5 ~$ X y9 M; j3 e c
//sum+=n;
9 Q5 E+ ~+ W2 q# b } ( M1 _2 j# R* n: g# J4 L: {. x
} catch (IOException e) { ; l/ @0 c/ `( w3 c) i: o% T. K; a
// TODO Auto-generated catch block 8 E# |# o0 p. h5 H+ M+ q; k0 D; p y8 s
e.printStackTrace(); ' G! H- a. P. b. J- K
} 4 F+ n& ?- w, M/ V! z% _; G& ?
long t=System.currentTimeMillis()-t1; 7 Y6 m1 h- X7 k
System.out.println("sum:"+sum+" time:"+t); ( m- I- c$ c! Z& a, Z% ]
} catch (FileNotFoundException e) {
0 r$ h) S4 h* |1 U // TODO Auto-generated catch block 0 w! \3 Y; ^8 o
e.printStackTrace(); + d* z2 S. D/ L4 ^- B P$ k
}
5 e7 n7 d: a. d& M W* L, m# k
MappedByteBuffer buffer=null;
) x7 U! q4 T7 a5 W) f7 Y5 e+ t try {
. `1 f5 _. d8 g, a0 N buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);
5 `$ G4 c5 |4 ]/ O( {9 W int sum=0; T7 \, ^/ e: [
int n;
. v5 h. P) Z* N* F2 T long t1=System.currentTimeMillis(); 3 B6 g- g' _! Z* }5 P! O; ?
for(int i=0;i<1253244;i++){ # h4 f% h3 |! E2 P9 I
//n=0x000000ff&buffer.get(i); - H" H9 i6 o& |3 P6 L
//sum+=n; ! p, N. ~/ H' v) H. q4 Q6 z
} . Q$ Z9 V( m$ z' l8 W. ]
long t=System.currentTimeMillis()-t1;
4 ^+ E" |( I- }+ f9 O System.out.println("sum:"+sum+" time:"+t); 3 W6 |4 f/ t# t
} catch (FileNotFoundException e) {
1 N0 [* d" C0 X2 a v // TODO Auto-generated catch block
. N8 u( x/ x. Z' |0 x$ O e.printStackTrace(); 5 K9 U; ^) N" i) F
} catch (IOException e) { & ]' M$ j( U0 m$ p
// TODO Auto-generated catch block # ?1 N) d+ n& a4 n% V: ?9 N V
e.printStackTrace(); : B9 S* f* ^, u
}
2 Z9 W6 ~% N8 c- e6 e1 k: Z1 y% V1 }
} 3 U) f" h- ]. W4 b4 R# N
2 [1 f, W1 _% U* z+ m% X
}
6 X8 b* N' e0 ?1 @4 g0 t测试结果:
( d$ a5 D }7 O' i2 J
# }- W [; N: N! |) msum:0 time:1458
, x# b5 W0 o* [( X, Ssum:0 time:67
9 Z- s0 m/ P$ usum:0 time:8
! i7 P$ N! u' H! \由此可见,将文件部分或者全部映射到内存后进行读写,速度将提高很多。- a( M" [- [0 l6 o# J4 Y
- i3 d, D. @/ q) q& J% O. {0 m
这是因为内存映射文件首先将外存上的文件映射到内存中的一块连续区域,被当成一个字节数组进行处理,读写操作直接对内存进行操作,而后再将内存区域重新映射到外存文件,这就节省了中间频繁的对外存进行读写的时间,大大降低了读写时间。9 \% X2 m% _. T* Z4 K
9 a+ {4 w7 u5 g# k) q3 z) E
|
|