实例介绍
【实例简介】轻量的文件操作方法工具类
【实例截图】
【实例截图】
【核心代码】
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); } }
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论