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去做切割的動作!

主程式
  1. public static void main(String[] args) {
  2. // TODO Auto-generated method stub
  3. Pattern pattern = Pattern.compile("(\\d+)::(\\w+)::(\\d+)::(\\d+)::(\\d+)");
  4. String delimiter = "::";
  5. try(Scanner file = new Scanner(new File("Transcripts.txt"));){
  6. while(file.hasNext()) {
  7. String line = file.nextLine();
  8. useDelimiter(line, delimiter);
  9. useRExpression(line, pattern);
  10. }
  11. }catch(FileNotFoundException e) {
  12. e.printStackTrace();
  13. }
  14. }

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

指定delimiter
  1. private static void useDelimiter(String line, String delimiter) {
  2. Scanner lineScan = new Scanner(line).useDelimiter(delimiter);
  3. try {
  4. System.out.println("-------useDelimiter-------");
  5. System.out.println(lineScan.nextInt());
  6. System.out.println(lineScan.next());
  7. System.out.println(lineScan.nextInt());
  8. System.out.println(lineScan.nextInt());
  9. System.out.println(lineScan.nextInt());
  10. }catch(InputMismatchException e) {
  11. System.out.println(lineScan.next()+" => mismatch by nextInt");
  12. }finally {
  13. lineScan.close();
  14. }
  15. }

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

設定pattern
  1. private static void useRExpression(String line, Pattern pattern) {
  2. Scanner lineScan = new Scanner(line);
  3. try{
  4. System.out.println("-------Regular Expression-------");
  5. lineScan.findInLine(pattern);
  6. MatchResult result = lineScan.match();
  7. for (int i=1; i<=result.groupCount(); i++)
  8. System.out.println(result.group(i));
  9. }catch(IllegalStateException e) {
  10. System.out.println(e.getMessage() +" from '"+line+"'");
  11. }finally {
  12. lineScan.close();
  13. }
  14. }

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

DEMO

留言