File類的使用

來源:互聯網
上載者:User

標籤:

File類的使用(代碼片+注釋)

 

 1 package Zhang; 2  3 import java.io.File; 4  5 public class FileTest { 6  7       public static void main(String[] args){      8  9             /*File檔案目錄10 11             * list()以字串數組形式返迴文件目錄中的檔案和檔案夾12 13             * listFiles()返回一個抽象路徑名數組,這些路徑名表示此抽象路徑名表示的目錄中的檔案14 15             * */16 17             for(String i:new File("/home/zhang/Music/").list()){18 19                   System.out.print(i+"\n");20 21             }22 23            24 25             File file=new File("/home/zhang");26 27             //測試此抽象路徑名或表示的檔案或目錄是否存在28 29             System.out.println("檔案是不是存在:"+file.exists());30 31             //測試此抽象路徑名表示的檔案是否是一個目錄32 33             System.out.println("是不是一個目錄:"+file.isDirectory());34 35             //測試此抽象路徑名指定的檔案是不是一個隱藏檔案36 37             System.out.println("是不是隱藏的檔案:"+file.isHidden());38 39             //設定此抽象路徑名指定的目錄的擁有者的可讀許可權40 41             file.setReadable(true);42 43             System.out.println("此目錄的父目錄是:"+file.getParent());44 45             //列出目錄中沒有被隱藏的目錄46 47        for(File fileDirectory:file.listFiles()){48 49                   if(!fileDirectory.isHidden()){50 51                         System.out.println(fileDirectory.getName());52 53                   }54 55             }56 57             System.out.println("----------------------");58 59             for(File fileName:File.listRoots()){60 61                   for(String strFile:fileName.list()){62 63                         System.out.println(strFile);64 65                   }66 67             }68 69       }70 71 }72 73  

 

 

2016-10-08

File類的使用

聯繫我們

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