1、将文件内容一行一行的读出来
2、在每读一行的时候,判断是否以a或b开始,如果是则进行处理,然后写到缓冲对象。如果不是则直接写入缓冲对象中
3、将缓冲对象中的内容回写到文件中
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- /**
- * 修改文件
- */
- public class FileModify {
- /**
- * 读取文件内容
- *
- * @param filePath
- * @return
- */
- public String read(String filePath) {
- BufferedReader br = null;
- String line = null;
- StringBuffer buf = new StringBuffer();
- try {
- // 根据文件路径创建缓冲输入流
- br = new BufferedReader(new FileReader(filePath));
- // 循环读取文件的每一行, 对需要修改的行进行修改, 放入缓冲对象中
- while ((line = br.readLine()) != null) {
- // 此处根据实际需要修改某些行的内容
- if (line.startsWith("a")) {
- buf.append("start with a").append("\n");
- }
- // 如果不用修改, 则按原来的内容回写
- else {
- buf.append(line).append("\n");
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- // 关闭流
- if (br != null) {
- try {
- br.close();
- } catch (IOException e) {
- br = null;
- }
- }
- }
- return buf.toString();
- }
- /**
- * 将内容回写到文件中
- *
- * @param filePath
- * @param content
- */
- public void write(String filePath, String content) {
- BufferedWriter bw = null;
- try {
- // 根据文件路径创建缓冲输出流
- bw = new BufferedWriter(new FileWriter(filePath));
- // 将内容写入文件中
- bw.write(content);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- // 关闭流
- if (bw != null) {
- try {
- bw.close();
- } catch (IOException e) {
- bw = null;
- }
- }
- }
- }
- /**
- * 主方法
- */
- public static void main(String[] args) {
- String filePath = "C:/file.txt"; // 文件路径
- FileModify obj = new FileModify();
- obj.write(filePath, obj.read(filePath)); // 读取修改文件
- }
- }