Java 進(jìn)階 1
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);
?? }
? }
}
//定義了一個(gè)文件字符輸入流對(duì)像fr,接著打開(kāi)輸入流文件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?回車(chē)換行
????
??????????? // 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
? 實(shí)用工具類(lèi)
posted on 2008-10-07 17:42 Anderson 閱讀(166) 評(píng)論(0) 編輯 收藏 所屬分類(lèi): java