1. 程式人生 > >android 建立資料夾和檔案

android 建立資料夾和檔案

目的:在APP的私有目錄下,建立資料夾和檔案。

getExternalFilesDirs("QY-N1")是尋找Android/data/<應用包名>/files路徑,"QY-N1"是子目錄的名稱,如果沒有找到呼叫之後會自動生成該目錄
getExternalFilesDirs("QY-N1"是尋找Android/data/<應用包名>/files路徑,"QY-N1"是子目錄的名稱,如果沒有找到不會主動建立目錄
 File file = getExternalFilesDir("QY-N1");
 System.out.println("路徑是"+file.getPath());
 if(file.exists())
 {
   System.out.println("檔案已經存在");
 }
 else
{ file.mkdirs();}
 String p = file.getPath()+File.separator+"QY-N1-13-41.xml";
 System.out.println("檔名稱"+p);

在java中新建一個類,進行簡單的封裝

public class FileService   {
    //在應用資料夾下建立資料夾
public File CreatFolderInAPP(String path, String FolderName){
        String status = Environment.getExternalStorageState
(); if (status.equals(Environment.MEDIA_MOUNTED)) { System.out.println("SD存在"); File file = new File(path,FolderName); if(file.exists()) { System.out.println("檔案已經存在"); } else { file.mkdirs();} return
file; } else { System.out.println("沒有發現SD卡"); } return null; } public String CreatFileInAPP(File Folder, String FilserName){ String file = Folder.getPath()+Folder.separator+FilserName; return file; } public FileService() { System.out.println("建立了一個FileService物件"); } }