替换文本文件内容
Posted lev1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了替换文本文件内容相关的知识,希望对你有一定的参考价值。
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();
以上是关于替换文本文件内容的主要内容,如果未能解决你的问题,请参考以下文章