替换文本文件内容
package cn.home;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Replace {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("F:\\javaclasszjl\\test.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
FileOutputStream fos = new FileOutputStream("F:\\javaclasszjl\\abc.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
String s1 = "";
String s2 = "";
System.out.print("替换前:");
while((s1 = br.readLine())!=null) {
System.out.println(s1);
s2 = s1;
}
s2 = s2.replace("{name}", "小黑");
s2 = s2.replace("{type}", "狗狗");
s2 = s2.replace("{master}", "张丹");
System.out.print("替换后:");
bw.write(s2);
System.out.println(s2);
bw.close();
osw.close();
fos.close();
br.close();
isr.close();
fis.close();
}
}