package testJava;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
public class ReplaceString {
public static void main(String[] args) {
File file = new File("c:\\test.txt");
ArrayList<String> content = new ArrayList<String>();
try {
InputStreamReader in = new InputStreamReader(new FileInputStream(file), "UTF-8");
BufferedReader read = new BufferedReader(in);
String line = null;
while ((line = read.readLine()) != null) {
line = line.replaceAll("OldString", "NewString");
content.add(line);
}
read.close();
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
BufferedWriter write = new BufferedWriter(out);
Iterator<String> cont = content.iterator();
while (cont.hasNext()) {
write.write(cont.next()+"\r\n");
}
write.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Completed!");
}
}