import java.io.File; import java.util.Scanner; public class FileTest { public static void main(String[] args) { String filePath, fileName; System.out.print("檔案名稱 ? "); Scanner sn = new Scanner(System.in); filePath = sn.next(); File fin = new File(filePath); fileName = fin.getName(); if (fin.isFile()) System.out.println("File exists : " + fileName); else if (fin.isDirectory()) System.out.println("Directory exists : " + fileName); else { System.out.println("File or Directory Not Found"); System.exit(0); } } }執行結果
測試有目錄存在時
沒有留言:
張貼留言