替换TXT文件里的字符串,一行一行查找替换,java代码

package com.play.code;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream; //import java.io.FileNotFoundException;
//import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class ChangeFile {
	public static void main(String[] argv) {
		try {
			BufferedReader bufReader = 
				new BufferedReader(
					new InputStreamReader(
						new FileInputStream(
							new File("D:/ooooo/我的周报.txt"))));

			StringBuffer strBuf = new StringBuffer();
			for (String tmp = null; (tmp = bufReader.readLine()) != null; tmp = null) {
				// 在这里做替换操作
				tmp = tmp.replaceAll("\\*", "#");
				tmp = tmp.replaceFirst("===", "\\* ");
				tmp = tmp.replaceAll("===", "");
				tmp = tmp.replaceAll("==", "===");
				strBuf.append(tmp);
				strBuf.append(System.getProperty("line.separator"));
			}
			bufReader.close();

			PrintWriter printWriter = new PrintWriter("D:/ooooo/我的周报-新.txt");
			printWriter.write(strBuf.toString().toCharArray());
			printWriter.flush();
			printWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
参考了这篇文章:http://www.blogjava.net/kenzhh/archive/2007/12/21/169364.html

你可能感兴趣的:(Java,java,null,string,class,file)