File類基礎,file類

來源:互聯網
上載者:User

File類基礎,file類

File類的作用:
Java的io包中定義了File類,用於對檔案或檔案夾的管理操作。
File類只能夠用於表示檔案或檔案夾的資訊(屬性)和對該檔案或檔案夾的刪除建立操作
(不能對內容進行訪問)
通過建立File類對象可以用程式來操作電腦上的檔案或檔案夾。

 

File類的構造方法:
File(String path):通過指定的路徑名字串來建立一個新的File對象,抽象路徑名是用來表示某個檔案或檔案夾的路徑定位。
File(File parent,String path):根據parent父檔案對象和path子檔案名稱字串來建立一個新的File對象。

 

File類的常用API
檔案操作:
exists():判斷檔案是否存在:傳回值為布爾值
getCanonicalPath() :擷取當前檔案的標準路徑名 
separator:用於表示路徑層級分割符,可以避免不同作業系統的分割符不一致的情況
createNewFile():在硬碟中建立一個檔案
length():長度
lastModified():建立時間:new Date(File.lastModified())
delete():刪除
getName():獲得檔案或檔案夾名

檔案夾操作:
exists():判斷檔案是否存在:傳回值為布爾值
getCanonicalPath()  :擷取當前檔案的標準路徑名 
separator:用於表示路徑層級分割符,可以避免不同作業系統的分割符不一致的情況
boolean mkdir():建立檔案夾
mkdirs():如果父資料夾不存在就一起建立
listFiles():該方法返回的路徑名用於表示此抽象路徑名的目錄中的檔案和目錄的數組
listFiles(FileFilter filter) :該方法返回的路徑名用於表示此抽象路徑名的目錄中的檔案和目錄的數組,並且帶有FileFilter類型的過濾器
isDirectory():判斷是否檔案夾
delDirectory(pp):調用刪除檔案夾方法

 1 public void testFile01() throws IOException{ 2         //在電腦記憶體中建立一個File對象,用來對應電腦的儲存硬碟中的當前檔案夾 3         File file = new File("."); 4         System.out.println("當前檔案夾是否存在:"+file.exists()); 5         System.out.println("查看當前檔案夾的路徑位置:"+file.getCanonicalPath()); 6         //與硬碟中是否存在檔案夾沒有直接聯絡,只是存在JVM中的對象 7         File fileDemo = new File(file,"fileDemo"); 8         System.out.println("查看當前檔案夾中是否存在fileDemo檔案夾:"+fileDemo.exists()); 9         if(!fileDemo.exists()){10             fileDemo.mkdir();//建立出fileDemo檔案夾  11         }12         13                 File testFile = new  File(file,"aa"+File.separator+"testFile");14         if(!testFile.exists()){15 //            boolean flag = testFile.mkdir();16 //            System.out.println("建立結果:"+flag);17             testFile.mkdirs();//建立檔案夾,當父目錄不存在會把父目錄先建立出來18         }19         20         File aFile = new File(fileDemo,"a.txt");21         System.out.println("a.txt在硬碟中是否存在:"+aFile.exists());22         if(!aFile.exists()){23             aFile.createNewFile();//在硬碟中建立一個檔案24         }25         System.out.println("a.txt在硬碟中是否存在:"+aFile.exists());26         System.out.println("a.txt的標準路徑為:"+aFile.getCanonicalPath());27         System.out.println("a.txt的檔案長度為:"+aFile.length());28         System.out.println("a.txt的建立時間:"+new Date(aFile.lastModified()));29         /*30          * 刪除檔案對象表示的檔案或檔案夾,成功刪除返回true。31          * 如果檔案對象表示的是一個檔案夾,刪除的時候必須保證此檔案夾是空檔案夾才能刪除32          */33         aFile.delete();34         //刪除aa會失敗35         File aa = new File(file,"aa");36         aa.delete();37     }
public void testFile02(){        File file = new File(".");//當前檔案夾對象        File[] files = null;        if(file.exists()){            //listFiles():查看當前檔案夾下有什麼子檔案或子檔案夾,如果檔案夾路徑為空白,返回空            //如果此路徑名不是表示一個檔案夾或者發生了I/O異常,也返回null             files = file.listFiles();        }        for(File f:files){            //getName():獲得檔案或檔案夾名            System.out.println(f.getName());        }    }
    public void testFile03() throws IOException{        File path = new File(".");        File pppp = new File(path,"pp"+File.separator+"ppp"+File.separator+"pppp");        File demoFile = new File(pppp,"demo.txt");        if(!pppp.exists()){            pppp.mkdirs();//將父資料夾與子檔案夾一起建立        }        if(!demoFile.exists()){            demoFile.createNewFile();//建立檔案        }        File pp = new File(path,"pp");        delDirectory(pp);//調用刪除檔案夾方法下面沒有檔案則可以成功    }
//使用遍曆方法刪除檔案夾,如果內部有檔案則刪除檔案,防止無法刪除檔案夾    public static void delDirectory(File dir) throws IOException{        if(!dir.exists()){//如果檔案夾不存在,拋出運行時異常            throw new RuntimeException("檔案夾"+dir+"不存在");        }        if(!dir.isDirectory()){//判斷是否檔案夾            throw new RuntimeException(dir+"不是目錄");        }        File[] subs = dir.listFiles();        if(subs!=null&&subs.length>0){//遍曆所有子檔案和檔案夾,刪除裡面內容            //遍曆所有的subs            for(File f:subs){                if(!f.isDirectory()){//不是檔案夾就是檔案                    System.out.println("檔案名稱為:"+f.getName());                    if(!f.delete()){//如果刪除失敗則拋出異常,如果成功則不進來,檔案也刪掉了                        throw new IOException("無法刪除檔案"+f.getName());                    }                }else{//檔案夾處理                    System.out.println("開始處理檔案夾"+f.getName());                    delDirectory(f);//調用刪除檔案夾方法,刪除子檔案夾                    System.out.println("子檔案夾已經刪除,開始刪除該檔案夾"+f.getName());                    f.delete();//刪除完子檔案和子檔案夾後,需要刪除當前的檔案夾                }            }        }        dir.delete();//刪除dir目錄的內容    }
    //使用過濾器過濾掉一部分檔案,處理符合規則的檔案    public void testFile04(){        File file = new File("src"+File.separator+"day07");//建立檔案對象        FileFilter filter = new FileFilter(){            //該方法用於定義過濾規則,如果return true則表示符合規則的            //傳回值為false  則表示不符合規則的            @Override            public boolean accept(File pathname) {                //matches匹配符合Regex規則的內容                if(pathname.getName().matches(".*[.]java")){                    return true;                }                return false;            }        };        //傳入過濾器對象,過濾掉不符合規則的file對象        File[] paths = file.listFiles(filter);        for(File f:paths){            System.out.println(f.getName());        }    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.