Java 文件的增删改查


一、简介

1、RuntimeException运行时异常、受检异常(Exception)、系统错误error。
2、RuntimeException运行时异常,表示代码本身存在BUG,常见的有空指针异常(NullPointerException )。
3、非RuntimeException,就是受检异常。比如处理文件流时的I/O问题,就属于编译时异常,相当于假设有IO异常就利用try-catch对其进行处理,或者 throws即可
4、error系统不可控错误,与程序无关

二、代码

01、获取文件类型

/**
 * 获取文件类型
 *
 * @param fileName [文件名称]
 * @return 
 */
public static String getFileType(String fileName) {
    if (null == fileName) {
        throw new RuntimeException("文件名称为空,请输入正确的文件名称");
    }

    return fileName.substring(fileName.lastIndexOf(".") + 1);
}

02、判断是否为图片

/**
 * 判断是否为图片
 *
 * @param fileName [文件名称]
 * @return
 */
public static boolean isImage(String fileName) {
    if (null == fileName) {
        return false;
    }

    String fileType = getFileType(fileName);    // 获取文件类型
    String[] imgArr = {"jpg", "png", "jpeg", "ico", "gif"};

    for (String s : imgArr) {
        if (s.equals(fileType)) {
            return true;
        }
    }
    return false;
}

03、递归创建文件夹

/**
 * 递归创建文件夹
 *
 * @param filePath [文件绝对路径]
 * @return
 */
public static boolean createDir(String filePath) {
    File file = new File(filePath);

    if (!file.getParentFile().exists()) {   // 不存在就创建文件夹
        createDir(file.getParentFile().getPath());
    }

    return file.mkdir();
}

04、判断文件是否存在

/**
 * 判断文件是否存在
 *
 * @param fileName [要检查文件的绝对路径]
 * @return 
 */
public static boolean isExists(String fileName) {
    if (null == fileName) {
        return false;
    }

    File file = new File(fileName);
    return file.exists();
}

05、创建一个新文件

/**
 * 创建一个新文件
 *
 * @param fileName  [需要的文件绝对路径]
 * @param isReplace [是否覆盖原文件创建]
 * @return
 */
public static boolean createFile(String fileName, boolean isReplace) {
    if (null == fileName) {
        return false;
    }

    boolean r = false;
    try {
        File file = new File(fileName);
        if (!file.exists() && !isReplace) {
            r = file.createNewFile();
        } else if (isReplace) { // 删除后,再创建
            r = file.delete();
            r = file.createNewFile();
        }
    } catch (Exception e) {
        e.getStackTrace();
    }
    return r;
}

06、删除文件

/**
 * 删除文件
 *
 * @param fileName [文件的绝对路径]
 * @return
 */
public static boolean delFile(String fileName) {
    if (null == fileName) {
        return false;
    }

    File file = new File(fileName);
    return file.delete();
}

07、重命名文件

/**
 * 重命名文件
 *
 * @param srcFileName [源文件路径]
 * @param desFileName [目标文件路径]
 * @return
 */
public static boolean renameTo(String srcFileName, String desFileName) {
    if (null == srcFileName || null == desFileName) {
        return false;
    }

    File srcFile = new File(srcFileName);
    File desFile = new File(desFileName);
    boolean r = false;
    if (srcFile.exists()) {
        r = srcFile.renameTo(desFile);
    }
    return r;
}

08、文件拷贝

/**
 * 文件拷贝
 *
 * @param srcFile   [源文件路径]
 * @param desFile   [目标文件路径]
 * @param isReplace [是否覆盖]
 * @return
 */
public static boolean copyFile(String srcFile, String desFile, boolean isReplace) {
    if (isExists(desFile)) {
        if (isReplace) {    // 如果替换文件,先删除
            delFile(desFile);
        } else {
            return true;
        }
    }

    InputStream inputStream = null;
    FileOutputStream fileOutputStream = null;
    boolean r = false;
    try {
        inputStream = new FileInputStream(srcFile);
        fileOutputStream = new FileOutputStream(desFile);
        byte[] bytes = new byte[1444];
        int byteRead;
        while ((byteRead = inputStream.read(bytes)) != -1) {
            fileOutputStream.write(bytes, 0, byteRead);
        }
        r = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (null != inputStream) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (null != fileOutputStream) {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return r;
}

09、获取指定目录下的所有文件信息(不包含文件夹)

/**
 * 获取指定目录下的所有文件信息(不包含文件夹)
 *
 * @param path [指定目录]
 * @return
 */
public static List<String> getFiles(String path) {
    File file = new File(path);
    File[] files = file.listFiles();

    int count = files != null ? files.length : 0;
    List<String> r = new ArrayList<>(count);
    for (int i = 0; i < count; i++) {
        if (files[i].isFile()) {
            if (files[i].isFile()) {
                r.add(files[i].getName());
            }
        }
    }
    return r;
}

10、获取文件大小,单位KB

/**
 * 获取文件大小,单位kb
 *
 * @param fileName [文件名称]
 * @return
 */
public static long getFileSize(String fileName) {
    if (null == fileName) {
        return 0L;
    }

    File file = new File(fileName);
    long r = 0L;
    if (file.exists() && file.isFile()) {
        return file.length() / 1024;
    }
    return r;
}

原文链接: https://mp.weixin.qq.com/s/e_LOAj4WkNRwLtCVAXu0Gw

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Little Tomato

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值