该用户从未签到
|
- package fileIo;
# e; S7 _- K/ ~& Z; I( A - & F% o: p; s. B) d6 ?8 d
- import java.io.BufferedReader; * D$ [; q! B0 ~0 i, Q6 P7 W
- import java.io.BufferedWriter; # i3 P; k) q4 ~& H* I }
- import java.io.File; . i1 ~+ W$ j5 C V- P' A9 k A
- import java.io.FileReader; ' U5 t2 ], v1 D; B
- import java.io.FileWriter; ' V9 n" t t! |0 |4 e0 Q u7 C
-
+ I1 P6 J, D# H8 A5 u( Q - public class ReadTextFile {
7 F% s+ _ |0 G; A6 W - public BufferedReader bufread; ! c+ X# O' @" @( f& ]3 d& \& x
- public BufferedWriter bufwriter; ( k% F; k* b$ T8 Q4 E* j: E j
- File writefile; # R* { V2 N: w4 w3 C
- String filepath, filecontent, read; 1 ^0 ]' ]0 d# Q: }! t& i; i& w
- String readStr = "" ; 7 ]8 R7 D9 R* ~( Y6 o
- // 从文本文件中读取内容 Z0 P! D' ]1 v0 q4 |# K
- public String readfile(String path) 5 _' b! q! M3 j8 n! j( K
- {
+ r# r9 w" Y1 K. p7 B$ e - try { 3 u1 F0 w! c& Z+ R6 m
- filepath = path; // 得到文本文件的路径 / A0 M8 [! k7 j% F& {
- File file = new File(filepath);
& O+ Z4 q# g* \2 v. K - FileReader fileread = new FileReader(file);
& Q3 q# n l% z2 e/ e) v7 |. L - bufread = new BufferedReader(fileread); * G( f/ z, u& \$ @
- while ((read = bufread.readLine()) != null ) { ; F6 h4 o: T2 @$ V3 [
- read = read + " /r/n " ; ) P V1 p% y7 q/ N- l- ]: I' U' n. E
- readStr = readStr + read;
8 ]3 X9 q3 E5 p1 ?/ @ - }
( k' S0 S/ F; ~% W - } catch (Exception d) { $ ^: G5 y6 h. d9 v! Q4 R
- System.out.println(d.getMessage()); # Z3 h: o6 g/ j, Z& u7 D
- }
$ w# B8 \" E& u$ Q - return readStr; // 返回从文本文件中读取内容
" `4 r9 f- m7 r H2 Y8 a - }
3 O, t+ {2 e5 I5 J2 D- l ] - * z7 t6 ~7 _, ~) z4 f- [3 q
- // 向文本文件中写入内容
) E5 }9 h% _/ z- b* r - public void writefile(String path, String content, boolean append) {
! J" o( J J! u3 s* E! R* j9 r# Y - try { - ?5 D$ k y6 H; H" H' }4 L1 f& e
- boolean addStr = append; // 通过这个对象来判断是否向文本文件中追加内容 5 j2 S; d) u8 w6 m9 Q0 A
- filepath = path; // 得到文本文件的路径 6 O6 M% C" _, `" m$ T' Y
- filecontent = content; // 需要写入的内容
8 A0 ]6 D0 }2 B1 G" n3 r$ y1 r. _/ N - writefile = new File(filepath); ! a% k( e( V# `+ L/ {: }
- if (writefile.exists() == false ) // 如果文本文件不存在则创建它
- i/ _3 Y9 l2 v" g3 g/ _# Y# T - {
/ x+ }, U: X# M - writefile.createNewFile(); % y3 I0 r3 h& a3 _+ O! o
- writefile = new File(filepath); // 重新实例化
+ ]9 K4 ?# I# D. W# A/ H - }
& L M ~; t! H4 L+ }! l: F - FileWriter filewriter = new FileWriter(writefile, addStr);
. i: q) V; Z4 G# Z - // 删除原有文件的内容 * [' \+ g) a( h
- java.io.RandomAccessFile file = new java.io.RandomAccessFile(path, " rw " ); ( u7 i0 R) \- b# F/ ?3 I
- file.setLength( 0 );
, L! K3 u7 w9 g# ~ - // 写入新的文件内容 7 c0 q# {+ e1 \7 [2 J% ?/ R
- filewriter.write(filecontent); 1 ]7 s% v m& h" e! l& u. d
- filewriter.close(); ) a9 E7 h+ D+ m/ g# m6 g
- filewriter.flush();
& f. K8 a3 |% R& ^5 ? - } catch (Exception d) { : W# {0 c1 x$ U& ]+ u# `% o$ C
- System.out.println(d.getMessage());
! }" E% v' Z: h - } 0 Y# E+ k+ I' {
- }
0 K! q0 {0 L! F& B/ x -
0 F, V' m, f8 Q' Y* Z: P - public static void main(String[] args) throws Exception {
$ _' E7 ^5 e0 u: e8 x5 S$ | - ReadTextFile parse = new ReadTextFile(); ( H0 u. i. V! c
- String filecontent = parse.readfile( " c:/applicationContext.xml " );
- F( @- S9 Z% Y+ c - parse.writefile( " c:/applicationContext.xml " ,filecontent, true ); ! u! G. Z3 S4 t# \4 U; m- X' h6 W
- . ^2 n3 ^- o' E" s
- }
6 g) S; U, O3 U8 p3 @ - }
复制代码 8 k9 t* D3 [4 H1 X
6 U" X7 L6 W- i; ~5 i; {2 E
|
|