[JAVA] 圖檔搜尋程式


/* 把電腦裡的圖檔印出來
 * 再把檔名存到一個檔案裡
 * 找到重複的檔案,並詢問是否要刪除
 */
import java.lang.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class fileImageSearch {
 // 成員區
 int sum = 0;
 long start = 0l;
 long end = 0l;
 String str = null;
 BufferedReader br = null;
 BufferedWriter bw = null;
 boolean store = false;
 SimpleDateFormat sdf = null;
 LinkedList al = new LinkedList<>();

 // 主程式區
 public static void main(String args[]) {
  fileImageSearch myClasa = new fileImageSearch();
  try {
   myClasa.Archive(); // 詢問使用者
   myClasa.start = System.currentTimeMillis();// 時間計算
   File f = new File("C:/");
   myClasa.Acc(f); // 印出所有檔名
   myClasa.end = System.currentTimeMillis();// 時間計算
   myClasa.Show();// 顯示結果
  } catch (Exception e) {}
 }

 
 public void Acc(File f) {// 讀出所有檔名
  sdf = new SimpleDateFormat("yyyy/MM/dd aaa hh:mm");
  File[] list = f.listFiles();
  try {
   for (int i = 0; i < list.length; i++) {
    if (list[i].isFile()) {
     if (Txt(list[i].getName())) {
      System.out.println("檔案名稱:  " + list[i] + "\t\t日期:"+ sdf.format(list[i].lastModified())+ "\t\t大小:  " + list[i].length() + " KB");
      sum++;
      al.add(list[i]);
      // System.out.println(al.size());
      if (store) {
       bw.write("檔案名稱:  " + list[i].getPath() + "\t\t日期:"+ sdf.format(list[i].lastModified())+ "\t\t大小:  " + list[i].length()+ " KB\r\n");
      }
     }
    } else {
     Acc(new File(list[i], ""));
    }
   }
  } catch (Exception e) {
  }
 }

 // 判斷是否為圖檔
 public boolean Txt(String name) { //檔案比對
  if (name.endsWith("jpg") || name.endsWith("gif")|| name.endsWith("bmp") || name.endsWith("tif")
    || name.endsWith("ufo") || name.endsWith("pcx")|| name.endsWith("png") || name.endsWith("upi"))
   return true;
  else
   return false;
 }

 public void Archive() throws Exception { //詢問是否存檔
  System.out.println("準備開始收尋...");
  br = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("請問是否存檔(Y/N)?");
  str = br.readLine();
  if (str.equalsIgnoreCase("Y")) {
   System.out.println("請輸入要儲存的檔案名稱:  ");
   bw = new BufferedWriter(new FileWriter("C:/Users/user/Desktop/" + br.readLine() + ".txt"));
   store = true;
  } else {}
 }

 public void Show() throws Exception { // 顯示+存檔
  System.out.println();
  System.out.println("電腦裡 共有  " + sum + "  圖檔,共花費  "   + ((end - start) / 1000) + "  秒");
  System.out.println("謝謝使用,歡迎再次使用。");
  System.out.println("儲存在List2的檔案名個數為:  "+al.size()); 
  if (str.equalsIgnoreCase("Y")) {
   bw.write("\r\n");
   bw.write("電腦裡 共有  " + sum + "  圖檔,共花費  " + ((end - start) / 1000)+ "  秒");
   bw.write("掃描結束\r\n");
   bw.write("謝謝使用,歡迎再次使用。");
   bw.flush();
   bw.close();
  }
 }
}