美文网首页Android知识半栈工程师Android开发探索
Android File(文件/目录)常见操作的封装:创建文件、

Android File(文件/目录)常见操作的封装:创建文件、

作者: Android技术分享 | 来源:发表于2018-05-23 17:08 被阅读153次

项目中很多地方用到了文件IO操作,
最好对这些常用的操作进行统一封装,而非让文件IO代码散落在项目各模块代码中。


创建文件

需求:
-- 在指定目录创建文件,
-- 先判断目录是否存在,如果不存在,则递归创建,
-- 再判断文件是否存在,如果不存在,则创建
-- 最后,返回文件对象
方法传参:目标路径、文件名称

public File newFile(String filePath, String fileName){
        if(filePath == null || filePath.length() == 0 
         || fileName == null || fileName.length() == 0){
            return null;
        }
        try {
            //判断目录是否存在,如果不存在,递归创建目录
            File dir = new File(filePath);
            if(!dir.exists()){
                dir.mkdirs();
            }

            //组织文件路径
            StringBuilder sbFile = new StringBuilder(filePath);
            if(!filePath.endsWith("/")){
                sbFile.append("/");
            }
            sbFile.append(fileName);

            //创建文件并返回文件对象
            File file = new File(sbFile.toString());
            if(!file.exists()){
                file.createNewFile();
            }
            return file;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return null;
    }

删除文件或目录

需求:
-- 可删除单个文件,也可删除整个目录,
-- 先判断File类型是文件还是目录类型,
-- 如果是文件,则直接删除
-- 如果是目录,则获得目录信息,递归删除文件及文件夹
方法传参:目标路径(文件或目录路径)

    public void removeFile(String filePath) {
        if(filePath == null || filePath.length() == 0){
            return;
        }
        try {
            File file = new File(filePath);
            if(file.exists()){
                removeFile(file);
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

   private void removeFile(File file){
        //如果是文件直接删除
        if(file.isFile()){
            file.delete();
            return;
        }
        //如果是目录,递归判断,如果是空目录,直接删除,如果是文件,遍历删除
        if(file.isDirectory()){
            File[] childFile = file.listFiles();
            if(childFile == null || childFile.length == 0){
                file.delete();
                return;
            }
            for(File f : childFile){
                removeFile(f);
            }
            file.delete();
        }
    }

获得文件或目录大小(size)

需求:
-- 可获得单个文件大小,也可获得目录大小,
-- 先判断File类型是文件还是目录类型,
-- 如果是文件,则直接获取大小并返回
-- 如果是目录,递归获得各文件大小累加,然后返回
方法传参:目标路径(文件或目录路径)

    float size = 0;
    public float getFileSize(String filePath) {
        if(filePath == null || filePath.length() == 0){
            return 0;
        }
        try {
            File file = new File(filePath);
            if(file.exists()){
                size = 0;
                return getSize(file);
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return 0;
    }

    private float getSize(File file) {
        try {
            //如果是目录则递归计算其内容的总大小
            if (file.isDirectory()) {
                File[] children = file.listFiles();
                for (File f : children) {
                    size += getSize(f);
                }
                return size;
            }
            //如果是文件则直接返回其大小
            else {
                return (float) file.length();
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return size;
    }

拷贝文件

需求:
-- 将文件拷贝到指定目录
-- 如果目录不存在,则递归创建
-- 拷贝文件
方法传参:文件路径、拷贝目标路径

public void copyFile(String filePath, String newDirPath) {
        if(filePath == null || filePath.length() == 0){
            return;
        }
        try {
            File file = new File(filePath);
            if(!file.exists()){
                return;
            }
            //判断目录是否存在,如果不存在,则创建
            File newDir = new File(newDirPath);
            if(!newDir.exists()){
                newDir.mkdirs();
            }
            //创建目标文件
            File newFile = newFile(context, newDirPath, file.getName());
            InputStream is = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(newFile);
            byte[] buffer = new byte[4096];
            int byteCount = 0;
            while ((byteCount = is.read(buffer)) != -1) {
                fos.write(buffer, 0, byteCount);
            }
            fos.flush();
            is.close();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

拷贝目录

需求:
-- 将整个目录拷贝到指定目录
-- 如果目录不存在,则递归创建
-- 递归拷贝文件
方法传参:源目录路径、拷贝目标路径

public void copyDir(String dirPath, String newDirPath) {
        if(dirPath == null || dirPath.length() == 0 
          || newDirPath==null || newDirPath.length() == 0){
            return;
        }
        try {
            File file = new File(dirPath);
            if(!file.exists() && !file.isDirectory()){
                return;
            }
            File[] childFile = file.listFiles();
            if(childFile == null || childFile.length == 0){
                return;
            }
            File newFile = new File(newDirPath);
            newFile.mkdirs();
            for (File fileTemp : childFile) {
                if(fileTemp.isDirectory()){
                    copyDir(fileTemp.getPath(), newDirPath + "/" + fileTemp.getName());
                }else {
                    copyFile(fileTemp.getPath(), newDirPath);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

剪切文件

需求:
-- 将文件剪切到指定目录
-- 如果目录不存在,则递归创建
-- 拷贝文件,删除源文件
方法传参:源文件路径、拷贝目标路径

    public void moveFile(String filePath, String newDirPath) {
        if(filePath == null || filePath.length() == 0 
        || newDirPath==null || newDirPath.length() == 0){
            return;
        }
        try {
            //拷贝文件
            copyFile(filePath, newDirPath);
            //删除原文件
            removeFile(filePath);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

剪切目录

需求:
-- 将目录剪切到指定目录
-- 如果目录不存在,则递归创建
-- 递归拷贝文件,删除源目录
方法传参:源目录路径、拷贝目标路径

      public void moveDir(String dirPath, String newDirPath) {
        if(dirPath == null || dirPath.length() == 0 
        || newDirPath==null || newDirPath.length() == 0){
            return;
        }
        try {
            //拷贝目录
            copyDir(dirPath, newDirPath);
            //删除目录
            removeFile(dirPath);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

其他操作

文件路径的分类使用,请参考https://www.jianshu.com/p/0515df3b697d


以上代码经过测试,如果调用还有问题,请检查APP权限或自行优化。

相关文章

  • Android File(文件/目录)常见操作的封装:创建文件、

    项目中很多地方用到了文件IO操作,最好对这些常用的操作进行统一封装,而非让文件IO代码散落在项目各模块代码中。 创...

  • linux简单操作

    文件以及目录基本操作 mkdir dir //创建目录touch file | >file //创建 文件 cp ...

  • Python shutil模块

    目录操作: os.mkdir("file") 创建目录 复制文件: shutil.copyfil...

  • laravel File

    File 文件上传 文件目录操作

  • Java中创建文件/目录删除文件/目录方法(File类)

    File:它是文件和目录路径名的抽象表示 文件和目录是可以通过File封装成对象的 对于File而言,其封装的并不...

  • ansible核心模块之file

    file模块 帮助我们完成一些对文件的基本操作,比如,创建文件或目录、删除文件或目录、修改文件权限等 常用参数 (...

  • IO输入/输出流(一)

    前言: Java中对数据进行持久化操作 File类的概述和作用: java中把文件或目录(文件夹)都封装成File...

  • Java--File I/O

    java.io.File类用于表示文件(目录)。用来将文件或者文件夹封装成对象。File类只用于表示文件(目录)的...

  • File

    File:文件和目录路径名的抽象表示。 (该文件或目录并不一定真的存在) 1、创建操作1)创建文件夹 mkdir:...

  • Java File类

    File类 1.构造方法 构造方法主要是用来封装文件名和目录名,不会创建文件和文件夹 2.创建方法 创建文件一般思...

网友评论

  • 街道shu记:删除文件,如果是子文件是文件夹,直接删除不就行了,为什么还要遍历里面的文件,一个个删除?

本文标题:Android File(文件/目录)常见操作的封装:创建文件、

本文链接:https://www.haomeiwen.com/subject/hlorjftx.html