码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA 面试题:替换文本文件里的制定字符串

时间:2016-09-09 08:47:20      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

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!");
}
}
 

JAVA 面试题:替换文本文件里的制定字符串

标签:

原文地址:http://www.cnblogs.com/tianm2016/p/5855164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!