Advertisement
KuoHsiangYu

讀取檔案內容

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