该用户从未签到
|
- package fileIo;
& ~8 R0 j3 E* W- }0 K8 L) j; t -
; p& m" P! u7 ~5 I - import java.io.BufferedReader; . W$ U8 L6 d( s- a% \& `) Y
- import java.io.BufferedWriter; & H) S$ E& P1 s; M& ^( ~! m
- import java.io.File; : k9 A* b, T* e! |5 x2 D% M; U
- import java.io.FileReader; " i0 p. Z' _) f1 k, a
- import java.io.FileWriter;
( k0 @& U. o3 m; J% c. x8 I) P -
/ G: P. G. m7 @& d! } - public class ReadTextFile { ! v/ {& G5 Z( k
- public BufferedReader bufread;
U5 A6 ?7 Q& O: k - public BufferedWriter bufwriter;
. w$ d+ P2 |, T% T5 Z' ~# X/ \ - File writefile; * W8 k' H Z2 _5 n: S0 x
- String filepath, filecontent, read;
9 Y9 a1 C8 }! | - String readStr = "" ;
' W" P% D# X/ t - // 从文本文件中读取内容 " Q* e3 ?6 {; |8 T
- public String readfile(String path) 5 f3 `2 `+ u, r: C+ m) ^! z
- {
" |9 J$ r; o% E$ y' Q, ], y- k- F - try { # v2 q- V; L1 w9 o
- filepath = path; // 得到文本文件的路径 6 Q4 d% x2 T ^& ]# I
- File file = new File(filepath);
8 g; p) f# k3 |$ e - FileReader fileread = new FileReader(file);
5 P" o" i" l0 a3 ?( e - bufread = new BufferedReader(fileread);
+ z; o4 A6 t, x - while ((read = bufread.readLine()) != null ) { , k4 c! \' _1 X# c4 B: T
- read = read + " /r/n " ; + c6 d$ ?6 G1 ]9 z; s- X
- readStr = readStr + read; 4 [* y* D% Y6 \1 G2 w8 F
- }
9 ]0 u1 K+ \/ _+ U - } catch (Exception d) { 0 S% P S4 |6 }& ^+ J
- System.out.println(d.getMessage()); # `' B! N4 [2 i( B* q7 M; y( y
- }
: o J( C' c$ c- R - return readStr; // 返回从文本文件中读取内容 . b; B3 H" [( S
- }
2 k& ]% s2 E! ?' [3 u -
! J* F" b% |9 G( Z& D \6 g, y - // 向文本文件中写入内容
" |' `" b- C y6 V - public void writefile(String path, String content, boolean append) {
2 d- ~2 h& Y" L. u u - try { . [( h1 ]4 p. N' a, p
- boolean addStr = append; // 通过这个对象来判断是否向文本文件中追加内容 . H# }6 S4 y# j% \) q% h6 @ M: q) V
- filepath = path; // 得到文本文件的路径 : [4 X r, d1 B' q- m6 }7 f
- filecontent = content; // 需要写入的内容
2 \4 }/ J- } e: p7 C7 m" J- J - writefile = new File(filepath);
" J& }/ m' B" ~, ~; E - if (writefile.exists() == false ) // 如果文本文件不存在则创建它 7 j& |( f) O5 C/ D- l
- {
; p/ y( T5 q5 T( n) V1 o4 @ - writefile.createNewFile(); 0 M* E8 W+ c0 a3 h
- writefile = new File(filepath); // 重新实例化
w2 u0 F7 Y$ s p. Q - }
, o" z& W' J6 f; M - FileWriter filewriter = new FileWriter(writefile, addStr);
$ Y4 ~- x. C3 q3 _ w: v! U J& ^6 ] - // 删除原有文件的内容 5 V8 \: t, K. ]+ g% @; y0 }: d, l/ Y
- java.io.RandomAccessFile file = new java.io.RandomAccessFile(path, " rw " ); ( z. O0 [9 x Y3 A% h( m
- file.setLength( 0 ); 6 Z, ^3 ]7 Y$ _0 s/ \' [
- // 写入新的文件内容
1 U0 I! u) b; ? - filewriter.write(filecontent);
5 B" R- t0 W, X! m0 {" i - filewriter.close();
; q& D3 w \2 U% p$ Y$ d - filewriter.flush(); 9 |. ^9 b# f8 \( q, v; j0 W' m& h
- } catch (Exception d) { % J5 g4 T2 r3 r- R6 k
- System.out.println(d.getMessage()); ! r3 O5 q( o2 K5 M$ u& v
- }
& w3 X) F i0 f" `0 D8 X1 }; w$ {8 H - }
2 n& ^+ u4 y0 \2 P8 v* A3 g - ! [1 F+ Y( B7 y) f8 s8 z" e4 w
- public static void main(String[] args) throws Exception { 6 Y# C) W+ q7 _
- ReadTextFile parse = new ReadTextFile(); * ]" m/ o% }, R$ x; n: i5 V# D
- String filecontent = parse.readfile( " c:/applicationContext.xml " );
0 d8 ?$ K. s E0 x - parse.writefile( " c:/applicationContext.xml " ,filecontent, true );
9 H( \! g" z L: D& G/ c -
2 C. o& ^& h# L! I - } / A+ d( a% ~+ u; D0 H3 F- l' _: @
- }
复制代码 9 v! Z; ?& U q0 J3 t- T
+ O3 P1 \5 [, A6 w) [4 x" `0 a
|
|