zoukankan      html  css  js  c++  java
  • FileUtil

    直接上代码看Log

        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.就可了,用工具类是画蛇添足。

    上面工具类知识为了记录有哪些常用方法

    ================补充================

    root.getCanonicalPath()是获取标准路径
  • 相关阅读:
    Windows7 共享文件夹的两个BUG
    POJ 1845 Sumdiv(数论,求A^B的所有约数和)
    POJ 2481 Cows(树状数组)
    HDU 1124 Factorial(简单数论)
    POJ 1195 Mobile phones(二维树状数组)
    POJ 3067 Japan(树状数组求逆序对)
    HDU 4027 Can you answer these queries?(线段树)
    HDU 1576 A/B(数论简单题,求逆元)
    HDU 1166 敌兵布阵(线段树,树状数组)
    ZOJ 1610 Count the Colors(线段树)
  • 原文地址:https://www.cnblogs.com/wabi87547568/p/5371705.html
Copyright © 2011-2022 走看看