KuoHsiangYu

讀取檔案內容2

Oct 8th, 2018
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. //讀取檔案內容2
  2. //https://www.facebook.com/groups/1403852566495675/permalink/2189564644591126/
  3. //author: Kuo, Hsiang-Yu
  4. //author: 90ED_7FD4_5B87
  5. package com.sample;
  6.  
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.util.Scanner;
  10. import java.util.StringTokenizer;
  11.  
  12. public class MainClass {
  13.     public static void main(String[] args) {
  14.         // long t1 = System.nanoTime();
  15.         File file = new File("C:\\Users\\acer\\WorkspaceJava\\讀取檔案內容2\\test.txt");
  16.         Scanner scanner = null;
  17.         try {
  18.             scanner = new Scanner(file);
  19.         } catch (FileNotFoundException e) {
  20.             e.printStackTrace();
  21.             System.out.println("e = " + e);
  22.         }
  23.         int countLine = 0;
  24.         while (scanner.hasNext()) {
  25.             // 統計這個檔案有幾行字串。
  26.             scanner.nextLine();
  27.             countLine = countLine + 1;
  28.         }
  29.         // System.out.println("countLine = " + countLine);
  30.         try {
  31.             scanner = null;
  32.             scanner = new Scanner(file);
  33.         } catch (FileNotFoundException e) {
  34.             e.printStackTrace();
  35.             System.out.println("e = " + e);
  36.         }
  37.         Item[] item = new Item[countLine];// 建一個物件陣列來儲存字串內容。
  38.         String input = "";
  39.         String[] output2 = new String[4];// 設一個陣列,專門儲存切割出來的4個字串。
  40.         int j = 0, k = 0;
  41.         while (scanner.hasNext()) {
  42.             input = scanner.nextLine();
  43.             input = input.trim();// 清除字串前後多餘空白
  44.             // System.out.println(input);
  45.             StringTokenizer stringTokenizer = new StringTokenizer(input);
  46.             j = 0;
  47.             while (stringTokenizer.hasMoreTokens()) {
  48.                 output2[j] = stringTokenizer.nextToken();
  49.                 j = j + 1;
  50.             }
  51.             item[k] = new Item(output2[0], output2[1], output2[2], output2[3]);
  52.             k = k + 1;
  53.         }
  54.         // long t2 = System.nanoTime();
  55.         // double seconds = (double) (t2 - t1) / 1000000000.0D;
  56.         // System.out.printf("%f seconds\n", seconds);
  57.         scanner = null;
  58.         scanner = new Scanner(System.in);
  59.         System.out.printf("請輸入檢索資料:");
  60.         input = scanner.next();
  61.         boolean findData = false;
  62.         for (int i = 0; i < item.length; i++) {
  63.             if (item[i].type.equals(input)) {
  64.                 item[i].show();
  65.                 findData = true;
  66.             }
  67.         }
  68.         if (findData == false) {
  69.             System.out.printf("查無資料。\n");
  70.         }
  71.         scanner.close();
  72.         System.out.printf("\n");
  73.         System.out.println("BUILD SUCCESSFUL (total time: 0 seconds)");
  74.     }
  75. }
  76.  
  77. class Item {
  78.     String id;
  79.     String name;
  80.     String type;
  81.     String year;
  82.  
  83.     Item(String id, String name, String type, String year) {
  84.         this.id = id;
  85.         this.name = name;
  86.         this.type = type;
  87.         this.year = year;
  88.     }
  89.  
  90.     void show() {
  91.         // System.out.println(id + " " + name + " " + type + " " + year);
  92.         System.out.printf("%-8s%-7s%-8s%s\n", id, name, type, year);
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment