|
该用户从未签到
|
- package fileIo;
6 T0 P" r e$ _% F1 ~- G- N9 S g - 7 s: q1 Q1 j, w# a
- import java.io.BufferedReader; 6 B3 M$ M! E" K
- import java.io.BufferedWriter; ! t# d3 n5 K# [5 }
- import java.io.File;
$ a2 I( Q4 g, x( F. `7 ` - import java.io.FileReader;
: X& P$ u$ x. z7 z' i - import java.io.FileWriter; 1 K2 c* q4 m# `- O! G" ^( {! [
- 7 e3 c- c! x {+ H$ U
- public class ReadTextFile { $ J* ~/ c% q+ ] C2 z
- public BufferedReader bufread; , h2 A9 h/ Q- e g3 d, y6 L
- public BufferedWriter bufwriter; . z& Z( j1 t9 `9 _2 r, D3 ?
- File writefile;
, q/ Z+ e r1 y/ R- V - String filepath, filecontent, read;
7 P, y' p' E" Y. W& s - String readStr = "" ; ; ? b( p t' t6 O
- // 从文本文件中读取内容 ) W9 d' F' @' i7 ^
- public String readfile(String path) 4 m4 R; n: w/ z6 a/ y* F
- {
0 Y2 j. k) h3 l/ P4 c) \7 K( \ - try { % ]0 s- {' j# a/ A
- filepath = path; // 得到文本文件的路径 6 U E3 J% C. h; b1 T1 e
- File file = new File(filepath); 2 }* ~( V# C \% E/ R, f
- FileReader fileread = new FileReader(file); P5 B" a( t& R5 |4 d. i/ b2 [
- bufread = new BufferedReader(fileread); - @2 F- c6 N' n# y0 R! }' m
- while ((read = bufread.readLine()) != null ) {
( a6 M- _" ~8 E w! g3 [ - read = read + " /r/n " ;
( m0 T9 V; J* @, g/ J# l+ y1 P - readStr = readStr + read; ' {! } n, W& p/ O, f
- } ! e% n, g% k1 v' b
- } catch (Exception d) {
9 X% J' A' {" j" q/ p6 R - System.out.println(d.getMessage());
+ P4 U. I2 {0 M. F, L: y - }
4 f8 W$ o2 U h2 T- w$ p: f$ Q6 K - return readStr; // 返回从文本文件中读取内容 ( D. r" M* p" U% Y
- }
; m& @# u/ P6 ? -
: W; j8 X: @% T5 K$ y2 g7 U6 g - // 向文本文件中写入内容
k# \! F" D4 W8 i. N! R2 v - public void writefile(String path, String content, boolean append) {
0 h3 b9 S r y - try { + k% w8 w; y" a. ~( o
- boolean addStr = append; // 通过这个对象来判断是否向文本文件中追加内容 & F4 `0 r/ L5 i8 E; J0 a% u
- filepath = path; // 得到文本文件的路径 ' H" ?1 g. W$ m' m4 q) K
- filecontent = content; // 需要写入的内容
5 D8 w2 p. L2 i o0 {0 \ - writefile = new File(filepath); ( f; b6 c3 ~% ?. t+ c
- if (writefile.exists() == false ) // 如果文本文件不存在则创建它
# |2 ^% M3 w+ H1 G. s - {
g3 N0 ^: U: s; w - writefile.createNewFile(); + `) B( z1 ~! \8 s8 h9 c7 k
- writefile = new File(filepath); // 重新实例化 5 w$ c% r: X3 o& Z' ? i$ }) X% r
- }
5 L" L) @+ @' y - FileWriter filewriter = new FileWriter(writefile, addStr);
& E& I+ z8 Z7 z0 T2 w3 y6 b6 F - // 删除原有文件的内容 7 Q O, {" n0 C" c1 E5 [" F
- java.io.RandomAccessFile file = new java.io.RandomAccessFile(path, " rw " ); 8 \: I0 V% M! b3 a, T r: q
- file.setLength( 0 ); 4 r- A! V2 ]8 e
- // 写入新的文件内容 ; \4 x% `% e5 u5 S; h: C1 L
- filewriter.write(filecontent); F# @9 @9 A! N' e+ q0 C |" S
- filewriter.close(); $ D: {+ a7 t3 s4 X: Y1 Q
- filewriter.flush(); y! U) w5 x; T, x& U& i2 }( D' O
- } catch (Exception d) { - Y; ]4 M. _& F+ G1 z
- System.out.println(d.getMessage());
+ n2 A6 \# t! d - } 4 ?% ?1 R9 n4 W8 L6 j$ _
- } $ `; I" N( a5 N/ L0 |/ Q
- / D+ }9 {6 l+ T0 ^6 ^/ N
- public static void main(String[] args) throws Exception {
, S3 I% |% P# O* O - ReadTextFile parse = new ReadTextFile();
% [' d) K/ `# {0 `! d- X: f F+ T - String filecontent = parse.readfile( " c:/applicationContext.xml " ); + e6 G* R) }2 ] |/ f
- parse.writefile( " c:/applicationContext.xml " ,filecontent, true );
0 Y/ ~9 u! g9 |% b1 k# y( S, N -
) F# J2 q# Q& Q2 W E+ D/ ` - } - K- _& v+ `% D9 H
- }
复制代码 . V0 \, A( y0 I' I# ~: ^
( i3 \! D' V! v( }: z$ F
|
|