该用户从未签到
|
在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作,都将导致进程因频繁读写外存而降低速度.如下为一个对比实验。
$ C' u2 L- ~9 o% N( a% B7 V" j% P, S$ D) W% v B2 @
package test;
4 s+ l: S1 X3 I" o
9 J7 |) A8 C, n, o4 f1 himport java.io.BufferedInputStream;
7 t# t4 a$ f4 ?4 k4 g/ Dimport java.io.FileInputStream;
/ J8 A, @" w, G0 Zimport java.io.FileNotFoundException;
4 e& p. w4 s8 W1 z! j8 g" limport java.io.IOException;
0 V, Q- j0 T5 H1 `' B# u( e% ]import java.io.RandomAccessFile;
' ?8 b ~- P% ], y! w I! \import java.nio.MappedByteBuffer;
& H, P3 m" i! V1 Fimport java.nio.channels.FileChannel; 7 X: _0 T1 L7 [. W
( ?2 m8 T/ l& {- E/ J. mpublic class Test { - `& V/ ~8 S5 N. a( t
; O0 O6 T9 T; [- M0 B# i3 d2 }9 |. S. r
public static void main(String[] args) { / s# z+ R4 L! U& _! P1 o
try { * j+ T. e7 U/ R% q
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
* k% x! G: d1 l2 t$ B9 I) L( } int sum=0; ' O# ~0 b3 m8 R% X g( a4 P
int n;
5 A5 p8 N( y* d+ r long t1=System.currentTimeMillis();
; J5 n: @6 Y. N# A, A try { . y9 P% h+ Q# I. A# C g
while((n=fis.read())>=0){
* U) K J% W Z/ E sum+=n;
0 l4 \6 B7 @' K }
* B% p0 D- R4 R- T2 ] } catch (IOException e) { . }2 C5 \3 c2 \ h
// TODO Auto-generated catch block # \4 }" h2 }1 w
e.printStackTrace(); L& ^! F, P& M7 J. j9 ~" K! L
}
1 J2 B; Z- Z. _' o9 r4 y, Z long t=System.currentTimeMillis()-t1;
( k! }8 x. P2 H: u1 | System.out.println("sum:"+sum+" time:"+t); ! G/ j% c0 I) `
} catch (FileNotFoundException e) { + z' E& _7 i7 l0 C" C8 _! B* ^
// TODO Auto-generated catch block 5 B' s4 F8 @, p3 x) l
e.printStackTrace(); ( [ @& i: @, |* }- R/ f
}
0 }( y) ]2 ^8 V# A2 H4 k0 P2 i* `; \. n8 w
try { ' Y: e: Q: a' |! K2 {/ d
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
( l* z7 T5 G( ?1 e f+ _ BufferedInputStream bis=new BufferedInputStream(fis);
6 x6 g. H; w( x; v$ S int sum=0;
: b3 ~: L: o* O1 o X7 B int n; ( |+ @" [! I; `; E* x2 F' E
long t1=System.currentTimeMillis();
$ t5 ], V0 s7 s$ c: q9 C3 o try { ) j! h4 k4 U' q7 B% z
while((n=bis.read())>=0){ 7 D6 |" S5 n) F
sum+=n;
: ~7 s) j' r! w, E } " T% o& y( ^, w0 O; H% D8 z
} catch (IOException e) {
! w5 u1 j" k: Q% l // TODO Auto-generated catch block
4 e0 k) o/ Z6 m* t( r e.printStackTrace(); ) V8 [- I+ c! [3 ]/ w e7 W3 J% {3 V( j% n
} / m) B' u3 `5 z
long t=System.currentTimeMillis()-t1;
2 I0 ~( X6 N7 J3 W1 C+ j, r System.out.println("sum:"+sum+" time:"+t); - d: o# z1 g+ U
} catch (FileNotFoundException e) { $ i1 L0 V, u* m: H( t- m9 A1 J
// TODO Auto-generated catch block
7 K$ X4 D7 o0 M& b2 |7 { e.printStackTrace();
7 X' j, w$ d( D }
/ x4 I( ?8 ^* t5 B+ \1 z' D% p! W, l5 a
MappedByteBuffer buffer=null; 3 M, Z. I1 e( H& c3 E
try { " B" V1 r' v$ @4 h$ L* m
buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); / z" m& d8 Q$ }2 d, v- n
int sum=0; & j7 U0 N4 G% |! l6 K0 @% n
int n; . v1 r2 s. \2 k7 Y5 x q2 ] K
long t1=System.currentTimeMillis();
' W- Z# {$ l0 z" O2 z for(int i=0;i<1253244;i++){ $ h& o! |9 Q" J% b$ P
n=0x000000ff&buffer.get(i); . b6 M* T" l8 F4 m
sum+=n; : n; G& a. D0 m9 H; S$ q
}
. |4 S3 B5 W' R I3 W4 R7 i/ Q6 ` long t=System.currentTimeMillis()-t1; . o0 U% w6 p i5 M7 V7 O0 R
System.out.println("sum:"+sum+" time:"+t); 0 }* a) E$ n! r- z* @% p. g3 l: k
} catch (FileNotFoundException e) {
9 J0 f2 e4 j: E5 P( o7 x // TODO Auto-generated catch block * {% T3 H1 D' C+ E% S$ L! u
e.printStackTrace();
6 ?( e. M4 E! V) |1 d \0 | } catch (IOException e) { / G0 d, Z2 L9 |! X% v
// TODO Auto-generated catch block % Q; R" F' w8 h
e.printStackTrace(); , d+ p' \% s1 U% H3 Y
} ! n# K$ K( t! E7 l+ y0 J
- C4 R+ v" {1 E/ R' M
}
) a3 y' A% y9 ^! P( ~
8 J- J) E* X0 D} ) Y: J( c* z$ ~0 o& `
测试文件为一个大小为1253244字节的文件。测试结果:+ H) Y. M+ f8 u) \! c! B9 b( I5 _
/ t9 L# Q8 A% n' t8 s: h
sum:220152087 time:1464
; C' x0 ^/ M$ o( {9 n7 B! ^sum:220152087 time:72 : U4 v5 ^, t' [4 p& T
sum:220152087 time:25 8 ?; G7 Y( n; `/ p* t! B
说明读数据无误。删去其中的数据处理部分。
, z* [% x+ A7 {! S2 g
5 Z; S" h% l) Jpackage test;
7 i1 f6 O1 y; c+ [9 g* N7 n. c' G/ Y9 E1 h; _1 }" F/ g- p' Y
import java.io.BufferedInputStream; : J+ D Y6 k0 C
import java.io.FileInputStream; ' V# H' u! a5 }3 K
import java.io.FileNotFoundException;
# v h: R% }2 h+ f0 w3 ^import java.io.IOException;
1 Y3 }2 Q7 ]1 Zimport java.io.RandomAccessFile;
4 W/ o' R3 `+ }: fimport java.nio.MappedByteBuffer; # x- q& u+ c& c8 _- u# y
import java.nio.channels.FileChannel;
2 J" B5 S% s& X3 ?+ a% O+ k2 C. \; @
public class Test { ) Y' j) y8 ^; x' \! }5 q7 |" ^
( D: |+ M) B3 R
& L+ E: s8 F# p* F public static void main(String[] args) { ; Q6 V$ m" ^) P9 N
try {
: K1 a1 C; Q* W- D5 }. p, r FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); ; L+ _; `3 w( _. M* Q% ?! p
int sum=0; + r3 ~- |) V2 |" F4 G
int n;
- p2 U6 H: P Z P- a' q9 M7 S long t1=System.currentTimeMillis(); 3 v A/ m* r- ?1 c* ~6 N
try { ! s. F1 d" h8 w# X3 t
while((n=fis.read())>=0){
, K% W1 g/ N5 }* V( x. r2 J! z% _ //sum+=n; : z$ p( R5 r3 i4 J) i/ K+ l
}
; q7 |' _4 i' E; Y, q- P4 W, s& P } catch (IOException e) { ! X3 a2 }! ~. U0 o* A% J5 j* E
// TODO Auto-generated catch block
1 V& z. `( B/ h. f e.printStackTrace();
+ \; N: i8 s5 ^; X: O% Z' o" c9 x }
! B2 }( J' R7 h' r7 |- H7 ^. f; E5 _' w } long t=System.currentTimeMillis()-t1; 3 e4 D$ _0 B; C$ q6 N
System.out.println("sum:"+sum+" time:"+t); 7 d+ z+ \9 D, Y6 i: {; @; ~" }
} catch (FileNotFoundException e) {
5 M! @" Y5 B% W- t# B0 | // TODO Auto-generated catch block
/ E( h* X$ N3 W9 U" t+ x e.printStackTrace();
S6 [# V8 H6 [9 Y7 f s }
( I6 C) B! a, W& x! [3 H1 r% T
' p' @- h" D; S- o! Y5 L2 k& I try { ; o$ {2 R* H { d- c6 p
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); 2 T7 }! {/ P7 X
BufferedInputStream bis=new BufferedInputStream(fis);
$ K$ Z& S$ l) M. \4 w) Z- a( w3 T int sum=0; " P4 \# b8 F- T; o
int n; 3 ^( n! m. o% v
long t1=System.currentTimeMillis();
5 T/ ^$ @: _; J try { " e$ z6 O& c4 B" [
while((n=bis.read())>=0){
\& t' r* b( e" ] //sum+=n;
1 q/ D s. h, Q9 N- N# \2 G }
; u) T0 M$ O# q$ f3 w0 s } catch (IOException e) { , W' e+ o5 |, e3 F
// TODO Auto-generated catch block + O7 Q+ t' Q w
e.printStackTrace();
, w3 k) d" q! w- d. N }
8 Z4 D/ [$ w7 N% k/ a0 J long t=System.currentTimeMillis()-t1; 9 @* l2 g: O6 g; Q
System.out.println("sum:"+sum+" time:"+t); ' x: l/ G* A% K+ n( e' \
} catch (FileNotFoundException e) {
. k+ B U. X. b, w // TODO Auto-generated catch block / U/ U2 Z5 s; b( U/ \
e.printStackTrace();
& E7 z% Y/ f( {, o* H: F( Q } - l7 ?9 A$ r2 V+ C9 V) o% D# V
. f3 G; e) E) U w4 @3 ^0 j5 B MappedByteBuffer buffer=null;
* |7 h) ?9 o+ B' Z try {
6 b* C2 J2 s* y; r+ R5 {# y buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);
8 d3 ~( d0 v( b0 I int sum=0; 2 V8 `1 q" B. ?# `! V |, Y
int n; , w- f+ h$ R" C8 k( ^" @( J. K; h
long t1=System.currentTimeMillis(); ( R. A4 D' \$ z2 N0 h
for(int i=0;i<1253244;i++){ ' N; a& r5 R" J' C ~6 S, j) m
//n=0x000000ff&buffer.get(i);
) N, ]) n% p7 @* O" `2 S" I( b1 a1 r5 c //sum+=n; & ~' z/ |, a$ s r8 m+ f+ ~
}
0 y7 _* b6 z) P O6 k: v# Z5 t long t=System.currentTimeMillis()-t1; 4 c# z8 {9 A8 @- S
System.out.println("sum:"+sum+" time:"+t);
' k, ^0 b& ?9 L8 ^ } catch (FileNotFoundException e) { 7 `$ k7 A5 K7 B n5 m2 |
// TODO Auto-generated catch block z3 N' w( `. S% Z
e.printStackTrace(); @" ^6 E; Q+ ]; B7 ]! V' ]
} catch (IOException e) {
?7 b- l. A8 R6 U# Z* Q) w, R# h // TODO Auto-generated catch block
( A' W$ T$ d7 m+ U e.printStackTrace(); 9 Q" c6 L2 N6 |
} ) o! x, ]7 g$ ?! s' j5 j$ \5 e
6 R4 l" j- K$ J8 s4 h2 d } T: n% m3 y8 ^1 ]* J# f2 p
5 W/ E9 n1 P7 `3 K$ _$ u" J. H}
% ]& n# _' l1 W5 A b测试结果:
/ M8 r8 L: Y& p3 \! P+ R- E- K$ D+ ^2 g
sum:0 time:1458
; i5 ]6 T0 Q" h9 J$ [sum:0 time:67
! n2 o7 ^7 c$ Ysum:0 time:8
; [8 D7 x4 s3 g; S- ^, k0 @由此可见,将文件部分或者全部映射到内存后进行读写,速度将提高很多。
4 X1 Q# P# l* G. f5 A, j: H" A5 C" D) m0 K: r0 D) V
这是因为内存映射文件首先将外存上的文件映射到内存中的一块连续区域,被当成一个字节数组进行处理,读写操作直接对内存进行操作,而后再将内存区域重新映射到外存文件,这就节省了中间频繁的对外存进行读写的时间,大大降低了读写时间。1 u' E' ~- `7 r+ a( K4 ~
9 M: ~* [6 \, F0 K8 [" Z& R |
|