zoukankan      html  css  js  c++  java
  • Java操作文件Util

      1 package io.guangsoft.utils;
      2 
      3 import java.io.File;
      4 import java.io.FileInputStream;
      5 import java.io.FileOutputStream;
      6 import java.io.IOException;
      7 import java.nio.channels.FileChannel;
      8 import java.util.ArrayList;
      9 import java.util.List;
     10 
     11 public class FilesUtil {
     12 
     13     //判断文件或文件夹是否存在
     14     public static boolean isExit(String fileName) {
     15         if (StringUtils.isNotEmpty(fileName)) {
     16             File oldfile = new File(fileName);
     17             if (oldfile.exists()) {
     18                 return true;
     19             }
     20         }
     21         return false;
     22     }
     23 
     24     //NIO方式复制文件
     25     public static void copyFileNIO(File originFile, File destFile) {
     26         File destDir = destFile.getParentFile();
     27         if (!destDir.exists()) {
     28             destDir.mkdirs();
     29         }
     30         FileInputStream fi = null;
     31         FileOutputStream fo = null;
     32         FileChannel in = null;
     33         FileChannel out = null;
     34         try {
     35             fi = new FileInputStream(originFile);
     36             fo = new FileOutputStream(destFile);
     37             in = fi.getChannel();// 得到对应的文件通道
     38             out = fo.getChannel();// 得到对应的文件通道
     39             in.transferTo(0, in.size(), out);// 连接两个通道,并且从in通道读取,然后写入out通道
     40         } catch (IOException e) {
     41             e.printStackTrace();
     42         } finally {
     43             try {
     44                 fi.close();
     45                 in.close();
     46                 fo.close();
     47                 out.close();
     48             } catch (IOException e) {
     49                 e.printStackTrace();
     50             }
     51         }
     52     }
     53 
     54     //根据文件路径复制文件
     55     public static void copyFile(String oldPath, String newPath) {
     56         try {
     57             File oldfile = new File(oldPath);
     58             if (oldfile.exists()) {
     59                 File destFile = new File(newPath);
     60                 copyFileNIO(oldfile, destFile);
     61             }
     62         } catch (Exception e) {
     63             System.out.println("复制单个文件操作出错");
     64             e.printStackTrace();
     65         }
     66 
     67     }
     68 
     69     //根据文件路径删除文件
     70     public static void delFile(String filePathAndName) {
     71         try {
     72             String filePath = filePathAndName;
     73             filePath = filePath.toString();
     74             File myDelFile = new File(filePath);
     75             myDelFile.delete();
     76         } catch (Exception e) {
     77             System.out.println("删除文件操作出错");
     78             e.printStackTrace();
     79         }
     80 
     81     }
     82 
     83     //移动文件
     84     public static void moveFile(String oldPath, String newPath) {
     85         copyFile(oldPath, newPath);
     86         delFile(oldPath);
     87     }
     88 
     89     //复制整个文件夹到指定目录
     90     public static void copy(String src, String des) {
     91         File file1 = new File(src);
     92         String desDir = src.substring(src.lastIndexOf("\") + 1, src.length());
     93         File[] fs = file1.listFiles();
     94         File file2 = new File(des + desDir);
     95         if (!file2.exists()) {
     96             file2.mkdirs();
     97         }
     98         if (fs != null) {
     99             for (File f : fs) {
    100                 copyFile(f.getPath(), des + desDir + "\" + f.getName()); // 调用文件拷贝的方法
    101             }
    102         }
    103     }
    104 
    105     //递归删除目录下的所有文件及子目录下所有文件
    106     public static boolean deleteDir(File dir) {
    107         boolean ret = true;
    108         if (dir.isDirectory()) {
    109             String[] children = dir.list();
    110             // 递归删除目录中的子目录
    111             if (children != null) {
    112                 for (int i = 0; i < children.length; i++) {
    113                     boolean success = deleteDir(new File(dir, children[i]));
    114                     if (!success) {
    115                         return false;
    116                     }
    117                 }
    118             }
    119         }
    120         // 目录此时为空,可以删除
    121         try{
    122             ret = dir.delete();
    123             if(!ret){
    124                 
    125                 System.out.println("删除失败:"+dir.getName());
    126                 System.gc();//系统进行资源强制回收
    127                 ret = dir.delete();
    128             }
    129         }catch(Exception ex){
    130             ret = false;
    131         }finally{
    132             
    133         }
    134         return ret;
    135     }
    136 
    137     //获取文件夹下所有文件路径
    138     public static List<String> getFilePathList(String dirPath) {
    139         List<String> filePathlist = new ArrayList<String>();
    140         File dir = new File(dirPath);
    141         File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
    142         if (files != null) {
    143             for (int i = 0; i < files.length; i++) {
    144                 if (files[i].isDirectory()) { // 判断是文件还是文件夹
    145                     getFilePathList(files[i].getAbsolutePath()); // 获取文件绝对路径
    146                 } else {
    147                     String strFileName = files[i].getAbsolutePath();
    148                     filePathlist.add(strFileName);
    149                 }
    150             }
    151         }
    152         return filePathlist;
    153     }
    154     
    155 }
  • 相关阅读:
    MySQL/MariaDB/Percona数据库升级脚本
    systemd详解
    Nginx下Redmine2.6配置
    Linux下Python获取IP地址
    浅谈Linux内存管理机制
    深入理解PHP Opcode缓存原理
    Varnish – 高性能http加速器
    问题记录
    Java 排序报错
    记录一次数据库链接不够用被drop掉的问题
  • 原文地址:https://www.cnblogs.com/guanghe/p/10484861.html
Copyright © 2011-2022 走看看