该用户从未签到
|
- package fileIo;
0 Y, x/ t. X& Z7 ~( x3 X' k$ V2 r -
/ R; v) N- p* G( F3 t3 y - import java.io.BufferedReader;
! r# L W$ Q" ?3 Q0 c0 r# r - import java.io.BufferedWriter; b( u- ?: ^4 N, g, C6 S
- import java.io.File;
% J0 j" p: Y) r9 Y: |2 Z - import java.io.FileReader;
% V) y) a. _1 {9 _8 V - import java.io.FileWriter; # I, Z: @$ |( u/ u
-
2 G9 a7 {4 y5 c1 d% t1 C - public class ReadTextFile {
$ `9 f Q8 Y5 @ - public BufferedReader bufread;
# T% X& z9 g% @: K3 `5 o - public BufferedWriter bufwriter;
& s; X- H' D+ \ - File writefile;
8 E* Y0 w1 z V6 R - String filepath, filecontent, read; . L$ y9 c9 U) c* w
- String readStr = "" ;
4 z) a8 X! J o# q* t1 T) H - // 从文本文件中读取内容
7 x! u8 @0 H+ _" [ - public String readfile(String path)
: ^, f( l! }& l+ ~( `7 X# E: C, p9 W - { * N: m. t/ X) G+ h
- try {
/ H4 j# y- E$ h/ _# z9 Z - filepath = path; // 得到文本文件的路径
3 D) ~. |4 c; k' L& u! V - File file = new File(filepath); . G7 G# D& o$ A5 J( X% H9 T. W
- FileReader fileread = new FileReader(file);
- W9 R* T4 J" w! q% h - bufread = new BufferedReader(fileread);
1 E" ?% {# r% w3 L6 ^! C+ j - while ((read = bufread.readLine()) != null ) { e! T% o7 J# u0 z
- read = read + " /r/n " ;
% u; y& J. B5 ~$ U9 r( r - readStr = readStr + read;
4 N" o. k$ J3 |! d$ s - } ) D5 \8 S$ x5 W
- } catch (Exception d) {
' [! L) d0 ]% y - System.out.println(d.getMessage()); 7 v5 g* `* L) }9 \) Q4 [) h; A
- }
. t/ p3 d. C5 e4 c _) V! z* L - return readStr; // 返回从文本文件中读取内容
9 J3 c' p4 S6 x, G! z - } 5 W1 M$ P; S$ H4 O5 A0 C* G4 j Y
- 9 ?8 ^5 R# k/ z
- // 向文本文件中写入内容 ) b; l0 A9 E8 R5 K6 u
- public void writefile(String path, String content, boolean append) {
4 u: v5 |# X3 d! q, Q - try {
7 v( O, h$ L& P9 G. N) a; C! D* Y$ ? - boolean addStr = append; // 通过这个对象来判断是否向文本文件中追加内容
i, {+ W4 \' | - filepath = path; // 得到文本文件的路径
$ v |- h+ t$ Y4 r! A8 V% q - filecontent = content; // 需要写入的内容 ! r |1 U2 F1 \/ k7 J6 a8 o
- writefile = new File(filepath);
$ l& j5 V4 W: a; H* T9 h- V5 C - if (writefile.exists() == false ) // 如果文本文件不存在则创建它
- _! h; \. u. v, Q* `+ @& D - { + z9 a0 p, K4 u& l
- writefile.createNewFile();
" `8 q6 `4 ^) \ c3 O! I ^ - writefile = new File(filepath); // 重新实例化 * ?$ o X/ p7 p% t$ ]
- }
5 Q" s( z$ t M' O - FileWriter filewriter = new FileWriter(writefile, addStr);
3 U# @# b. _. N# Y9 F" B) V1 x - // 删除原有文件的内容 + d$ i# g) z8 |1 l& K0 V r
- java.io.RandomAccessFile file = new java.io.RandomAccessFile(path, " rw " ); % W; j' q" C- f9 S7 V8 g' m
- file.setLength( 0 ); / t/ B; T; B' V! G( Q" u
- // 写入新的文件内容
- A' Q0 D) }- T M - filewriter.write(filecontent);
5 U: L- H/ l0 h - filewriter.close();
* [: n: _. g8 v4 L - filewriter.flush();
* b) k8 u4 U8 \8 n2 h; [ - } catch (Exception d) { ( ]. }6 k# U5 s, d0 h" h/ J- c
- System.out.println(d.getMessage());
' y+ \; l9 v4 W - }
/ y9 e2 c/ l( P! ^8 @ - } : }1 x& y4 r4 S, w0 h- H
- , K1 B8 Q; L7 b1 @" V. F
- public static void main(String[] args) throws Exception { 5 j6 \3 c% |$ ~$ u1 F6 [( Y0 w
- ReadTextFile parse = new ReadTextFile();
: M4 `1 P- z2 w - String filecontent = parse.readfile( " c:/applicationContext.xml " ); & l; @- \* ]& n
- parse.writefile( " c:/applicationContext.xml " ,filecontent, true );
6 L' ~" u' h" U -
3 H# }$ {6 \. W' @+ Z - }
* ?: w% k, Y0 F& ? - }
复制代码 4 G8 ]$ j) n( j
% A r$ C9 X8 `; s& N" R) Y% _ |
|