该用户从未签到
|
- package fileIo; 2 O/ G3 H# v- g( Z
- & z1 A3 I" G, d
- import java.io.BufferedReader;
4 o5 {0 t9 |3 g8 E% w9 i1 u. F4 l - import java.io.BufferedWriter; + R, w! _0 ], [) n
- import java.io.File;
% A% C: S; x- l# C( T$ N; j - import java.io.FileReader; , T& }' R: u& `2 D' }
- import java.io.FileWriter;
. `. t2 Z: r, n4 M! U - # i$ J- W L+ n$ E
- public class ReadTextFile { - a4 ~0 ]/ D v4 K2 u
- public BufferedReader bufread; 2 l2 V( y; r2 m- @+ G
- public BufferedWriter bufwriter;
. s" ^7 ]+ q) e - File writefile; , G3 ^1 N3 i8 [3 X. b
- String filepath, filecontent, read; 3 d; I+ m6 f' H# a3 U0 t, I
- String readStr = "" ;
6 @. o* r" t0 \- O; d% K - // 从文本文件中读取内容
0 F* r* h+ a3 C6 R - public String readfile(String path) 4 [( O2 M; S! J: z' C) h
- {
- u7 J) ~& c; T0 L7 X% z( W o1 q/ y - try { # A, v/ C7 s7 Q6 _
- filepath = path; // 得到文本文件的路径 " T* E9 R. L& V" k9 f9 L- U% L; g4 `
- File file = new File(filepath);
! C9 x3 u. w! L4 v1 b0 i - FileReader fileread = new FileReader(file); 1 r6 W8 ?- p7 T+ H% x$ v$ H
- bufread = new BufferedReader(fileread);
( P; X) n }0 x/ p" s - while ((read = bufread.readLine()) != null ) {
$ r. s/ _) `1 `# p) O# L8 X( D9 w; H - read = read + " /r/n " ; 4 G- q. G3 _! S9 v
- readStr = readStr + read; + L8 R. s% q3 ?# q, r
- } * K& ~8 E' Z, w
- } catch (Exception d) { / W4 X! u, q) Y# e1 z
- System.out.println(d.getMessage()); 8 f' I4 v# s6 t+ G) q( r
- } ) R( L+ e5 L0 w" l2 L7 t8 q& M
- return readStr; // 返回从文本文件中读取内容 2 n, P# \2 \/ c; b2 _" r8 b
- }
8 i6 e: k/ f+ z4 v4 f - ; z: v7 W0 D4 X; c9 o! x( O* z" A& v
- // 向文本文件中写入内容
) d7 M, D5 [7 r- K6 _6 v - public void writefile(String path, String content, boolean append) {
9 M0 j; E' m- w2 p: z3 h - try { 6 t, B2 W. c: }2 R# ]4 K V8 d
- boolean addStr = append; // 通过这个对象来判断是否向文本文件中追加内容
; p! q Z P# K0 V6 r' I+ P8 ] - filepath = path; // 得到文本文件的路径 4 E0 P1 D+ C$ |* s7 @
- filecontent = content; // 需要写入的内容
# ?8 _2 r, H% K8 J' F - writefile = new File(filepath); + `# X" R2 I9 e- E! _0 {1 c
- if (writefile.exists() == false ) // 如果文本文件不存在则创建它 9 ^) B2 J0 _" _+ w. c" G
- {
: s, L" m9 n# o: @ L - writefile.createNewFile();
) s o* n# p- I+ O - writefile = new File(filepath); // 重新实例化
) `: ~ f0 p3 U8 K! E% k - } ( t0 i9 F$ Z( r% L# ~4 u
- FileWriter filewriter = new FileWriter(writefile, addStr); ! x6 [3 c: t Q
- // 删除原有文件的内容 ; q6 M( }4 z$ F( @% b, q3 G0 J3 w1 j& t
- java.io.RandomAccessFile file = new java.io.RandomAccessFile(path, " rw " ); 9 Y5 w9 e9 _- p! W
- file.setLength( 0 );
: i8 s, z9 |: c9 W0 y; r - // 写入新的文件内容 N# `) F( Y! A/ q- K- X* o; A& d
- filewriter.write(filecontent); 4 s1 p4 @: I! D3 q
- filewriter.close();
* J% s# K: j1 s/ g5 h6 d6 |+ w - filewriter.flush();
2 L$ R" D0 p6 G2 P- x% e$ H - } catch (Exception d) {
* k; ]. R' {/ ]& `7 _ - System.out.println(d.getMessage()); ) p e$ e7 @) m
- }
! b; Q) C; r$ k8 T - } ! Q1 D" I$ A& v+ g+ M8 i
-
. L% H# Q+ P1 s& R* @0 l3 n9 p: p - public static void main(String[] args) throws Exception { ; t" E8 C: ]$ Y. ~- }
- ReadTextFile parse = new ReadTextFile();
9 \. k( y# C# t - String filecontent = parse.readfile( " c:/applicationContext.xml " );
1 S2 W7 T6 J3 x8 H( P, f; C - parse.writefile( " c:/applicationContext.xml " ,filecontent, true ); # {. o- E+ c0 I' s
- 4 {1 r$ y0 l7 @3 u; k S: m. J
- }
: i/ z; }5 M& {1 t# X/ _ - }
复制代码 3 m7 o! P& e2 x6 D5 B, N
0 k2 `" ?8 K3 @* \3 }
|
|