该用户从未签到
|
在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作,都将导致进程因频繁读写外存而降低速度.如下为一个对比实验。1 z& ]0 x% b! I3 {/ q
* U( W* ~3 f9 G" `' Ypackage test;
4 O( w% u n( _1 I- F# f
7 S, x I6 ~9 T D7 y2 K" b) Gimport java.io.BufferedInputStream;
- J+ A; } ?4 h& h+ f7 Uimport java.io.FileInputStream;
( h4 [' s: y6 z( H9 }import java.io.FileNotFoundException;
$ L0 f$ [5 `7 J$ x, ?0 qimport java.io.IOException; 3 E F) x3 K4 f* _, i
import java.io.RandomAccessFile; 3 G+ c9 v$ v3 W6 C7 M' X9 |' ?' ?
import java.nio.MappedByteBuffer;
% P0 k# B$ c3 A6 Gimport java.nio.channels.FileChannel; % q( O$ i" f$ o2 j1 u: k) Y
; G# D) L! f. [public class Test {
: ?# u9 k& c# H: n$ E5 `5 x' x$ }/ B6 Y! S+ m- l& w+ m
$ {% H- h; r2 B
public static void main(String[] args) { 7 v8 ]& S$ f. j6 c
try { 2 h' t% r5 h& x* V9 _
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); : U6 X9 k/ s; ?8 ~7 x4 s
int sum=0;
' `* x: ~1 O0 f, ?7 z B& \ int n; . ]8 @: o4 m3 C$ F& ~
long t1=System.currentTimeMillis();
5 t& o3 v A; U try { : ?" | Q! C* U; u
while((n=fis.read())>=0){ 3 m2 w8 E) M6 |
sum+=n; ( N6 Q! p& S7 z' ?
} ' |( a+ A+ K" ]" s
} catch (IOException e) {
3 t6 E0 q# b# \4 b1 m // TODO Auto-generated catch block 8 _0 b$ v7 I$ i' ~; P p
e.printStackTrace();
4 ?6 g6 A4 l6 C# Z# v2 Q% I } 1 y' d S! D6 H1 h* s9 h
long t=System.currentTimeMillis()-t1;
3 b1 t2 ^3 o, h System.out.println("sum:"+sum+" time:"+t);
8 u$ c) b! Z8 U) F% v. X } catch (FileNotFoundException e) { 5 F, M a1 _4 k$ ^* l4 L
// TODO Auto-generated catch block
! j% V7 J: B+ d7 l* n e.printStackTrace();
0 g; d2 w8 V' o+ q6 n/ V3 v& U# z } ) z! @: v% r1 o+ i, S
$ }- {# f% D- Z1 U8 l5 r9 J try { " l. G. q5 N* v8 ~) j1 {6 W
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); 1 L, l8 T. G. g% W! ^
BufferedInputStream bis=new BufferedInputStream(fis); ) N5 O2 v* Y; ~; Q
int sum=0;
! E. C5 K7 a" u6 Y int n; 2 N. B" n; g& |* L! M& T$ c% }
long t1=System.currentTimeMillis();
( e" l4 @0 f* s8 T- `$ } try { , e0 N' \* b. g; _9 ~# ^
while((n=bis.read())>=0){
5 w% s4 ^0 C( ]% U8 _* u sum+=n;
0 p, S# B% j. {0 o& \ }
( b0 G# B; H( a! p# \9 e } catch (IOException e) { 5 n0 { `- a- |! c8 b# [. T) Q
// TODO Auto-generated catch block U1 ~' E& f- R) O" }
e.printStackTrace();
0 Z/ K) y4 W( O& F }
9 j+ k' j% q2 w$ Z long t=System.currentTimeMillis()-t1; 8 y9 v( v/ {) i. r7 z5 S
System.out.println("sum:"+sum+" time:"+t); ! L$ Q( f, k/ C7 s* A8 ^ s+ h
} catch (FileNotFoundException e) {
5 y7 s L: \$ s1 b x // TODO Auto-generated catch block ; [/ Y+ D4 K+ L5 A
e.printStackTrace(); / ?" I' f+ o$ J! o5 @, `* P2 ]" b
}
: d0 |* `0 _3 Z' x# C
5 s! y! f1 |# t4 e* K* b; \( L! H MappedByteBuffer buffer=null;
1 n0 A# r' p* X) v0 [& d$ x try { 3 b% q. E$ y7 b) e
buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); 9 i. T4 s/ `2 ~) V- R' ~+ J
int sum=0; # W! j! S* T4 Q6 x
int n;
( w$ Y- l; X1 k) w+ W5 s' x long t1=System.currentTimeMillis(); ; R* J W4 ?6 [, D- Z; Y" o2 K0 q
for(int i=0;i<1253244;i++){ 8 y: H' B7 |; d3 f& \
n=0x000000ff&buffer.get(i); 5 B% Y& y0 Y- h# y' ?8 D
sum+=n; ! p$ L' X& ]7 E6 ]# a
} 9 O8 e$ B. T5 O$ ~. {2 l
long t=System.currentTimeMillis()-t1;
0 c; @/ v8 N0 q6 n8 N4 J System.out.println("sum:"+sum+" time:"+t);
% R( L+ q! D- h5 o } catch (FileNotFoundException e) {
3 r& e" I+ Y2 M1 O // TODO Auto-generated catch block
/ I; b: A t, O5 ]+ i) f# y- x/ ~ e.printStackTrace();
0 v( u, M% s2 A9 J* p } catch (IOException e) { * X$ f) B7 k. t/ m+ i) T
// TODO Auto-generated catch block
" C* K) ~6 _+ Z, P) D! ~- e e.printStackTrace(); 2 X1 P& Y# P5 ]! G3 p6 @7 a. @" K
}
5 D4 ]" C( y% a: F7 ~8 H3 Q: W0 ?# ~
} % C( ?6 p! |; J, |! @3 A& e$ {8 W
# @8 Q6 s/ L: u2 ^* }/ I! t$ j" K0 v2 v} 8 u5 w" R. l) }9 U1 k1 V
测试文件为一个大小为1253244字节的文件。测试结果:
0 M) W. @& u) T
0 M3 J% b! {" L- c, fsum:220152087 time:1464 4 }, D# C: f# f1 Z9 ]. ~
sum:220152087 time:72 9 B* W3 c+ ]- [+ O
sum:220152087 time:25
5 _0 F1 r, k6 R说明读数据无误。删去其中的数据处理部分。( e Y; {9 m7 L
& g% N: I5 _) i8 q
package test;
' t) s1 O- C# G/ R% R/ M9 _' h" @" Z T6 B* ?( n
import java.io.BufferedInputStream; * V( e0 I E, Q5 C1 M
import java.io.FileInputStream;
( f5 l5 r1 H2 q4 ]7 g e6 Jimport java.io.FileNotFoundException;
. L! i) i$ i6 R) Aimport java.io.IOException;
( K( P5 h t5 n, F. ^$ p0 }- Oimport java.io.RandomAccessFile; 4 X( }2 K% ?0 S4 _8 v
import java.nio.MappedByteBuffer; 3 C4 Q" q: ?& S( l: `4 ]; Q( w6 e
import java.nio.channels.FileChannel;
3 n) x$ A. R% R+ s; ^7 E3 @, l# C, n3 W4 Q2 `
public class Test { " S1 r& w1 u( l) I0 [7 Z9 s; u
, Y6 q* ~# q- C/ |# L5 M+ P
% l+ f, L! w; `. F8 j) p public static void main(String[] args) { + }7 j1 `3 E; p; r& ~# c
try {
% }. X7 S4 d: ^* s FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
: e& A: H7 ]% B- a& ^5 ? int sum=0; : G/ t! P# K. t
int n;
( n3 i8 X e* K% ~& D$ T5 o long t1=System.currentTimeMillis(); ' O& a, Y! q, U: u& V
try {
/ S# L9 Q3 R8 ` while((n=fis.read())>=0){
! t) V' R; M! x3 ?+ @& E, R //sum+=n; ( \2 c# e8 @. [) F" ^$ \' y
} / S8 A& r! z4 B$ a) Q7 r, Z
} catch (IOException e) {
9 @, ]& N0 u5 c // TODO Auto-generated catch block
- H' [$ c* W& L: v e.printStackTrace(); : y/ A, D3 j$ ?' W& d! x' {& Z
}
. d6 u7 H( D3 i( d5 e5 s/ v! g4 C6 B long t=System.currentTimeMillis()-t1; 9 P$ X1 h) j3 K% l0 `; ]' j
System.out.println("sum:"+sum+" time:"+t); ; A2 S0 h, U) T
} catch (FileNotFoundException e) {
4 r: d% u4 y3 E, T/ n# V- T // TODO Auto-generated catch block
+ f$ K' K8 ?; `) T e.printStackTrace(); ! d k; `% Q" y
} $ T. ?2 S2 X/ M. z5 u
4 k& u8 L7 ^6 s+ u5 [4 O+ n: Q9 C Y try {
! N1 Q1 X2 P0 f, Q: g9 s' B5 O FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
2 }1 t/ S# k9 `' s" i* d5 p- G& }4 _+ F9 M BufferedInputStream bis=new BufferedInputStream(fis); ! H7 l+ o9 t' J. ^8 T. t; ]* m5 @
int sum=0;
6 v; ]! N6 B/ c$ ]/ T1 q9 m int n; # R3 J) l: i$ m: y# x/ r, |* Q0 }, K
long t1=System.currentTimeMillis(); # G4 l* Z& V( J
try { ( s( x( }2 P; O W
while((n=bis.read())>=0){ 3 ^' {& G9 y3 I) E& r. f
//sum+=n;
6 Y/ N* | u' s9 F } 8 Z) [9 u3 o5 m$ b9 g' q' g
} catch (IOException e) { 6 J, d+ {' Z/ n
// TODO Auto-generated catch block
' Z- P- E6 P6 g7 V. W- V8 b* [ e.printStackTrace(); 0 _& E4 a4 p! |/ w; [" v- C
} Z& c' J, Y1 \0 M( k3 I9 N
long t=System.currentTimeMillis()-t1;
; N5 P |4 i( i. q1 v: Y System.out.println("sum:"+sum+" time:"+t);
/ L+ c) t: C1 o } catch (FileNotFoundException e) { ( X& d6 M4 ?2 r8 m W
// TODO Auto-generated catch block
; J. ^. C' Z% u e.printStackTrace(); " | g1 ?6 D% u {8 \) Z$ {7 H
}
7 K' P9 L5 c2 t" ?6 X( i1 v
5 m1 C" ?& c4 N& Q MappedByteBuffer buffer=null;
6 ^ L* [& r J8 o try { 8 _) ~3 V" x( R4 K, j
buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); ' a8 }0 D- `( Y, S9 S9 ^, b, O* }) U6 l# N
int sum=0; ' r2 S+ n: o- B
int n; ' i9 _! d m9 D5 r" [: v0 I
long t1=System.currentTimeMillis();
* f! k& O& L% E4 `' F$ g4 T% B; G for(int i=0;i<1253244;i++){ % M' {3 ? o2 l7 x, Z
//n=0x000000ff&buffer.get(i); ) W. j0 e" k6 M* X! m9 x7 ?$ N
//sum+=n; 9 k$ @) Q6 X- \/ l& j- L6 l' S
}
% Y8 w% n9 I/ I long t=System.currentTimeMillis()-t1; / D( A, c) y2 |! d2 b6 y
System.out.println("sum:"+sum+" time:"+t);
1 E o. L8 d$ o: Y8 m( N- @# n } catch (FileNotFoundException e) {
: |( a* u& K8 e // TODO Auto-generated catch block
N5 {: }* D7 `9 U" ~% m8 F e.printStackTrace(); & D& ^" @" h7 d
} catch (IOException e) { ' h: A! A7 K9 l
// TODO Auto-generated catch block
; T) ]# f v6 T* b7 S( Y1 H e.printStackTrace();
# x8 x1 J X7 @' ? } # [/ [7 A0 x; Z$ {1 t% ?, b
2 q$ e) C& X7 @/ M. L } 6 l. K p5 b4 B4 O2 z1 C
6 H1 y2 _$ n/ c4 X& s) G2 }} 7 v+ K; u2 O3 H4 \7 D
测试结果:
. _1 J9 J0 X, G, m, h' Z6 E G- X) `* n4 u" Y+ g o+ F# p
sum:0 time:1458
5 z, G ~6 G8 K+ `- X) L5 ]sum:0 time:67 6 }) k2 v, k) ^' ]
sum:0 time:8 ( Y* h$ j" T. n, ?$ i: |- a
由此可见,将文件部分或者全部映射到内存后进行读写,速度将提高很多。6 T3 e# i) R7 A- x/ u6 a
. K) Y& Z& o! w# Y" {& I
这是因为内存映射文件首先将外存上的文件映射到内存中的一块连续区域,被当成一个字节数组进行处理,读写操作直接对内存进行操作,而后再将内存区域重新映射到外存文件,这就节省了中间频繁的对外存进行读写的时间,大大降低了读写时间。
" N. A' P" C- g1 _& ?. i% [( q" ^. X
# d5 N8 C& Q4 c9 S |
|