该用户从未签到
|
- package fileIo;
0 R" X9 s! B% r8 _5 b -
3 c' ^2 y9 r: V* t - import java.io.BufferedReader;
. p9 E: E2 m( i E4 t) r: n9 { - import java.io.BufferedWriter;
7 m% l* N4 ^2 Q/ j% v8 n T) G - import java.io.File;
/ t9 H8 n7 A' }9 |. V - import java.io.FileReader; ( d- |# j8 \# y: j3 n
- import java.io.FileWriter; : [# C5 A3 y4 b- y4 M8 h
-
8 n4 O9 |) V1 {+ j% L) [2 i - public class ReadTextFile {
( T4 X/ A5 k$ M# ^ \; Q5 b3 h - public BufferedReader bufread; # m$ i' j! Y+ `2 f& q' ^
- public BufferedWriter bufwriter;
9 p: Z3 [, x4 J6 V" u - File writefile;
& b* y! ?! _" Y7 t. A - String filepath, filecontent, read; ' o9 L5 X2 j) p- t, q! e
- String readStr = "" ;
6 V6 X( {4 @2 Z" `3 G - // 从文本文件中读取内容 & I6 J, |# N) l; k0 w' z% H" _5 |
- public String readfile(String path)
6 k7 W" U, \" B; K* L; a9 w' M - { ' u+ a$ y' G# k& _ N
- try {
5 b: |7 `/ W1 U5 A - filepath = path; // 得到文本文件的路径 ( s9 u: n: j s+ Q- |$ b
- File file = new File(filepath);
8 c4 w# t! T& r- T" r- d& y; y - FileReader fileread = new FileReader(file);
* b6 u P3 O5 |+ B" y - bufread = new BufferedReader(fileread); ; O w7 {& C7 M% L O
- while ((read = bufread.readLine()) != null ) { 9 P, I. l& {! {- z2 I
- read = read + " /r/n " ;
( |0 A" w9 x: [ - readStr = readStr + read; $ A/ p# `& j" ^7 {) W4 O
- }
3 A8 K* ^" }' C - } catch (Exception d) {
/ t, d0 Z( V- b' g - System.out.println(d.getMessage()); ' r y2 q% l. e
- } 8 v3 m7 V* d* q8 k( z, m/ {# Z
- return readStr; // 返回从文本文件中读取内容
7 |8 W5 K: B6 w* O) W4 m - }
. I; \. L, e. F# O7 I4 w -
) p: K& B7 r' E5 A' h" T - // 向文本文件中写入内容
( u4 l5 Y* r# j% C" y0 g - public void writefile(String path, String content, boolean append) {
' f: h- d: O+ {7 q' V - try { ( C4 Z1 \) A/ c
- boolean addStr = append; // 通过这个对象来判断是否向文本文件中追加内容
9 S; E" F; q. R L! } H - filepath = path; // 得到文本文件的路径
( ~' N: }5 N' V+ V7 N& ^6 l9 T/ C/ G - filecontent = content; // 需要写入的内容
! y6 M3 l4 M+ x; i; E# U8 w - writefile = new File(filepath);
# {4 M' A* U, B4 d/ W4 n0 n - if (writefile.exists() == false ) // 如果文本文件不存在则创建它 , w, L- @5 \* K+ k" a' d
- { 3 L7 F. {8 F9 F. Z, P5 u
- writefile.createNewFile(); 5 N- M, L C$ y4 L) {: x8 F: x
- writefile = new File(filepath); // 重新实例化 4 a' i! E4 R+ Z `, T. T% }
- }
* b# y! v9 h5 T U/ ~: `- a - FileWriter filewriter = new FileWriter(writefile, addStr);
% t' b3 J) d* z! Z - // 删除原有文件的内容
+ R+ O9 [2 F$ g2 M - java.io.RandomAccessFile file = new java.io.RandomAccessFile(path, " rw " ); & w5 v4 j9 a. y5 B+ b/ v
- file.setLength( 0 );
) a9 {# _' a. C: X- o - // 写入新的文件内容 7 N: q& e$ C- x! r
- filewriter.write(filecontent);
) u+ b: f" c1 Z, j6 |! ? - filewriter.close();
$ n; j& m5 }: J; B - filewriter.flush(); 0 |# m& O; Y2 {9 }3 X
- } catch (Exception d) { & {4 n, v2 S/ A/ f
- System.out.println(d.getMessage());
& [: |7 ~9 l' X - } / H8 O3 Y# R! d( A
- } $ D! t* [: G, F, k0 d
- . F$ J6 y& T' d- }" m2 w4 o
- public static void main(String[] args) throws Exception {
6 {* r& {/ B! j" r# S - ReadTextFile parse = new ReadTextFile();
, X: L3 L1 G& V6 D4 E7 T7 I8 Y; F - String filecontent = parse.readfile( " c:/applicationContext.xml " ); & V% `2 S l4 F7 }
- parse.writefile( " c:/applicationContext.xml " ,filecontent, true );
% p- J x# j9 ^6 s -
; s; Q: o. M; K& h* ^" x, x - }
- {. w5 h. I+ d - }
复制代码
9 w7 h: o% h2 v# Z. b8 r
* \& S! I/ T: I. V6 ~" }- V: I% \, A |
|