该用户从未签到
|
- package fileIo;
9 O' b% L H) ?; I+ J4 T' U& a9 L - 3 A4 k7 f3 W/ p. m* j2 I' t
- import java.io.BufferedReader; & ]9 R- C! _5 c: h) q
- import java.io.BufferedWriter; 6 R5 s, k5 M" ^) K$ h6 J( A( E
- import java.io.File; & }6 c, W3 l2 Q
- import java.io.FileReader; ' c- E. f% d x6 M: f. N* A& G
- import java.io.FileWriter; . K2 j& j3 B& B+ e! R/ h3 e
-
! @! N+ c# k; w - public class ReadTextFile {
/ W' P# ~, g3 Y+ u( M - public BufferedReader bufread;
& M; d$ C' n- E' y. Q+ s - public BufferedWriter bufwriter; + w0 w) x' e& n+ r; v
- File writefile;
7 ]8 R7 }# e2 [ - String filepath, filecontent, read;
) ^, G' E6 v0 P+ z - String readStr = "" ; ; g$ t: e5 q& Q/ c, @7 D
- // 从文本文件中读取内容 . D: ^; u' m# L+ J
- public String readfile(String path) . D$ _4 a) p+ ^2 ^7 s
- { . B D% H0 r2 A# r
- try { + B( P& T9 P3 z$ z* V: v! @8 ~" Q
- filepath = path; // 得到文本文件的路径
" L/ M+ |( h% J& e2 X4 L - File file = new File(filepath); 4 ]) s) B: w A, y, P( Q6 {/ E4 ?
- FileReader fileread = new FileReader(file); 1 h Q0 f# t8 ]4 k% g
- bufread = new BufferedReader(fileread); 4 }, ^, o; x* f3 W
- while ((read = bufread.readLine()) != null ) {
' O" {" O) m) p+ { - read = read + " /r/n " ;
6 ^# E' x$ r. @6 K- i$ W - readStr = readStr + read;
" _, ~. z6 d h9 F* B3 h2 P4 t - } h7 H L# T+ b4 F9 s3 M
- } catch (Exception d) { . {4 ]% n5 t6 b" i! o
- System.out.println(d.getMessage()); ! f: d+ X* u: Q" h1 b a4 ^
- }
3 F' p/ g! ]* K! U - return readStr; // 返回从文本文件中读取内容 . q( e# k+ D9 |7 g/ O
- }
' _3 p6 H0 @6 f$ n; w# l+ W -
5 V* W2 z/ h( k( T3 X# |; U& k - // 向文本文件中写入内容
8 M+ ^7 m0 n! \% T$ @( J* { - public void writefile(String path, String content, boolean append) {
, o! ]1 C3 ^8 a+ v9 ~* a) _/ } - try { ( L7 L L6 X: H' V, w
- boolean addStr = append; // 通过这个对象来判断是否向文本文件中追加内容
8 D" P) _9 `9 {- v5 x - filepath = path; // 得到文本文件的路径
( J8 L; a0 v. [2 B' } - filecontent = content; // 需要写入的内容 % c U" u* P" ^* r% S" m7 j2 d
- writefile = new File(filepath);
' E/ S4 F, g+ v0 G" Y - if (writefile.exists() == false ) // 如果文本文件不存在则创建它
7 Y! s2 a' x3 Y' ] - { 5 b/ M3 l8 d3 e3 k4 Q3 ~
- writefile.createNewFile();
. t$ C' m8 @/ ~7 m( r5 W - writefile = new File(filepath); // 重新实例化
; D7 I. a$ s* [5 l- f& J - } / V+ I% e9 L/ I3 T F" m# }- t! n
- FileWriter filewriter = new FileWriter(writefile, addStr); ) [( V. z& I* h% V
- // 删除原有文件的内容
, U( G7 F7 X& v% b% l9 {7 ~. z Q+ K - java.io.RandomAccessFile file = new java.io.RandomAccessFile(path, " rw " ); 1 y+ W; {# j4 L
- file.setLength( 0 );
: W% D; U, Q# V% p. P9 s( | - // 写入新的文件内容 7 t7 Z2 N( h: c; {4 w$ Z" a
- filewriter.write(filecontent); ( T7 W' U- U# H; ]1 x
- filewriter.close();
: z) R% |3 e4 O/ l. m# }0 p - filewriter.flush(); ) S1 u3 T9 l' V2 r: R5 ^
- } catch (Exception d) {
2 |" ?+ i( T" `' p+ E$ R - System.out.println(d.getMessage());
# F. N% y& t. [+ z! E0 k+ J - }
, I/ o& E% Z. @# C1 J# M; L - }
* V8 o. D6 i2 s, {6 J1 K8 I -
7 Y) \3 R4 v0 ]2 J - public static void main(String[] args) throws Exception { . n g8 u X4 F Z' P
- ReadTextFile parse = new ReadTextFile(); : ^2 V: g) t3 ~; k
- String filecontent = parse.readfile( " c:/applicationContext.xml " );
! u$ P) }8 x& G; o5 q9 b# h0 ~ - parse.writefile( " c:/applicationContext.xml " ,filecontent, true ); $ J+ O$ Q7 ~6 f# S3 K6 l7 v2 H
- " A) k5 n" [7 p% @7 R- p9 ^# e
- } 9 J) |' ]7 [5 s4 a2 v- u$ t6 M
- }
复制代码
" Y3 f6 s) B: I* |$ \/ ^' v
9 B# J3 \" f/ \ |
|