基本信息
源码名称:常见的文件操作方法工具类(删除、重命名、复制等等)
源码大小:1.73KB
文件格式:.zip
开发语言:Java
更新时间:2019-10-30
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 1 元 
   源码介绍
轻量的文件操作方法工具类

package com.example.common.utils;

import java.io.*;

/**
 * @author 笑笑
 */
public class FileUtil {

    /**
     * 功能描述: 新建文件
     * @param: path
     * @return:
     */
    public static boolean createFile(String path) throws IOException {
        File newFile = new File(path);
        // 判断是否有这个文件有不管没有创建
        if (!newFile.exists()) {
            newFile.createNewFile();
        }
        return true;
    }


    /**
     * 功能描述: 新建文件夹
     * @param: path
     * @return:
     */
    public static boolean createFolder(String path) {
        File newFile = new File(path);
        // 判断是否有这个文件有不管没有创建
        if (!newFile.exists()) {
            newFile.mkdirs();
        }
        return true;
    }

    /**
     * 复制单个文件
     * @param oldPath
     * @param newPath
     * @throws IOException
     */
    public static boolean copyFile(String oldPath, String newPath) throws IOException {
        File oldFile = new File(oldPath);
        if (!oldFile.exists())
            return false;

        File newFile = new File(newPath);
        // 判断是否有这个文件有不管没有创建
        if (!newFile.exists()) {
            newFile.mkdirs();
        }
        fileCopy(oldFile.getPath(),
                newPath   File.separator   oldFile.getName());// 继续调用复制方法
        // 递归的地方,自己调用自己的方法,就可以复制文件夹的文件夹了
        return true;

    }


    /**
     * 复制文件或者文件夹下的所有内容
     * @param oldPath
     * @param newPath
     * @throws IOException
     */
    public static boolean copyFolder(String oldPath, String newPath) throws IOException {

        File oldFile = new File(oldPath);
        if (!oldFile.exists())
            return false;

        File newFile = new File(newPath);
        // 判断是否有这个文件有不管没有创建
        if (!newFile.exists()) {
            newFile.mkdirs();
        }

        // 遍历文件及文件夹
        for (File file : oldFile.listFiles()) {
            if (file.isFile()) {
                // 文件
                fileCopy(file.getPath(), newPath   File.separator   file.getName()); // 调用文件拷贝的方法
            } else if (file.isDirectory()) {
                // 文件夹
                copyFolder(file.getPath(), newPath   File.separator   file.getName());// 继续调用复制方法
                // 递归的地方,自己调用自己的方法,就可以复制文件夹的文件夹了
            }
        }

        return true;
    }

    /**
     * 复制文件的实际方法体
     * @param src
     * @param des
     * @throws IOException
     */
    private static void fileCopy(String src,String des) throws IOException {
        //io流固定格式
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(des));
        int i = -1;//记录获取长度
        byte[] bt = new byte[2014];//缓冲区
        while ((i = bis.read(bt))!=-1) {
            bos.write(bt, 0, i);
        }
        bis.close();
        bos.close();
        //关闭流
    }

    /**
     * 删除文件或者文件夹下的所有内容
     * @param src
     */
    public static void delFile(String src){
        File file = new File(src);
        if(!file.exists()) return;
        delDir(file);
    }

    /**
     * 递归删除文件
     * @param f
     */
    private static void delDir(File f) {
        // 判断是否是一个目录, 不是的话跳过, 直接删除; 如果是一个目录, 先将其内容清空.
        if(f.isDirectory()) {
            // 获取子文件/目录
            File[] subFiles = f.listFiles();
            // 遍历该目录
            for (File subFile : subFiles) {
                // 递归调用删除该文件: 如果这是一个空目录或文件, 一次递归就可删除. 如果这是一个非空目录, 多次
                // 递归清空其内容后再删除
                delDir(subFile);
            }
        }
        // 删除空目录或文件
        f.delete();
    }

    /**
     * 往文件中写入内容,append 为 true 则不覆盖原内容, false 则覆盖原内容
     * @param src
     * @param content
     * @param append
     */
    public static void writeFile(File src, String content, boolean append) throws Exception {
        try {
            FileWriter fw = new FileWriter(src, append);
            fw.write(content);
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * 功能描述: 重命名文件
     *
     * @param: src 文件路径 newName 新文件名
     * @return:
     * @auther: 笑笑
     * @date: 2018/12/11 9:59
     */
    public static void renameFile(String basePath, String newName, String oldName){
//        System.out.println("基础路径"   basePath);
        File oldfile = new File(basePath   oldName);
        File newfile = new File(basePath   newName);
        if(!oldfile.exists()){
            return;//重命名文件不存在
        }
        if(newfile.exists()){
            newfile.delete(); // 若在该目录下已经有一个文件和新文件名相同,则删除
        }
        oldfile.renameTo(newfile);
    }
}