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()是获取标准路径
相关文章
相关标签/搜索