• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

JavaSE文件操作工具类FileUtil详解

java 搞代码 4年前 (2022-01-05) 39次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了JavaSE系列之文件操作工具类FileUtil,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了JavaSE文件操作工具类FileUtil的具体代码,供大家参考,具体内容如下

先展示一下文件工具类中打印文件夹树形结构的结果:

代码如下:

 package com.mjq.iotest; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 练习File API * @author Administrator * */ public class FileUtil { /** * 单例模式 *//* private static FileUtil instance = null; private FileUtil() { } public static FileUtil getInstance() { synchronized (FileUtil.class) { if(null == instance) { instance = new FileUtil(); } } return instance; }*/ /** * 创建文件/文件夹 * @param path 路径 * @return 是否创建成功 * @throws Exception */ public static boolean creatFile(String path) throws Exception { if(null == path || path.length() == 0) { throw new Exception("路径不正确!"); } File file = new File(path); //如果路径存在,则不创建 if(!file.exists()) { if(file.isDirectory()) { //文件夹 file.mkdirs(); // file.mkdir();  创建单层路径 file.mkdirs() 可以创建多层路径 return true; } else { //文件  先创建父路径,然后再创建文件 String dirPath = path.substring(0,path.lastIndexOf(File.separator)); File dirFile = new File(dirPath); if(!dirFile.exists()) { dirFile.mkdirs(); } File fileFile = new File(path); try { fileFile.createNewFile(); return true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } else { throw new Exception("文件已存在!"); } return false; } /** * 删除文件/文件夹及其中的所有子文件夹和文件 * @return * @throws Exception */ public static void deleteFile(String filePath) throws Exception { if(null == filePath || filePath.length() == 0) { throw new Exception("filePath:"+filePath+"路径不正确!"); } File file = new File(filePath); if(!file.exists()) { throw new Exception("filePath:"+filePath+"文件不存在!"); } if(file.isFile()) { file.delete(); } if(file.isDirectory()) { File [] childFiles = file.listFiles(); if(null != childFiles && childFiles.length!=0) { //循环递归删除 for(File childFile:childFiles) { deleteFile(childFile.getAbsolutePath()); } } file.delete(); } } /** * 获取文件基本信息 * @param file */ public static void getBaseInfo(File file) { //文件绝对路径  文件大小  文件是否是文件夹   文件是否是文件   文件是否可读   文件是否可写      文件是否可执行     文件修改时间    文件父目录名 //文件所在分区总大小  未使用大小  可用大小 System.out.println("文件基本信息如下:"); System.out.println("文件绝对路径:"+file.getAbsolutePath()); System.out.println("文件名称:"+file.getName()); System.out.println("文件大小:"+file.length()); System.out.println("文件是否是文件夹:"+file.isDirectory()); Syste<strong style="color:transparent">来源gaodai#ma#com搞@@代~&码网</strong>m.out.println("文件是否是文件:"+file.isFile()); System.out.println("文件是否可读:"+file.canExecute()); System.out.println("文件是否可读:"+file.canRead()); System.out.println("文件是否可写:"+file.canWrite()); System.out.println("文件修改时间:"+file.lastModified()); System.out.println("文件父目录名称:"+file.getParent()); System.out.println("文件所在分区大小:"+file.getTotalSpace()/1024/1024+"Mb"); System.out.println("文件所在分区未使用大小:"+file.getFreeSpace()/1024/1024+"Mb"); System.out.println("文件所在分区可用大小:"+file.getUsableSpace()/1024/1024+"Mb"); System.out.println("文件夹结构如下图:"); try { printFileStructure(file,1); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 打印文件路径 * @param file * @param deepth * @throws Exception */ public static void printFileStructure(File file ,int deepth) throws Exception { if(!file.exists()) { throw new Exception("文件路径不存在!"); } if(!file.isHidden()) { if(file.isFile()) { //直接打印 printFile(file,deepth); return; } if(file.isDirectory()) { //先打印自身,然后递归打印子文件夹和子文件 printFile(file,deepth); File [] childFiles = file.listFiles(); if(null != childFiles && childFiles.length>0) { deepth++; for(File childFile : childFiles) { printFileStructure(childFile ,deepth); } } } } } /** * 打印文件夹树形结构 * @param file * @param deepth */ public static void printFile(File file ,int deepth) { String name = file.getName(); StringBuffer sb = new StringBuffer(); StringBuffer tempSb = new StringBuffer(); for(int i =0;i name.endsWith(".java") || new File(name).isDirectory()); for(String name : nameList) { System.out.println(name); } ======================================================================================== 这里使用Lamda表达式实现FilenameFilter 接口种的accept()方法 */ File[] fileList = file.listFiles(new FilenameFilter(){ /** * 使用正则表达式进行匹配 * @param regexStr * @return */ private boolean regexMatch(String name,String regexStr) { Pattern pattern = Pattern.compile(regexStr); Matcher matcher = pattern.matcher(name); return matcher.find(); } @Override public boolean accept(File dir, String name) { return regexMatch(name,regex); }}); if(null != fileList && fileList.length>0) { for(File filteredFile: fileList) { filteredFile.delete(); } } } /** * 复制文件/文件夹及其中的所有子文件夹和文件 * @return */ public static void copyFile(String srcFilePath,String destFilePath) { InputStream is = null; OutputStream os = null; try { if(creatFile(destFilePath)) { File srcFile = new File(srcFilePath); File destFile = new File(destFilePath); is = new FileInputStream(srcFile); os = new FileOutputStream(destFile); byte [] buffer = new byte[2048]; int temp = 0; while((temp = is.read(buffer))!=-1) { os.write(buffer, 0, temp); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { //java 7 以后可以不关闭,可以自动关闭 if(null != os) { try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(null != is) { try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } /** * 复制指定地文件 * @return *//* public static boolean copyNamedFile() { } *//** * 剪切文件/文件夹及其中的所有子文件夹和文件 * @return */ public static void cutFile(String srcFilePath,String destFilePath) { //先复制,再删除 try { copyFile( srcFilePath, destFilePath); deleteFile(srcFilePath); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 剪切文件/文件夹及其中的所有子文件夹和文件 * @return *//* public static boolean cutNamedFile() { } *//** * 文件压缩 * @param destPath * @param fileFormats * @param srcFile * @return *//* public static boolean fileCompress(String destPath,String fileFormats,String srcFile,String regex) { } *//** * 文件解压缩 * @param destPath * @param srcFile * @return *//* public static boolean fileDecompress(String destPath,String srcPath) { } *//** * 文件加密 * @param srcPath * @param destPath * @param encryptKey * @param encryptAlgorithm * @return *//* public static boolean fileEncrypt(String srcPath,String destPath,String encryptKey,String encryptAlgorithm) { } *//** * 文件解密 * @param srcPath * @param destPath * @param encryptKey * @param encryptAlgorithm * @return *//* public static boolean fileDecrypt(String srcPath,String destPath,String encryptKey,String encryptAlgorithm) { }*/ public static void main(String [] args) { File file = new File("D:\\北邮人下载\\书籍\\编译原理"); getBaseInfo(file); try { /*deleteNamedFile("D:\\北邮人下载\\书籍",".pdf");*/ } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } /*cutFile("F:\\抢票软件\\12306Bypass.exe","F:\\抢票软件\\12306Bypass\\12306Bypass.exe");*/ } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是JavaSE文件操作工具类FileUtil详解的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:JavaSE文件操作工具类FileUtil详解
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址