FileUtil

直接上代碼看Logandroid

    private void getTest() {
        File tt;
        String ttt;
        tt=Environment.getRootDirectory();
        Log.e("android根目錄:",tt.toString());
        tt=Environment.getDataDirectory();
        Log.e("android數據目錄:",tt.toString());
        tt=Environment.getDownloadCacheDirectory();
        Log.e("android下載緩存目錄:",tt.toString());
        tt=Environment.getExternalStorageDirectory();
        Log.e("android SD卡目錄:",tt.toString());
        ttt=Environment.getExternalStorageState();
        Log.e("外部存儲設備的當前狀態",ttt);
    }


 E/android根目錄:: /system
 E/android數據目錄:: /data
 E/android下載緩存目錄:: /cache
 E/android SD卡目錄:: /storage/emulated/0
 E/外部存儲設備的當前狀態: mounted

  

再來看看經常使用的方法緩存

/**
     * 建立文件
     */
    public File createFile(String fileName) throws IOException {
        String SDPATH = Environment.getExternalStorageDirectory() + "/";
        File file = new File(SDPATH + fileName);
        file.createNewFile();
        return file;
    }

    /**
     * 建立目錄
     */
    public File createDir(String fileName) throws IOException {
        String SDPATH = Environment.getExternalStorageDirectory() + "/";
        File dir = new File(SDPATH + fileName);
        dir.mkdir();
        return dir;
    }

    /**
     * 修改文件夾和文件名
     */
    public File renameFile(File file, File newFile) {
        file.renameTo(newFile);
        return file;
    }

    /**
     * 刪除文件或文件夾
     */
    public void deleteFile(File file) {
        file.delete();
    }

    /**
     * 獲取文件或文件夾的相對路徑
     */
    public String getFilePath(File file) {
        String path = file.getPath();
        return path;
    }

    /**
     * 獲取文件或文件夾的絕對路徑
     */
    public String getFileAbsolutePath(File file) {
        String path = file.getAbsolutePath();
        return path;
    }

    /**
     * 獲取文件或文件夾的名稱
     */
    public String getFileName(File file) {
        String fileName = file.getName();
        return fileName;
    }

    /**
     * 獲取文件或文件夾的父目錄
     */
    public String getFileParentPath(File file) {
        String parentPath = file.getParent();
        return parentPath;
    }

    /**
     * 是不是文件
     */
    public boolean hasFile(File file) {
        return file.isFile();
    }

    /**
     * 是不是文件夾
     */
    public boolean hasDirectory(File file) {
        return file.isDirectory();
    }

    /**
     * 列出文件夾下的全部文件和文件夾名
     */
    public File[] getAllFile(File file) {
        return file.listFiles();
    }

  總結,File根本不須要工具類,直接file.就可了,用工具類是多此一舉。工具

上面工具類知識爲了記錄有哪些經常使用方法blog

 

================補充================get

root.getCanonicalPath()是獲取標準路徑
相關文章
相關標籤/搜索