Java - Use Scanner to read file and parse string value

一般我們都是利用Scanner來實作輸入功能,如Scanner input = new Scanner(System.in);
在使用上非常方便,因為搭配Scanner的API已經幫你實作出你要回傳哪些型態的value並且不需要強制設定try catch的捕捉。

在此要帶的是利用Scanner去讀取檔案的行及parse String value,並且在String處理部分分別使用,如指定delimiter或設定pattern去做切割的動作!

主程式
public static void main(String[] args) {
  // TODO Auto-generated method stub
  Pattern pattern = Pattern.compile("(\\d+)::(\\w+)::(\\d+)::(\\d+)::(\\d+)");
  String delimiter = "::";
  try(Scanner file = new Scanner(new File("Transcripts.txt"));){
   while(file.hasNext()) {
    String line = file.nextLine();
    useDelimiter(line, delimiter);
    useRExpression(line, pattern);
   }
  }catch(FileNotFoundException e) {
   e.printStackTrace();
  }
 }

Transcrpt.txt
1::Tom::60::70::aa
2::Ben::90::80::77
3::Jerry::87::92::22

指定delimiter
private static void useDelimiter(String line, String delimiter) {
  Scanner lineScan = new Scanner(line).useDelimiter(delimiter);
  try {
   System.out.println("-------useDelimiter-------");
   System.out.println(lineScan.nextInt());
   System.out.println(lineScan.next());
   System.out.println(lineScan.nextInt());
   System.out.println(lineScan.nextInt());
   System.out.println(lineScan.nextInt());
  }catch(InputMismatchException e) {
   System.out.println(lineScan.next()+" => mismatch by nextInt");
  }finally {
   lineScan.close();
  }
 }

當使用Scanner時,預設分割字元是空白,因此您需呼叫useDelimiter去重新assign delimiter
PS. 若想要回復回原來的設定可以呼叫reset()
在此指定的delimiter為"::",可以發現在第一行的資料其最後一個項目應為數字,在此故意設為'aa',如此一來Scanner會throw Exception,此時呼叫next()可以取得該筆input option!

設定pattern
private static void useRExpression(String line, Pattern pattern) {
  Scanner lineScan = new Scanner(line);
  try{
   System.out.println("-------Regular Expression-------");
   lineScan.findInLine(pattern);
   MatchResult result = lineScan.match();
   for (int i=1; i<=result.groupCount(); i++)
    System.out.println(result.group(i));
  }catch(IllegalStateException e) {
   System.out.println(e.getMessage() +" from '"+line+"'");
  }finally {
   lineScan.close();
  }
 }

當使用正規表示式時,可以呼叫findInLine,若在match時有不符合pattern的內容在裡面,此時Scanner會throw Exception!

DEMO

留言