该用户从未签到
|
- package fileIo; ; A7 j7 x7 W& |9 {! b
-
" ^: K& {, y1 f7 T( Q/ y$ D. v8 q - import java.io.BufferedReader;
, v0 J' K! w" v - import java.io.BufferedWriter; 1 g/ f/ _/ j. p+ f/ d7 f9 S |
- import java.io.File; 6 y% Y% ]3 t* v4 g
- import java.io.FileReader;
" `# @) I! T' t* F: \2 m - import java.io.FileWriter; 0 _( w) l3 i+ |# p# u# V! y$ u
- + x, K$ \* d$ F' J5 C! U
- public class ReadTextFile {
3 p( I6 A6 }! V1 v - public BufferedReader bufread; 8 u$ Q. p) G( n4 b
- public BufferedWriter bufwriter;
: Z1 l4 Q- `' l6 d - File writefile; 6 L5 z/ j @) E4 ?: D, M- H/ \
- String filepath, filecontent, read; ( B' g) h( i# M# N
- String readStr = "" ;
) A# v a1 N2 C8 \ - // 从文本文件中读取内容 . j" B& w2 `. R1 ~% e
- public String readfile(String path) % A+ j1 O8 r* E. s
- { & s" t; _$ K! B! i i3 f
- try {
$ {2 ^6 Y0 \+ Y - filepath = path; // 得到文本文件的路径 ) p+ s( l( @% d+ U X
- File file = new File(filepath); % ~2 S/ _9 Z% V
- FileReader fileread = new FileReader(file); " ?( ^" Y, r9 S8 K, }- A& e6 ^
- bufread = new BufferedReader(fileread); 3 }( [9 b6 |2 b) N9 ~2 x
- while ((read = bufread.readLine()) != null ) {
: L6 _% k5 o3 I; Q# q - read = read + " /r/n " ; : M% E9 N" O8 ~1 K0 c
- readStr = readStr + read; Z3 j1 v5 w8 O$ C G& W
- } ! g7 M1 f0 x7 G7 O) X+ |1 Q
- } catch (Exception d) { 5 ^0 Z5 J6 t/ X% m# ^. n
- System.out.println(d.getMessage());
' B( l! x7 q6 Q$ m0 U - } : n& c" e" }( o: J2 l$ t
- return readStr; // 返回从文本文件中读取内容 ( h) l g, g/ b4 i2 L
- }
7 Q% R2 t) X9 H0 W% W6 V6 W) m - 6 d3 I" @3 f/ { r# e
- // 向文本文件中写入内容 , {7 y0 R1 R6 w/ v1 Z( n' r
- public void writefile(String path, String content, boolean append) { # }& [0 j3 A& {4 {
- try {
9 \) C' m& {/ z/ R J" D - boolean addStr = append; // 通过这个对象来判断是否向文本文件中追加内容
* j$ y0 r: W% }/ V/ u - filepath = path; // 得到文本文件的路径
4 ?3 m) A. j. z, v& B9 A4 I: k% V4 P - filecontent = content; // 需要写入的内容
6 p0 o3 L; i! X1 N+ f - writefile = new File(filepath); $ n4 f# p4 S, F7 |0 p* `1 V( s
- if (writefile.exists() == false ) // 如果文本文件不存在则创建它 $ L8 f& @+ s2 @
- {
; ?: d, B- H0 S - writefile.createNewFile();
! Q1 ]$ W9 D/ w0 _0 o - writefile = new File(filepath); // 重新实例化 1 n: J' |3 t- U& @3 X
- } $ \- j7 e! u) r
- FileWriter filewriter = new FileWriter(writefile, addStr); 3 I7 ^0 I! G( n( l5 T
- // 删除原有文件的内容
: V: l, s4 b) V* {' \4 \7 j - java.io.RandomAccessFile file = new java.io.RandomAccessFile(path, " rw " ); 2 o$ {3 X0 f' z5 L7 T* |
- file.setLength( 0 ); ! h( E5 i6 q" O$ i: R/ S8 Y/ a
- // 写入新的文件内容 ' \4 Y0 j5 V: B6 l5 o
- filewriter.write(filecontent); + V' c; u: D9 Z6 i& P a
- filewriter.close();
$ m2 D" z) Q' D9 |4 ~7 `+ I - filewriter.flush(); * Z2 e8 [2 f& l2 [
- } catch (Exception d) {
. Y) J1 g+ ~7 i, {6 s - System.out.println(d.getMessage()); ' [. A% B; L$ Z* x8 w3 E. [6 s
- } 2 V9 o" ^" W* U3 J6 K# N
- } 1 `2 O+ X7 Y3 z4 e% q
- , r$ N. P2 _1 o7 T- o: \
- public static void main(String[] args) throws Exception {
, ^, G5 A: ]$ G- ~- o - ReadTextFile parse = new ReadTextFile(); & @# O; R& j1 ?4 c0 ~7 A
- String filecontent = parse.readfile( " c:/applicationContext.xml " );
+ h2 r0 F) e/ N5 A4 u$ [5 B - parse.writefile( " c:/applicationContext.xml " ,filecontent, true );
8 R) d' l5 J" Z& j( R1 L* S" L - * H6 a O( G+ H- m6 J
- }
! q$ d: f, E; k - }
复制代码
8 o/ F9 x, s" j9 V
$ s" f, ^) ]6 n# ~: W) ]: O) ?/ f |
|