import java.io.*;public class ReplaceText { public String myRead(String path) { FileReader myFileReader = null; try { myFileReader = new FileReader(path); } catch (FileNotFoundException e) { e.printStackTrace(); } BufferedReader myBufferedReader = new BufferedReader(myFileReader); String myString = null; String resultString = ""; try { while ((myString = myBufferedReader.readLine()) != null) { resultString += myString; resultString += "\n"; } } catch (IOException e1) { e1.printStackTrace(); } try { myFileReader.close(); } catch (IOException e2) { e2.printStackTrace(); } return resultString; } public String newReplace(String s, String oldS, String newS) { String resultS = ""; String[] ss; ss = s.split(" "); for (int i = 0; i < ss.length; i++) { if (ss[i].equals(oldS)) { ss[i] = newS; } } for (int i = 0; i < ss.length; i++) { resultS += ss[i] + " "; } System.out.println(resultS); return resultS; } public void myWrite(String path, String sb) { FileWriter fw = null; try { fw = new FileWriter(path); } catch (IOException e2) { e2.printStackTrace(); } try { fw.write(sb.toString()); } catch (IOException e) { e.printStackTrace(); } try { fw.close(); } catch (IOException e1) { e1.printStackTrace(); } } public static void main(String[] args) throws IOException { ReplaceText rt = new ReplaceText(); String sb = rt.newReplace(rt.myRead("c:/1.txt"), "i", "-i-"); rt.myWrite("c:/2.txt", sb); }}1.txt: ai i can i seeso i goodado i dosi i iso oosi ppif io] ooo99 jl il s i di ii i can i can i can i can2.txt: ai -i- can i seeso -i- goodado -i- dosi -i- iso oosi ppif io] ooo99 jl il s -i- di ii -i- can i can i can i can
打开文件B,用于写;while( 读文件A的一行 ) {
用String类的replace()方法进行文字替换;
把得到的新String写入文件B中;
}关闭文件A;
关闭文件B;
replaceAll
createFile
{
public static void main(String[] args)
{
System.out.println("Main begin...");
//源文件路径
String sourceFilePath = new String("D:\\test\\file\\test_source.txt");
String targetFilePath = new String("D:\\test\\file\\test_target.txt");
//要替换的字符串
String fromString = new String("someting to replace");
//替换为
String toString = new String("XXX YYYYY ZZZ");
GoGoGo(sourceFilePath, targetFilePath, fromString, toString);
System.out.println("Main end...\n----------------...");
}
public static void GoGoGo(
String sourceFilePath,
String targetFilePath,
String fromString,
String toString)
{
System.out.println("GoGoGo begin...");
//临时字符串
String tempString = new String("");
File sf = new File(sourceFilePath);
File tf = new File(targetFilePath);
try
{
BufferedReader brs = new BufferedReader(new FileReader(sf));
BufferedWriter bwt = new BufferedWriter(new FileWriter(tf));
tempString = brs.readLine();
while(tempString!=null)
{
tempString = tempString.replaceAll(fromString, toString);
bwt.write(tempString + "\r\n");
tempString = brs.readLine();
}
brs.close();
bwt.close();
}
catch(IOException e)
{
System.err.println("GoGoGo Error:" + e);
}
System.out.println("GoGoGo end...\n----------------...");
}
}
FileReader myFileReader = null;
try {
myFileReader = new FileReader(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader myBufferedReader = new BufferedReader(myFileReader); String myString = null; String resultString = ""; try {
while ((myString = myBufferedReader.readLine()) != null) {
resultString += myString;
resultString += "\n"; }
} catch (IOException e1) {
e1.printStackTrace();
} try {
myFileReader.close();
} catch (IOException e2) {
e2.printStackTrace();
}
return resultString;
} public String newReplace(String s, String oldS, String newS) {
String resultS = "";
String[] ss;
ss = s.split(" ");
for (int i = 0; i < ss.length; i++) {
if (ss[i].equals(oldS)) {
ss[i] = newS;
}
} for (int i = 0; i < ss.length; i++) {
resultS += ss[i] + " ";
}
System.out.println(resultS);
return resultS; } public void myWrite(String path, String sb) { FileWriter fw = null;
try {
fw = new FileWriter(path);
} catch (IOException e2) {
e2.printStackTrace();
}
try {
fw.write(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
} public static void main(String[] args) throws IOException {
ReplaceText rt = new ReplaceText();
String sb = rt.newReplace(rt.myRead("c:/1.txt"), "i", "-i-");
rt.myWrite("c:/2.txt", sb); }}1.txt:
ai i can
i seeso i goodado i dosi i iso oosi ppif
io] ooo99 jl il s i di ii i can
i can
i can
i can2.txt:
ai -i- can
i seeso -i- goodado -i- dosi -i- iso oosi ppif
io] ooo99 jl il s -i- di ii -i- can
i can
i can
i can
不好意思