java 遞迴實現刪除或查詢指定目錄下的全部檔案

來源:互聯網
上載者:User

標籤:讀取   file類   string   anr   new   推斷   目錄   director   system   

/** * 遞迴列舉盤符下的全部檔案的名稱,如E:\HeartIsland *  * @author HeartIsland *  */public class FileListDemo {    /**     * 遞迴列舉盤符下的全部檔案的名稱。如E:\HeartIsland之下的全部檔案 並吧路徑列印出來     *     * File file = new File("E:\\HeartIsland\\");     *  // 列出檔案的路徑     *  System.out.println(file.getPath());     *  // 推斷是否為目錄     *  System.out.println(file.isDirectory());     *  // 列舉某個盤符以下的檔案或者目錄     *      */    public static void test(File file) {        File[] files = file.listFiles();        for (int i = 0; i < files.length; i++) {            if (files[i].isDirectory()) {                test(files[i]);            } else {                System.out.print("刪除成功:"+files[i].getPath()+"\t");//檔案路徑查詢並列印                System.out.print("是否運行:"+files[i].delete()+"\n");//檔案運行刪除(有傳回值)            }        }    }    public static void main(String[] args) {        File file = new File("D:\\HeartIsland\\");        test(file);    }}





File類的讀取權限類別:


public boolean canExecute();//推斷能否夠運行


public boolean canRead();//推斷檔案能否夠讀取


public boolean canWrite();//推斷檔案是否可寫入




public boolean createNewFile();//建立一個新檔案


public boolean delete();//刪除檔案或目錄


public String getAbsolutePath();//獲得檔案的絕對路徑


public String getName();//獲得檔案的名稱


public String getPath();//將路徑名變為字串




public boolean isDirectory();//推斷檔案是否為一個目錄


public boolean isFile();//推斷檔案是否為檔案


public boolean isHidden();//推斷檔案是否是隱藏的


public long length();//返迴文件 的長度

java 遞迴實現刪除或查詢指定目錄下的全部檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.