该用户从未签到
|
- package fileIo; 4 y; h4 J0 X- Y
- ) U( C# u, F# p( p7 Q- N- V& r
- import java.io.BufferedReader; : O' l$ |) W- O* L8 W
- import java.io.BufferedWriter;
" J: x# F2 w4 C+ U - import java.io.File;
! ^4 Q% Y0 G- [3 c) o" l2 x - import java.io.FileReader; 0 F, z$ ]8 [! F+ g0 \8 \2 U
- import java.io.FileWriter;
0 U/ k! a! t( D/ ^9 ~9 Q - 3 l& B& T5 V/ L5 ]3 D" t6 A6 l: [
- public class ReadTextFile {
: S/ n7 t4 n i - public BufferedReader bufread;
0 f" ?, N1 ^$ v0 P - public BufferedWriter bufwriter; : N( H; _; Q" r
- File writefile; 6 |. p! O) C- Z: G# K
- String filepath, filecontent, read; Q' `9 r: P& k% e, z/ W7 x1 p; z2 ?
- String readStr = "" ;
2 m1 G' x" w* A$ `7 L1 _) e8 y - // 从文本文件中读取内容 9 ^1 P/ J. r5 B
- public String readfile(String path)
; V# q T, _, e I - { ' h; U& Y* q9 t5 z9 ]1 p
- try { , E4 F: E; E: s) k# t5 }
- filepath = path; // 得到文本文件的路径 4 O( L3 Y# p$ Q& b1 ]( \" B
- File file = new File(filepath);
0 K+ U+ i+ g3 Y7 I: @ - FileReader fileread = new FileReader(file); ' M: e% H1 V+ `
- bufread = new BufferedReader(fileread);
7 @! {& ^5 [3 q/ ]6 A - while ((read = bufread.readLine()) != null ) {
4 ~" D: t% X a C: c/ m - read = read + " /r/n " ; # l9 G/ t% Y( ~' E5 B
- readStr = readStr + read; ! T3 l% C9 u% m' t* o3 W& W' V" z! d
- }
* J/ S. v) p# ~! g - } catch (Exception d) {
9 l5 d/ d) {' C* Q4 a$ Y6 K9 I5 y1 r7 t - System.out.println(d.getMessage()); 2 H9 m8 m2 ?6 B0 b8 P
- }
7 |# ]1 Q2 H1 V8 U# P8 C7 u3 _' Y B - return readStr; // 返回从文本文件中读取内容 . U0 u) Q9 K( {4 m" K7 R
- } + a1 T) f" \7 D: R- ~' C6 U+ t
- & W! s7 U1 ?' Z. }9 h! e5 f* W7 i" j
- // 向文本文件中写入内容 - c+ M' X3 g2 t8 P4 l" N1 Z' j
- public void writefile(String path, String content, boolean append) { 7 Q2 z: I$ G- W% j, L& j
- try {
' s3 Y' {$ ^* a/ y- x - boolean addStr = append; // 通过这个对象来判断是否向文本文件中追加内容 & _/ {2 A7 y- d% S: G
- filepath = path; // 得到文本文件的路径 2 B0 e( e7 d# A2 r; u
- filecontent = content; // 需要写入的内容
5 ^- A& K' F5 F - writefile = new File(filepath);
, K) z- N; q& m% | - if (writefile.exists() == false ) // 如果文本文件不存在则创建它
" T7 t. M4 H) |9 m0 m N, } - { / N9 D0 v% Q- ~: |
- writefile.createNewFile(); $ F2 J/ Y$ ]% I6 w6 n
- writefile = new File(filepath); // 重新实例化
3 P( N: p9 K' T% Q" W - }
( h& T0 H2 z/ D: ]( z+ T$ t - FileWriter filewriter = new FileWriter(writefile, addStr);
$ U: @( S2 k1 J. \ q$ A C! l. o/ ` - // 删除原有文件的内容 # l7 Z+ @* x8 h
- java.io.RandomAccessFile file = new java.io.RandomAccessFile(path, " rw " );
2 g& v% J2 Y! y+ C - file.setLength( 0 ); 5 C7 D! x0 W- Z/ j' G- @
- // 写入新的文件内容
; X# ^( w4 P! ]- ]7 w0 B - filewriter.write(filecontent);
! G" L' q4 R) X, n$ Y8 t+ } - filewriter.close();
- I* V- R0 [' m, a& K - filewriter.flush();
+ ]1 J+ f) e; h" A0 C - } catch (Exception d) { 4 {9 ]; M/ ?4 U* A
- System.out.println(d.getMessage()); 8 h9 V/ N8 D3 j) d
- } , R0 U/ S Z/ |# N; Q' o @' w
- }
* Z& ~5 I3 Y* G7 b - % f* d7 _7 u- @
- public static void main(String[] args) throws Exception {
* k1 H- v) Z9 }: d2 P: O; K - ReadTextFile parse = new ReadTextFile(); / r4 H1 I+ Z! I$ x" t6 @
- String filecontent = parse.readfile( " c:/applicationContext.xml " );
/ c+ \8 a& Z8 [$ f# w - parse.writefile( " c:/applicationContext.xml " ,filecontent, true );
3 E3 R" ?% n$ B0 \& c, Q2 r - % e! o+ a; M, s- q7 o( J! y2 X
- } - j: ?, w+ H" e
- }
复制代码 1 M/ Y! N# M( Q
. K% Z, N# f" L6 K) U' ^
|
|