该用户从未签到
|
在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作,都将导致进程因频繁读写外存而降低速度.如下为一个对比实验。0 K* ]( b3 g m: Y
/ ?3 w: H4 K- E) G( j7 b6 p, Y
package test;
" F( u+ U% Y/ c3 P$ ]- r, f- t6 v: n" i, C+ d1 O& g" E
import java.io.BufferedInputStream; / X3 l D \/ P' I
import java.io.FileInputStream; ' E% @3 C4 _9 t
import java.io.FileNotFoundException; & x1 ^2 k0 a1 t v/ }4 g: w
import java.io.IOException; + D2 s+ W* W( @/ }$ b/ Y% c; V" o
import java.io.RandomAccessFile;
5 b4 R2 I4 g4 R. X- e6 vimport java.nio.MappedByteBuffer; $ N* G* Z! |! j0 ^5 z. v
import java.nio.channels.FileChannel;
* L0 t" K$ H9 V, e2 q. G9 @
# K& ]0 i/ ~) ]; @public class Test { 3 L* ^( q! `" X& R. w1 f
0 {7 R3 Q; A% m2 I7 f' \6 i
( k; P6 X2 s$ U" n" g5 Q2 P+ z0 }0 S public static void main(String[] args) {
`0 g j* V3 E; ^1 p try {
: k+ h/ q% q) N% X7 p FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); . r% ]( y' o+ c4 x& l9 f
int sum=0; 6 B: p/ Z& u6 L ^1 _
int n; - |; Z( \( T* b3 V! i, J7 m
long t1=System.currentTimeMillis(); ! q: l: w4 K; }# |+ T
try { 4 y' @- a8 l% p2 c4 Q. e2 F! i
while((n=fis.read())>=0){ ( T7 _9 h! Y) ~6 c( J* N, j
sum+=n; # F! S: \! f8 c6 ~4 S5 P& Z. I
} 3 g4 m' o0 ^3 h2 u! V
} catch (IOException e) { ; n; N' d: ?8 R: Y6 `5 o$ `
// TODO Auto-generated catch block
* V* Q: Z$ O. I e.printStackTrace(); + }4 K) d# v; M9 E. O
} . H8 B% o7 F' u( x+ o; c: K2 u
long t=System.currentTimeMillis()-t1; 2 {! Z/ `' X7 X6 X! Q2 y9 B1 X6 x& |
System.out.println("sum:"+sum+" time:"+t); % h. q. O; g0 i8 ~4 M% S0 D
} catch (FileNotFoundException e) {
/ J$ w4 F. Z4 f5 k: `! I9 o( M // TODO Auto-generated catch block
e. U( u6 M% {$ D e.printStackTrace(); 1 Y0 }: _% @2 G
}
. a! L- Q& E+ l& n+ i( e( L
1 Q M! j( [; s! T8 c3 n try { 8 b1 e+ J0 Y2 b+ V: j$ e+ C( i8 R
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); / L9 F) N& T4 h" c$ k7 u: @4 w
BufferedInputStream bis=new BufferedInputStream(fis); 3 q4 g; m: R' G& |( Y r8 n( i2 ~
int sum=0; C9 T! f/ z Z
int n; ; g/ e9 B3 J8 p( ~+ {
long t1=System.currentTimeMillis(); , D# k# T" J3 u& a ]- z
try {
0 |7 |& l( b* W3 k+ v0 T: g while((n=bis.read())>=0){ w% h3 G% D0 `1 B% y
sum+=n;
) M8 M' W& b( Y6 h }
- K4 I l5 k4 X4 r } catch (IOException e) {
+ R# @! ~2 K ?( N# K3 C // TODO Auto-generated catch block 2 c2 n* E6 k0 Q* t/ o0 R* W& o
e.printStackTrace();
. ^! Q- e8 I4 _) e }
' F( I; V/ w9 _/ n& Y long t=System.currentTimeMillis()-t1;
: c1 M2 [1 M! M# s0 l# o System.out.println("sum:"+sum+" time:"+t);
8 u# _: c5 |- y' g0 F } catch (FileNotFoundException e) {
i1 G1 n- o+ N. Q$ t( @' N // TODO Auto-generated catch block 6 |7 b' S7 P. W+ r2 o$ n$ I9 Z: b
e.printStackTrace(); 4 h" W; @; N8 r- ?
}
( x* Q- v9 Z8 p% c7 o
: v- T1 L/ P9 w# v MappedByteBuffer buffer=null;
. u0 d# `3 G& Q0 z try {
. f& I, \: H2 g7 A7 V) } buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); 3 r5 H+ E6 b/ ] M& ]
int sum=0;
# V1 M1 j! [' [- |1 K int n; ) ~9 a! b: W' K( i4 [
long t1=System.currentTimeMillis();
* o2 a# r3 K& p3 u, @ for(int i=0;i<1253244;i++){
7 m! Y3 M. ?1 k5 l4 p4 S n=0x000000ff&buffer.get(i);
% K Q3 i7 y0 t0 X& |1 o" n sum+=n;
7 O2 _ ^: |2 [6 g1 M/ C7 e }
% C4 r. e: G) q( d long t=System.currentTimeMillis()-t1; 4 U' m* p$ ^! v+ B+ Y
System.out.println("sum:"+sum+" time:"+t); $ q! T6 ?$ W9 r
} catch (FileNotFoundException e) { 3 W+ k& d3 \2 N+ Y4 P4 u8 l
// TODO Auto-generated catch block
$ _ O: K) {- O2 Q! _; Y e.printStackTrace();
" m1 a3 W/ o) e } catch (IOException e) { 0 a3 q: A8 b/ s! O( G7 R
// TODO Auto-generated catch block
) J1 |! H: [/ T' j- w" ` e.printStackTrace();
- L$ P* m* l. \! U0 v8 x, E0 Y } 0 n7 k9 K$ m% k
2 F2 H% j7 {4 q# y* S
} $ f3 y) d2 I* ?3 o* Z8 O; ~
, ^% r1 j. H0 z# F}
6 @ ~2 k# C& Z! v测试文件为一个大小为1253244字节的文件。测试结果:
9 e4 s- I7 @" X! T/ n6 j" U
) \ o* c$ L1 T" }& [sum:220152087 time:1464
6 P, K' T7 V8 Y' }sum:220152087 time:72 3 _0 X# U" z8 ]0 y0 U5 m
sum:220152087 time:25 6 S; _6 H! b) A
说明读数据无误。删去其中的数据处理部分。
5 f _9 w9 { ?9 W, Z' `7 B: n% F+ k' i
package test; ) y' L" L% S: g/ i
1 m, d5 g) G& Q* L, ?1 t
import java.io.BufferedInputStream; . M& ]. u3 N& |# O! S/ C* L
import java.io.FileInputStream;
- y0 [8 }' v5 n1 U5 N- V* Fimport java.io.FileNotFoundException;
0 w4 G/ z6 [" O0 ~( jimport java.io.IOException; 9 n _" g" i' y# p, d
import java.io.RandomAccessFile;
8 y* @9 ]+ w R, {import java.nio.MappedByteBuffer; : M( E0 s" c7 ~9 w9 ]; u
import java.nio.channels.FileChannel;
( l& j# ^$ L9 L5 o. n/ x9 K
) |# R* N i( z3 Ppublic class Test { - W( j) ]8 R0 O1 A2 c5 L$ j4 {7 B+ T
& l2 A6 U3 f% V1 {+ l9 M2 R
9 e0 C; m3 X4 f2 [3 Y
public static void main(String[] args) { / v& n8 L! t2 W5 ^. S) }
try { 9 q# r( W1 t9 N! F( P& X9 t8 z
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); 4 d! T! m7 W6 m3 [9 h) J3 ^% G& X3 n# p
int sum=0;
, G$ u3 `; S: `6 R4 B- M int n; . _7 T4 \/ a! `& x
long t1=System.currentTimeMillis();
* K3 s$ Z- D+ _ try {
/ J7 P- a$ d5 a7 O2 S* G H8 ? while((n=fis.read())>=0){ ( _! Y2 i% k- V1 V( @* V
//sum+=n; X8 `! m( n7 O: @
}
& I% _; t$ Z" k4 N7 G* z7 y2 k } catch (IOException e) { 1 C5 J* x9 k( @+ f- Y; a% \+ `
// TODO Auto-generated catch block
# Y' n$ i8 n% b e.printStackTrace();
X N0 M0 @/ f+ u1 a# K$ \ } 5 w J& A6 Y& |; u
long t=System.currentTimeMillis()-t1;
/ Q6 z4 ~" j* V0 b3 ^ d/ ] System.out.println("sum:"+sum+" time:"+t); 7 o( \$ F- g" b7 F) b/ s
} catch (FileNotFoundException e) { 8 j8 H2 L; r# ^2 B
// TODO Auto-generated catch block
/ B+ T; I- ?, y" v" z1 ?2 S7 C e.printStackTrace(); 6 J0 ^' q4 d/ v8 P
}
. A6 `9 K+ F/ P4 f& P& R9 z7 W
, z; f5 V& \, M/ ^: @ try {
7 U$ j/ A: ?- v* M( e. c; w) T% l$ p FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
1 Y" Z/ ^3 y9 C! ?( I5 ?& ~. ]/ e BufferedInputStream bis=new BufferedInputStream(fis); 3 J! M' l K# ]2 i
int sum=0;
. c2 |) p# v/ |/ w int n; - {4 }- ?. c. h
long t1=System.currentTimeMillis();
/ j E: z( k* e+ S6 Z3 V try { , P( Q4 J j' b C- u2 I7 I; e
while((n=bis.read())>=0){ 2 z# n( D% b1 }- o5 E$ ^2 F' }8 H. u
//sum+=n;
; H! ^3 _; T i* ^ } W* l1 `% O/ R( Y. ^+ H
} catch (IOException e) { , Z: q5 m t! D, a- |
// TODO Auto-generated catch block
, p6 W; j" x. n e.printStackTrace();
2 B' t7 [- t+ u) _: s6 D }
& N6 q: |& F5 T$ S2 ~ long t=System.currentTimeMillis()-t1;
% f, {# @0 a: U6 E" ?2 g5 L System.out.println("sum:"+sum+" time:"+t); 7 Y8 o3 d; a M. E
} catch (FileNotFoundException e) {
& _ z. O0 k* J // TODO Auto-generated catch block : c8 V+ a, B! C G2 n' n6 \4 _- n
e.printStackTrace(); / o& h2 u& p$ i7 u
}
) j( _* P" P$ Z
5 `& M& I( ^2 x3 R- \( v7 J MappedByteBuffer buffer=null;
3 n+ w- k# K0 U( H. y- d try { . H$ M& T/ `4 W% P6 T6 ~
buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); " w& e/ a t4 D% x- t" G, d
int sum=0; 9 p Q" f: F6 q3 {0 k
int n;
. p8 [- J1 l0 b2 L: g& { long t1=System.currentTimeMillis();
: a, q: G& ~* i p3 m4 C for(int i=0;i<1253244;i++){ - y* s# {6 E2 D. X' Y
//n=0x000000ff&buffer.get(i); % D: V. ]- x! M9 m7 I% I1 U+ }
//sum+=n;
) I5 a& v0 U: L1 ? } 9 z7 b5 C0 c, M1 O2 P' U
long t=System.currentTimeMillis()-t1; 2 [+ m3 l* ?- A6 m. E
System.out.println("sum:"+sum+" time:"+t);
# s; a& Q2 J% E) ^% E1 n3 w } catch (FileNotFoundException e) {
/ Z. m4 }# i% g o! L7 c. l/ z // TODO Auto-generated catch block . n2 T, g4 P% {% h
e.printStackTrace();
' v8 |6 t. p( G4 }/ T A3 U } catch (IOException e) { ( n! L& Q) R, M' W% V0 q* ^
// TODO Auto-generated catch block . x' Q1 y; m9 J/ X
e.printStackTrace();
4 O! E0 k/ K0 S } # O8 {& S6 l2 w% ]# D$ E7 z9 M
( O, l. r9 e- V7 |4 V9 E
}
, {9 T" ~5 O' \/ @9 h+ n: t7 ]+ i# q [
}
& g6 z2 a/ H( t0 x9 C7 e测试结果:+ [" n1 c: b* s1 v& }
$ ~) o4 z4 z! z6 }- X/ isum:0 time:1458 7 _2 @2 i2 \4 _+ L. z
sum:0 time:67
/ P( q* z( t0 b8 l3 _( }3 osum:0 time:8 $ e$ v3 X9 a3 C3 i% x" q
由此可见,将文件部分或者全部映射到内存后进行读写,速度将提高很多。
. X$ ~3 z# y: O' h! p
' Y* @/ ]" O# r. o这是因为内存映射文件首先将外存上的文件映射到内存中的一块连续区域,被当成一个字节数组进行处理,读写操作直接对内存进行操作,而后再将内存区域重新映射到外存文件,这就节省了中间频繁的对外存进行读写的时间,大大降低了读写时间。6 Y" k5 T9 A+ G2 e; r
- x' D g6 f6 ]3 L
|
|