public class File_test1 { public static void main(String[] args) { File f = new File("E:", "a.txt"); try { if (f.exists()) { System.out.println("檔案存在!"); System.out.println("檔案名稱:" + f.getName() .substring(0, f.getName().lastIndexOf('.'))); System.out.println("檔案相對路徑:" + f.getPath()); // 相對路徑 System.out.println("檔案相對路徑:" + f.getAbsolutePath()); // 絕對路徑 System.out.println("檔案父親目錄:" + f.getParent()); File p = f.getParentFile();// 擷取父目錄檔案 System.out.println("父目錄絕對路徑:" + p.getAbsolutePath()); System.out.print("檔案讀取許可權:"); System.out.println(f.canRead() ? "可讀" : "不可讀"); System.out.print("是否可寫:"); System.out.println(f.canWrite() ? "可寫" : "不可寫"); System.out.print("是否目錄:"); System.out.println(f.isDirectory() ? "是" : "否"); System.out.print("是否檔案:"); System.out.println(f.isFile()?"是":"否"); System.out.print("是否隱藏:"); System.out.println(f.isHidden()?"是":"否"); System.out.println("檔案長度:"+f.length()); } else { System.out.println("檔案不存,將建立檔案"); f.createNewFile(); } } catch (Exception e) { // TODO: handle exception System.out.println(e.getMessage()); } } }