package com;
import java.io.*;
public class test {
public static void Test(String oldfilePath,String newfilePath){
File file = new File(oldfilePath);
//判断文件存在并且是文件
Boolean boo = file.exists()&&file.isFile();
System.out.println(boo);
if (boo) {
BufferedReader bufferedReader = null;
try {
//构造一个BufferedReader类来读取文件
bufferedReader = new BufferedReader(new FileReader(file));
String linetxt = null;
//result用来存储文件内容
StringBuilder result = new StringBuilder();
//按使用readLine方法,一次读一行
while ((linetxt = bufferedReader.readLine()) != null) {
System.out.println(linetxt);
String newconttent = linetxt.replace("china","中国");//替换
result.append(newconttent);
File newfile = new File(newfilePath);
PrintStream ps = new PrintStream(new FileOutputStream(newfile, true));
ps.println(newconttent);// 往aim.txt文件里写入字符串
}
//输出读出的所有数据(StringBuilder类型)
System.out.println(result);
//对文件内容操作
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}finally {
try {
bufferedReader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}else{
System.out.println("找不到指定的文件");
}
}
public static void main(String argv[]){
String oldfilePath = "E:\\毕业设计图\\test\\data\\content.txt";//content.txt路径。记得改
String newfilePath = "E:\\毕业设计图\\test\\data\\aim.txt";//aim.txt路径,记得改
Test(oldfilePath,newfilePath);
}
}