使用java修改文件內容
1
package fileopt;
2 import java.io.BufferedReader;
3 import java.io.BufferedWriter;
4 import java.io.FileReader;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.io.RandomAccessFile;
8
9 /**
10 * 修改文件
11 */
12 public class FileModify {
13
14 /**
15 * 讀取文件內容
16 *
17 * @param filePath
18 * @return
19 */
20 public String read(String filePath) {
21 BufferedReader br = null;
22 String line = null;
23 StringBuffer buf = new StringBuffer();
24
25 try {
26 // 根據文件路徑創建緩沖輸入流
27 br = new BufferedReader( new FileReader(filePath));
28
29 // 循環讀取文件的每一行, 對需要修改的行進行修改, 放入緩沖對象中
30 while ((line = br.readLine()) != null) {
31 // 此處根據實際需要修改某些行的內容
32 if (line.startsWith("a")) {
33 buf.append(line).append(" start with a");
34 }
35 else if (line.startsWith("b")) {
36 buf.append(line).append(" start with b");
37 }
38 // 如果不用修改, 則按原來的內容回寫
39 else {
40 buf.append(line);
41 }
42 buf.append(System.getProperty("line.separator"));
43 }
44 } catch (Exception e) {
45 e.printStackTrace();
46 } finally {
47 // 關閉流
48 if (br != null) {
49 try {
50 br.close();
51 } catch (IOException e) {
52 br = null;
53 }
54 }
55 }
56
57 return buf.toString();
58 }
59
60 /**
61 * 將內容回寫到文件中
62 *
63 * @param filePath
64 * @param content
65 */
66 public void write(String filePath, String content) {
67 BufferedWriter bw = null;
68
69 try {
70 // 根據文件路徑創建緩沖輸出流
71 bw = new BufferedWriter( new FileWriter(filePath));
72 // 將內容寫入文件中
73 bw.write(content);
74 } catch (Exception e) {
75 e.printStackTrace();
76 } finally {
77 // 關閉流
78 if (bw != null) {
79 try {
80 bw.close();
81 } catch (IOException e) {
82 bw = null;
83 }
84 }
85 }
86 }
87 public void fileAppender(String fileName,String content) throws IOException{
88
89 BufferedReader reader = new BufferedReader( new FileReader(fileName));
90 String line = null;
91 // 一行一行的讀
92 StringBuilder sb = new StringBuilder();
93 sb.append(content);
94 while ((line = reader.readLine()) != null) {
95 sb.append(line);
96 sb.append("\r\n");
97 }
98 reader.close();
99
100 // 寫回去
101 RandomAccessFile mm = new RandomAccessFile(fileName, "rw");
102 mm.writeBytes(sb.toString());
103 mm.close();
104 }
105
106 /**
107 * 主方法
108 */
109 public static void main(String[] args) {
110 String filePath = FileModify. class.getResource("").getPath()+"test.properties"; // 文件路徑
111 FileModify obj = new FileModify();
112 obj.write(filePath, obj.read(filePath)); // 讀取修改文件
113 try {
114 obj.fileAppender(filePath, "set a=b \n");
115 } catch (IOException e) {
116 e.printStackTrace();
117 }
118 }
119
120 }
2 import java.io.BufferedReader;
3 import java.io.BufferedWriter;
4 import java.io.FileReader;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.io.RandomAccessFile;
8
9 /**
10 * 修改文件
11 */
12 public class FileModify {
13
14 /**
15 * 讀取文件內容
16 *
17 * @param filePath
18 * @return
19 */
20 public String read(String filePath) {
21 BufferedReader br = null;
22 String line = null;
23 StringBuffer buf = new StringBuffer();
24
25 try {
26 // 根據文件路徑創建緩沖輸入流
27 br = new BufferedReader( new FileReader(filePath));
28
29 // 循環讀取文件的每一行, 對需要修改的行進行修改, 放入緩沖對象中
30 while ((line = br.readLine()) != null) {
31 // 此處根據實際需要修改某些行的內容
32 if (line.startsWith("a")) {
33 buf.append(line).append(" start with a");
34 }
35 else if (line.startsWith("b")) {
36 buf.append(line).append(" start with b");
37 }
38 // 如果不用修改, 則按原來的內容回寫
39 else {
40 buf.append(line);
41 }
42 buf.append(System.getProperty("line.separator"));
43 }
44 } catch (Exception e) {
45 e.printStackTrace();
46 } finally {
47 // 關閉流
48 if (br != null) {
49 try {
50 br.close();
51 } catch (IOException e) {
52 br = null;
53 }
54 }
55 }
56
57 return buf.toString();
58 }
59
60 /**
61 * 將內容回寫到文件中
62 *
63 * @param filePath
64 * @param content
65 */
66 public void write(String filePath, String content) {
67 BufferedWriter bw = null;
68
69 try {
70 // 根據文件路徑創建緩沖輸出流
71 bw = new BufferedWriter( new FileWriter(filePath));
72 // 將內容寫入文件中
73 bw.write(content);
74 } catch (Exception e) {
75 e.printStackTrace();
76 } finally {
77 // 關閉流
78 if (bw != null) {
79 try {
80 bw.close();
81 } catch (IOException e) {
82 bw = null;
83 }
84 }
85 }
86 }
87 public void fileAppender(String fileName,String content) throws IOException{
88
89 BufferedReader reader = new BufferedReader( new FileReader(fileName));
90 String line = null;
91 // 一行一行的讀
92 StringBuilder sb = new StringBuilder();
93 sb.append(content);
94 while ((line = reader.readLine()) != null) {
95 sb.append(line);
96 sb.append("\r\n");
97 }
98 reader.close();
99
100 // 寫回去
101 RandomAccessFile mm = new RandomAccessFile(fileName, "rw");
102 mm.writeBytes(sb.toString());
103 mm.close();
104 }
105
106 /**
107 * 主方法
108 */
109 public static void main(String[] args) {
110 String filePath = FileModify. class.getResource("").getPath()+"test.properties"; // 文件路徑
111 FileModify obj = new FileModify();
112 obj.write(filePath, obj.read(filePath)); // 讀取修改文件
113 try {
114 obj.fileAppender(filePath, "set a=b \n");
115 } catch (IOException e) {
116 e.printStackTrace();
117 }
118 }
119
120 }