標籤:des android style blog io ar color 使用 sp
(一)擷取總根 File[] fileList=File.listRoots(); //返回fileList.length為1 //fileList.getAbsolutePath()為"/" //這就是系統的總根 (二)開啟總根目錄 File file=new File("/"); File[] fileList=file.listFiles(); //擷取的目錄中除了"/sdcard"和"/system"還有"/data"、"/cache"、"/dev"等 //Android的根目錄並不像Symbian系統那樣分為C盤、D盤、E盤等 //Android是基於Linux的,只有目錄,無所謂盤符 (三)擷取系統儲存根目錄 File file=Environment.getRootDirectory();//File file=new File("/system"); File[] fileList=file.listFiles(); //這裡說的系統僅僅指"/system" //不包括外部儲存的手機儲存的範圍遠遠大於所謂的系統儲存 (四)擷取SD卡儲存根目錄(及系統在SD卡設定的公用媒體檔案夾:camera、movie、picture、music...) File file=Environment.getExternalStorageDirectory();//File file=new File("/sdcard"); File[] fileList=file.listFiles(); //要擷取SD卡首先要確認SD卡是否裝載 boolean is=Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); //如果true,則已裝載 //如果false,則未裝載 File pictures = Environment.getExternalStorageState().equals(Environment.DIRECTORY_PICTURES);//File file=new File("/pictures"); Environment: DIRECTORY_MUSIC, //音頻 DIRECTORY_PODCASTS, //播客視頻 DIRECTORY_RINGTONES, //鈴聲音頻 DIRECTORY_ALARMS, //鬧鈴音頻 DIRECTORY_NOTIFICATIONS, //訊息音頻 DIRECTORY_PICTURES, //圖片 DIRECTORY_MOVIES, //視頻 DIRECTORY_DOWNLOADS, //下載 DIRECTORY_DCIM //照相機/攝像機 *******建立自己的緩衝目錄,如果過SD卡存在則使用SD上的緩衝目錄,不存在則使用系統緩衝目錄 final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !isExternalStorageRemovable() ? getExternalCacheDir(context).getPath() : context.getCacheDir().getPath(); File myCacheFile = new File(cachePath + File.separator + "myCacheFile"); //判斷SD是內建的還是可拆卸的,要求API 9 @TargetApi(9) public static boolean isExternalStorageRemovable() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { return Environment.isExternalStorageRemovable(); } return true; } //獲得SD卡緩衝目錄,API 8及以上可直接擷取,以下需自行構建目錄 @TargetApi(8) public static File getExternalCacheDir(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) { return context.getExternalCacheDir(); } // Before Froyo we need to construct the external cache dir ourselves final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/"; return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir); } ******** (五)擷取data根目錄 File file=Environment.getDataDirectory();//File file=new File("/data"); File[] fileList=file.listFiles(); //由於data檔案夾是android裡一個非常重要的檔案夾,所以一般許可權是無法擷取到檔案的,即fileList.length返回為0 (六)擷取私人檔案路徑 Context context=this;//首先,在Activity裡擷取context File file=context.getFilesDir(); String path=file.getAbsolutePath(); //此處返回的路勁為/data/data/包/files,其中的包就是我們建立的主Activity所在的包 //我們可以看到這個路徑也是在data檔案夾下 //程式本身是可以對自己的私人檔案進行操作 //程式中很多私人的資料會寫入到私人檔案路徑下,這也是android為什麼對data資料做保護的原因之一 (七)擷取檔案(夾)絕對路徑、相對路勁、檔案(夾)名、父目錄 File file=…… String relativePath=file.getPath();//相對路徑 String absolutePath=file.getAbsolutePath();//絕對路徑 String fileName=file.getName();//檔案(夾)名 String parentPath=file.getParent();//父目錄 (八)列出檔案夾下的所有檔案和檔案夾 File file=…… File[] fileList=file.listFiles(); (九)判斷是檔案還是檔案夾 File file=…… boolean is=file.isDirectory();//true-是,false-否 (十)判斷檔案(夾)是否存在 File file=…… boolean is=file.exists();//true-是,false-否 (十一)建立檔案(夾) File file=…… oolean is=file.isDirectory();//判斷是否為檔案夾 /*方法1*/ if(is){ String path=file.getAbsolutePath(); String name="ABC";//你要建立的檔案夾名或者檔案名稱 String pathx=path+name; File filex=new File(pathx); boolean is=filex.exists();//判斷檔案(夾)是否存在 if(!is){ filex.mkdir();//建立檔案夾 //filex.createNewFile();//建立檔案 } /*方法2*/ if(is){ String path=file.getAbsolutePath(); String name="test.txt";//你要建立的檔案夾名或者檔案名稱 File filex=new File(path,name);//方法1和方法2的區別在於此 boolean is=filex.exists();//判斷檔案(夾)是否存在 if(!is){ filex.mkdir();//建立檔案夾 //filex.createNewFile();//建立檔案 } (十二)重新命名檔案(夾) File file=…… String parentPath=file.getParent(); String newName="name";//重新命名後的檔案或者檔案夾名 File filex=new File(parentPath,newName);//File filex=new File(parentPaht+newName) file.renameTo(filex); (十三)刪除檔案(夾) File file=…… file.delete();//立即刪除 //file.deleteOnExit();//程式退出後刪除,只有正常退出才會刪除 ----------------------------------------------------------------------------------------------
Android 檔案系統路徑