java读写删.text,.xml文件内容
1 package com.paiao.bdpm.flow.base;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.File;
6 import java.io.FileReader;
7 import java.io.FileWriter;
8
9 public class ReadTextFile {
10 public BufferedReader bufread;
11 public BufferedWriter bufwriter;
12 File writefile;
13 String filepath, filecontent, read;
14 String readStr = "" ;
15 // 从文本文件中读取内容
16 public String readfile(String path)
17 {
18 try {
19 filepath = path; // 得到文本文件的路径
20 File file = new File(filepath);
21 FileReader fileread = new FileReader(file);
22 bufread = new BufferedReader(fileread);
23 while ((read = bufread.readLine()) != null ) {
24 read = read + " \r\n " ;
25 readStr = readStr + read;
26 }
27 } catch (Exception d) {
28 System.out.println(d.getMessage());
29 }
30 return readStr; // 返回从文本文件中读取内容
31 }
32
33 // 向文本文件中写入内容
34 public void writefile(String path, String content, boolean append) {
35 try {
36 boolean addStr = append; // 通过这个对象来判断是否向文本文件中追加内容
37 filepath = path; // 得到文本文件的路径
38 filecontent = content; // 需要写入的内容
39 writefile = new File(filepath);
40 if (writefile.exists() == false ) // 如果文本文件不存在则创建它
41 {
42 writefile.createNewFile();
43 writefile = new File(filepath); // 重新实例化
44 }
45 FileWriter filewriter = new FileWriter(writefile, addStr);
46 // 删除原有文件的内容
47 java.io.RandomAccessFile file = new java.io.RandomAccessFile(path, " rw " );
48 file.setLength( 0 );
49 // 写入新的文件内容
50 filewriter.write(filecontent);
51 filewriter.close();
52 filewriter.flush();
53 } catch (Exception d) {
54 System.out.println(d.getMessage());
55 }
56 }
57
58 public static void main(String[] args) throws Exception {
59 ReadTextFile parse = new ReadTextFile();
60 String filecontent = parse.readfile( " c:/applicationContext.xml " );
61 parse.writefile( " c:/applicationContext.xml " ,filecontent, true );
62
63 }
64 }
1 package com.paiao.bdpm.flow.base;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.File;
6 import java.io.FileReader;
7 import java.io.FileWriter;
8
9 public class ReadTextFile {
10 public BufferedReader bufread;
11 public BufferedWriter bufwriter;
12 File writefile;
13 String filepath, filecontent, read;
14 String readStr = "" ;
15 // 从文本文件中读取内容
16 public String readfile(String path)
17 {
18 try {
19 filepath = path; // 得到文本文件的路径
20 File file = new File(filepath);
21 FileReader fileread = new FileReader(file);
22 bufread = new BufferedReader(fileread);
23 while ((read = bufread.readLine()) != null ) {
24 read = read + " \r\n " ;
25 readStr = readStr + read;
26 }
27 } catch (Exception d) {
28 System.out.println(d.getMessage());
29 }
30 return readStr; // 返回从文本文件中读取内容
31 }
32
33 // 向文本文件中写入内容
34 public void writefile(String path, String content, boolean append) {
35 try {
36 boolean addStr = append; // 通过这个对象来判断是否向文本文件中追加内容
37 filepath = path; // 得到文本文件的路径
38 filecontent = content; // 需要写入的内容
39 writefile = new File(filepath);
40 if (writefile.exists() == false ) // 如果文本文件不存在则创建它
41 {
42 writefile.createNewFile();
43 writefile = new File(filepath); // 重新实例化
44 }
45 FileWriter filewriter = new FileWriter(writefile, addStr);
46 // 删除原有文件的内容
47 java.io.RandomAccessFile file = new java.io.RandomAccessFile(path, " rw " );
48 file.setLength( 0 );
49 // 写入新的文件内容
50 filewriter.write(filecontent);
51 filewriter.close();
52 filewriter.flush();
53 } catch (Exception d) {
54 System.out.println(d.getMessage());
55 }
56 }
57
58 public static void main(String[] args) throws Exception {
59 ReadTextFile parse = new ReadTextFile();
60 String filecontent = parse.readfile( " c:/applicationContext.xml " );
61 parse.writefile( " c:/applicationContext.xml " ,filecontent, true );
62
63 }
64 }