import java.io.File;
import java.util.Scanner;
import java.io.*;
public class TextScanner {
public static void main(String[] args) {
try{
FileReader fr = new FileReader("D:\\w.txt"); //FileReader fr
BufferedReader br = new BufferedReader(fr); //BufferedReader br
String a;
while((a= br.readLine())!=null){
System.out.print(a);
}
fr.close();
br.close();
}catch(IOException e){
System.out.print(e);
}
}
}
//定义了一个文件字符输入流对像fr,接著打开输入流文件w.txt并连接成带缓冲的输入流,然后依行读取文本,每次文件text.txt中读出一行
import java.util.regex.*;
public class BasicReplace {
public static void main(String[] args) {
CharSequence inputStr = "a b c a b c";
String patternStr = "a";
String replacementStr = "x"; // \r\n 回车换行
// Compile regular expression
Pattern pattern = Pattern.compile(patternStr);
// Replace all occurrences of pattern in input
Matcher matcher = pattern.matcher(inputStr);
String output = matcher.replaceAll(replacementStr);
System.out.println(output);
// x b c x b c
}
}
regex refer :
http://www.regexlab.com/zh/regref.htm
http://java.ccidnet.com/art/297/20060611/576975_1.html
实用工具类