java 之 File類
阿新 • • 發佈:2020-09-01
淺析理論:
注:java中,\是轉義字元,如果要表示一個\字元,則必須轉義(\\) 一、檔案路徑的抽象(java.io.File) 三個構造器 File(String pathname) File f = new File("g:/a/b/c.txt");//new File("g:\\a\\b\\c.txt") File f = new File("c.txt");//相對路徑(相對類路徑) File(String parent, String child);//傳入父目錄和檔案字串路徑 File f = new File("g:/a/b", "c.txt"); File(File parent, String child);//傳入父目錄File物件和檔案字串路勁 File f = new File("c:/a/b"); File f1 = new File(f, "c.txt"); 二、常用方法 File[] fs = File.listRoots(); //獲取系統所有碟符 String path = fs.getPath(); //返回路徑(傳入路徑) String path2 = fs.getAbsolutePath(); //返回絕對路徑 String path3 = fs.getCanonicalPath(); //返回規範路徑【常用】 String path = f.getParent(); //獲取父路徑 File parent = f.getParentFile(); //獲取父路徑 f.exists();//判斷對應路徑檔案是否已存在 boolean f.mkdir(); //僅建立當前層級的目錄,用於目錄的父目錄已存在的情況下 boolean f.mkdirs(); //依次建立所有不存在的層級目錄 f.createNewFile(); //新建檔案,如果此路徑的父目錄存在的時候才會建立成功 f.delete(); //刪除檔案 f.renameTo(File dest); //重新命名檔案 三、常用屬性 boolean canExecute(); //可執行 boolean canRead(); //可讀 boolean canWrite(); //可寫 boolean isAbsolute(); //是否絕對路徑 boolean isDirectory(); //是否是目錄 boolean isFile(); //是否是檔案 boolean isHidden(); //是否是隱藏檔案 lastModified(); //最後一次被修改的時間 long length(); //檔案的長度 四、遍歷、過濾檔案 String[] list(); //指定表示目錄中的檔案和目錄 File[] listFiles(); //指定表示目錄中的檔案和目錄 //只能過濾當前層級的,需要通過遞迴去判斷 String[] list(FileFilter filter); //指定表示滿足指定過濾器的檔案和目錄 File[] listFiles(FileFilter filter); //指定表示滿足指定過濾器的檔案和目錄 File[] listFiles(FilenameFilter filter); //指定表示滿足指定過濾器的檔案和目錄
淺析案例:
package com.gongxy; import java.io.File; import java.io.FileFilter; import java.util.Arrays; /** * @author gongyg * @description File簡單案例 */ public class FileTest { public static void main(String[] args) throws Exception { //threeConstructorTest(); //getPathTest(); //createDeleteFile(); //commonAttribute(); iteratorFile(); } /* File 三個構造器測試 */ static void threeConstructorTest() throws Exception{ File f1 = new File("f:/測試/a/b.txt");//構造器1 File f2 = new File("b.txt");//這個是專案根路徑 File f3 = new File("f:/測試/a", "b.txt");//構造器2 File f5 = new File("f:/測試/a"); File f6 = new File(f5,"b.txt");//構造器3 System.out.println(f1.getCanonicalPath());//F:\測試\a\b.txt System.out.println(f2.getCanonicalPath());//F:\3Java-Study\my-java-study-0802\b.txt System.out.println(f3.getCanonicalPath());//F:\測試\a\b.txt System.out.println(f5.getCanonicalPath());//F:\測試\a System.out.println(f6.getCanonicalPath());//F:\測試\a\b.txt System.out.println(f1.exists());//false } /* 獲取相關路徑的API */ static void getPathTest() throws Exception{ File[] fs = File.listRoots(); System.out.println(Arrays.toString(fs));//[C:\, D:\, E:\, F:\] File f = new File("../c.txt"); System.out.println(f.getPath());//..\c.txt(傳入路徑) System.out.println(f.getAbsolutePath());//F:\3Java-Study\my-java-study-0802\..\c.txt System.out.println(f.getCanonicalPath());//F:\3Java-Study\c.txt System.out.println(f.getParent());//.. System.out.println(f.getParentFile());//.. } static void createDeleteFile() throws Exception{ File f = new File("g:/測試/測試a"); boolean result; if(!f.exists()){ f.mkdir();//false f.mkdirs();//true } File f1 = new File("測試B/b.txt"); if(!f1.exists()){ File f2 = f1.getParentFile(); if(!f2.exists()){ f2.mkdirs(); } f1.createNewFile();//true } if(f1.exists()){ //f1.delete();//只刪除了b.txt檔案 } File f3 = new File("測試B/c.txt"); f1.renameTo(f3);//只能在同一個目錄下更改,且如果已有檔案,則改名失敗 } static void commonAttribute() throws Exception{ File f1 = new File("c.txt"); f1.createNewFile(); boolean result = f1.canExecute();//true result = f1.canRead();//true result = f1.canWrite();//true result = f1.isAbsolute();//false result = f1.isDirectory();//false result = f1.isHidden();//false result = f1.isFile();//true f1.lastModified();//1598943309757 f1.length();//0 } static void iteratorFile() throws Exception{ File f = new File("2-App"); String[] sArray = f.list(); System.out.println(Arrays.toString(sArray)); //printPath(f); File[] fArray = f.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.getName().endsWith(".java"); } }); } static void printPath(File f) throws Exception{ File[] fArray = f.listFiles(); if(fArray.length == 0){ System.out.println(f); return; } for (File f1: fArray) { if(f1.isDirectory()){ printPath(f1); } System.out.println(f1.getCanonicalPath()); } } }