Android入門之檔案系統操作(二)檔案操作相關指令

來源:互聯網
上載者:User

標籤:comm   storage   roots   context   dev   路徑   href   roo   toc   

(一)擷取總根

[java] view plain copy
  1. File[] fileList=File.listRoots();  
  2. //返回fileList.length為1  
  3. //fileList.getAbsolutePath()為"/"  
  4. //這就是系統的總根  

(二)開啟總根目錄

[java] view plain copy
  1. File file=new File("/");  
  2. File[] fileList=file.listFiles();  
  3. //擷取的目錄中除了"/sdcard"和"/system"還有"/data"、"/cache"、"/dev"等  
  4. //Android的根目錄並不像Symbian系統那樣分為C盤、D盤、E盤等  
  5. //Android是基於Linux的,只有目錄,無所謂盤符  

(三)擷取系統儲存根目錄

[java] view plain copy
  1. File file=Environment.getRootDirectory();//File file=new File("/system");  
  2. File[] fileList=file.listFiles();  
  3. //這裡說的系統僅僅指"/system"  
  4. //不包括外部儲存的手機儲存的範圍遠遠大於所謂的系統儲存  

(四)擷取SD卡儲存根目錄

[java] view plain copy
  1. File file=Environment.getExternalStorageDirectory();//File file=new File("/sdcard");  
  2. File[] fileList=file.listFiles();  
  3. //要擷取SD卡首先要確認SD卡是否裝載  
  4. boolean is=Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);  
  5. //如果true,則已裝載  
  6. //如果false,則未裝載  

(五)擷取data根目錄

[java] view plain copy
  1. File file=Environment.getDataDirectory();//File file=new File("/data");  
  2. File[] fileList=file.listFiles();  
  3. //由於data檔案夾是android裡一個非常重要的檔案夾,所以一般許可權是無法擷取到檔案的,即fileList.length返回為0  

(六)擷取私人檔案路徑

[java] view plain copy
  1. Context context=this;//首先,在Activity裡擷取context  
  2. File file=context.getFilesDir();  
  3. String path=file.getAbsolutePath();  
  4. //此處返回的路勁為/data/data/包/files,其中的包就是我們建立的主Activity所在的包  
  5. //我們可以看到這個路徑也是在data檔案夾下  
  6. //程式本身是可以對自己的私人檔案進行操作  
  7. //程式中很多私人的資料會寫入到私人檔案路徑下,這也是android為什麼對data資料做保護的原因之一  

(七)擷取檔案(夾)絕對路徑、相對路勁、檔案(夾)名、父目錄

[java] view plain copy
  1. File file=……  
  2. String relativePath=file.getPath();//相對路徑  
  3. String absolutePath=file.getAbsolutePath();//絕對路徑  
  4. String fileName=file.getName();//檔案(夾)名  
  5. String parentPath=file.getParent();//父目錄  

(八)列出檔案夾下的所有檔案和檔案夾

[java] view plain copy
  1. File file=……  
  2. File[] fileList=file.listFiles();  

(九)判斷是檔案還是檔案夾

[java] view plain copy
  1. File file=……  
  2. boolean is=file.isDirectory();//true-是,false-否  

(十)判斷檔案(夾)是否存在

[java] view plain copy
  1. File file=……  
  2. boolean is=file.exists();//true-是,false-否  

(十一)建立檔案(夾)

[java] view plain copy
  1. File file=……  
  2. oolean is=file.isDirectory();//判斷是否為檔案夾  
  3. /*方法1*/  
  4. if(is){  
  5.     String path=file.getAbsolutePath();  
  6.     String name="ABC";//你要建立的檔案夾名或者檔案名稱  
  7.     String pathx=path+name;  
  8.     File filex=new File(pathx);  
  9.     boolean is=filex.exists();//判斷檔案(夾)是否存在  
  10.     if(!is){  
  11.         filex.mkdir();//建立檔案夾  
  12.         //filex.createNewFile();//建立檔案  
  13.     }  
  14. /*方法2*/  
  15. if(is){  
  16.     String path=file.getAbsolutePath();  
  17.     String name="test.txt";//你要建立的檔案夾名或者檔案名稱  
  18.     File filex=new File(path,name);//方法1和方法2的區別在於此  
  19.     boolean is=filex.exists();//判斷檔案(夾)是否存在  
  20.     if(!is){  
  21.         filex.mkdir();//建立檔案夾  
  22.         //filex.createNewFile();//建立檔案  
  23. }  

(十二)重新命名檔案(夾)

[java] view plain copy
  1. File file=……  
  2. String parentPath=file.getParent();  
  3. String newName="name";//重新命名後的檔案或者檔案夾名  
  4. File filex=new File(parentPath,newName);//File filex=new File(parentPaht+newName)  
  5. file.renameTo(filex);  

(十三)刪除檔案(夾)

[java] view plain copy
    1. File file=……  
    2. file.delete();//立即刪除  
    3. //file.deleteOnExit();//程式退出後刪除,只有正常退出才會刪除 

Android入門之檔案系統操作(二)檔案操作相關指令

相關文章

聯繫我們

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