Android學習筆記(5)—關於擷取File路徑、檔案過濾

來源:互聯網
上載者:User

註:基於開發的項目:LearningFlash 一些程式碼片段

1、變數的聲明

private String mRoot = "";//根路徑private String[] fileName;//用來存放檔案名稱private File[] mFiles; //用來存放擷取到的檔案int FileLen = 0; //擷取到的檔案個數

2、基於根路徑擷取器路徑下的所有檔案

private int getCur_Directory(String dirPath) {// TODO Auto-generated method stubtry {File f = new File(dirPath);File[] files = f.listFiles();// 根據。。。分類// sortFilesByDirectory(temp);mFiles = files;FileLen = files.length;} catch (Exception ex) {}return FileLen;}

3、調用getCur_Directory

mRoot = "/sdcard/快樂學堂/快樂英語/英語對話";FileLen = getCur_Directory(mRoot);fileName = new String[FileLen];for (int i = 0; i < FileLen; i++) {fileName[i] = mFiles[i].getName();}

4、最近在研究關於檔案名稱過濾問題,查了一下資料,得知FileFilter可實現,無非就是使用一個方法:accept

可以看一下例子:

   private int getCur_Directory(String dirPath) {          // TODO Auto-generated method stub            try {              File f = new File(dirPath);              File[] files = f.listFiles(new FileFilter() {@Overridepublic boolean accept(File pathname) {// TODO Auto-generated method stubString filename = pathname.getPath();if (pathname.isDirectory())return true;              if(filename.endsWith(".lyl"))                  return true;              else                  return false; }});              // 根據。。。分類              // sortFilesByDirectory(temp);              mFiles = files;              FileLen = files.length;            } catch (Exception ex) {            }           return FileLen;      }  

return true則包含就來,不然就不包含。也就是說把不想要的過濾了。這樣便達到了要求。

5、傳一個路徑,擷取改路徑下的所有想要尾碼的檔案:http://blog.csdn.net/xiangyong2008/article/details/5899740

聯繫我們

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