java文件操作的工具类

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

为了修改大量收藏的美剧文件名,用swing写了个小工具,代码是文件处理部分(netbeans的拖拽工具用起来挺麻烦的)
/*
 * To change this license header,choose License Headers in Project Properties.
 * To change this template file,choose Tools | Templates
 * and open the template in the editor.
 */
package datei.steuern;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author s.watson
 */
public class FileTools {

  public FileTools() {

  }

  /**
   * formatPath 转义文件目录
   *
   * @param path
   * @return
   */
  public static String formatPath(String path) {
    return path.replaceAll("\\\\","/");
  }

  /**
   * combainPath文件路径合并
   *
   * @param eins
   * @param zwei
   * @return
   */
  private static String combainPath(String eins,String zwei) {
    String dori = "";
    eins = null == eins ? "" : formatPath(eins);
    zwei = null == zwei ? "" : formatPath(zwei);
    if (!eins.endsWith("/") && zwei.indexOf("/") != 0) {
      dori = eins + "/" + zwei;
    } else {
      dori = (eins + zwei).replaceAll("//","/");
    }
    return dori;
  }

  /**
   * list2Array 列表转换数组
   *
   * @param list
   * @return
   */
  private static String[] list2Array(List list) {
    String array[] = (String[]) list.toArray(new String[list.size()]);
    return array;
  }

  /**
   * cp 复制文件
   *
   * @param source
   * @param destination
   * @param loop
   * @return
   */
  public static List<File> cp(String source,String destination,boolean loop) {
    List<File> list = new ArrayList();
    try {
      File srcFile = new File(source);
      File desFile = new File(destination);
      list.addAll(cp(srcFile,desFile,loop));
    } catch (Exception ex) {
      j2log(null,null,ex);
    }
    return list;
  }

  /**
   * cp 复制文件
   *
   * @param source
   * @param destination
   * @param loop
   * @return
   */
  public static List<File> cp(File source,File destination,boolean loop) {
    List<File> list = new ArrayList();
    try {
      if (!source.exists() || source.isDirectory()) {
        throw new FileNotFoundException();
      }
      list.add(cp(source,destination));
      if (loop) {
        String[] subFile = source.list();
        for (String subPath : subFile) {
          String src = combainPath(source.getPath(),subPath);//子文件原文件路径
          String des = combainPath(destination.getPath(),subPath);//子文件目标路径
          File subfile = new File(src);
          if (subfile.isFile()) {
            list.add(cp(src,des));
          } else if (subfile.isDirectory() && loop) {
            list.addAll(cp(src,des,loop));
          }
        }
      }
    } catch (Exception ex) {
      j2log(null,ex);
    }
    return list;
  }

  /**
   * cp 单文件复制文件
   *
   * @param source
   * @param destination
   * @return
   */
  public static File cp(String source,String destination) {
    File desFile = null;
    try {
      File srcFile = new File(source);
      desFile = new File(destination);
      desFile = cp(srcFile,desFile);
    } catch (Exception ex) {
      j2log(null,ex);
    }
    return desFile;
  }

  /**
   * cp 单文件复制文件
   *
   * @param source
   * @param destination
   * @return
   */
  public static File cp(File source,File destination) {
    FileInputStream in = null;
    FileOutputStream out = null;
    FileChannel inc = null;
    FileChannel outc = null;
    try {
      if (!source.exists() || source.isDirectory()) {
        throw new FileNotFoundException();
      }
      if (source.getPath().equals(destination.getPath())) {
        return source;
      }
      long allbytes = du(source,false);
      if (!destination.exists()) {
        destination.createNewFile();
      }
      in = new FileInputStream(source.getPath());
      out = new FileOutputStream(destination);
      inc = in.getChannel();
      outc = out.getChannel();
      ByteBuffer byteBuffer = null;
      long length = 2097152;//基本大小,默认2M
      long _2M = 2097152;
      while (inc.position() < inc.size()) {
        if (allbytes > (64 * length)) {//如果文件大小大于128M 缓存改为64M
          length = 32 * _2M;
        } else if (allbytes > (32 * length)) {//如果文件大小大于64M 缓存改为32M
          length = 16 * _2M;
        } else if (allbytes > (16 * length)) {//如果文件大小大于32M 缓存改为16M
          length = 8 * _2M;
        } else if (allbytes > (8 * length)) {//如果文件大小大于16M 缓存改为8M
          length = 4 * _2M;
        } else if (allbytes > (4 * length)) {//如果文件大小大于8M 缓存改为4M
          length = 2 * _2M;
        } else if (allbytes > (2 * length)) {//如果文件大小大于4M 缓存改为2M
          length = _2M;
        } else if (allbytes > (length)) {//如果文件大小大于2M 缓存改为1M
          length = _2M / 2;
        } else if (allbytes < length) {//如果文件小于基本大小,直接输出
          length = allbytes;
        }
        allbytes = inc.size() - inc.position();
        byteBuffer = ByteBuffer.allocateDirect((int) length);
        inc.read(byteBuffer);
        byteBuffer.flip();
        outc.write(byteBuffer);
        outc.force(false);
      }
    } catch (Exception ex) {
      j2log(null,ex);
    } finally {
      try {
        if (null != inc) {
          inc.close();
        }
        if (null != outc) {
          outc.close();
        }
        if (null != in) {
          in.close();
        }
        if (null != out) {
          out.close();
        }
      } catch (Exception ex) {
        j2log(null,ex);
      }
    }
    return destination;
  }

  /**
   * rename 文件重命名
   *
   * @param filePath
   * @param from
   * @param to
   * @return
   */
  public static File rename(String filePath,String from,String to) {
    File newFile = null;
    try {
      File oldFile = new File(combainPath(filePath,from));
      newFile = new File(combainPath(filePath,to));
      rename(newFile,oldFile);
    } catch (Exception ex) {
      j2log(null,ex);
    }
    return newFile;
  }

  /**
   * rename 文件重命名
   *
   * @param to
   * @param from
   * @return
   */
  public static File rename(File from,File to) {
    try {
      String newPath = to.getPath();
      String oldPath = from.getPath();
      if (!oldPath.equals(newPath)) {
        if (!to.exists()) {
          from.renameTo(to);
        }
      }
    } catch (Exception ex) {
      j2log(null,ex);
    }
    return to;
  }

  /**
   * mv 移动文件
   *
   * @param fileName
   * @param source
   * @param destination
   * @param cover
   */
  public static void mv(String fileName,String source,boolean cover) {
    try {
      if (!source.equals(destination)) {
        File oldFile = new File(combainPath(source,fileName));
        File newFile = new File(combainPath(destination,fileName));
        mv(oldFile,newFile,cover);
      }
    } catch (Exception ex) {
      j2log(null,ex);
    }
  }

  /**
   * mv 移动文件
   *
   * @param source
   * @param destination
   * @param cover
   */
  public static void mv(String source,boolean cover) {
    try {
      if (!source.equals(destination)) {
        File oldFile = new File(source);
        File newFile = new File(destination);
        mv(oldFile,ex);
    }
  }

  /**
   * mv 移动文件
   *
   * @param source
   * @param destination
   * @param cover
   */
  public static void mv(File source,boolean cover) {
    try {
      if (!source.exists()) {
        throw new FileNotFoundException();
      }
      StringBuilder fileName = new StringBuilder(source.getName());
      if (!cover && source.getPath().equals(destination.getPath())) {
        if (fileName.indexOf(".") > 0) {
          fileName.insert(fileName.lastIndexOf("."),"_副本");
        } else {
          fileName.append("_副本");
        }
        cp(source,new File(combainPath(source.getParent(),fileName.toString())));
      } else {
        source.renameTo(destination);
      }
    } catch (Exception ex) {
      j2log(null,ex);
    }
  }

  /**
   * extensions 获取文件扩展名信息
   *
   * @param filePath
   * @param fileName
   * @return
   */
  private static String[] extensions(String filePath,String fileName) {
    String[] extension = {};
    try {
      String fullPath = combainPath(filePath,fileName);
      File file = new File(fullPath);
      extensions(file);
    } catch (Exception ex) {
      j2log(null,ex);
    }
    return extension;
  }

  /**
   * extensions 获取文件扩展名信息
   *
   * @param fullPath
   * @return
   */
  private static String[] extensions(String fullPath) {
    String[] extension = {};
    try {
      File file = new File(fullPath);
      extensions(file);
    } catch (Exception ex) {
      j2log(null,ex);
    }
    return extension;
  }

  /**
   * extensions 获取文件扩展名信息
   *
   * @param file
   * @return
   */
  private static String[] extensions(File file) {
    String[] extension = {};
    try {
      if (file.isFile()) {
        String fileName = file.getName();
        if (fileName.lastIndexOf(".") >= 0) {
          int lastIndex = fileName.lastIndexOf(".");
          extension[0] = String.valueOf(lastIndex);//扩展名的“.”的索引
          extension[1] = fileName.substring(lastIndex + 1);//扩展名
          extension[2] = fileName.substring(0,lastIndex);//文件名
        }
      }
    } catch (Exception ex) {
      j2log(null,ex);
    }
    return extension;
  }

  /**
   * ls 遍历文件
   *
   * @param filePath
   * @param loop
   * @return
   */
  public static List<File> ls(String filePath,boolean loop) {
    List<File> list = new ArrayList();
    try {
      File file = new File(filePath);
      list.addAll(ls(file,ex);
    }
    return list;
  }

  /**
   * ls 遍历文件
   *
   * @param file
   * @param loop
   * @return
   */
  public static List<File> ls(File file,boolean loop) {
    List<File> list = new ArrayList();
    try {
      list.add(file);
      if (!file.isDirectory()) {
        list.add(file);
      } else if (file.isDirectory()) {
        File[] subList = file.listFiles();
        subList = filesSort(subList,true);
        for (File subFile : subList) {
          if (subFile.isDirectory() && loop) {
            list.addAll(ls(subFile.getPath(),loop));
          } else {
            list.add(subFile);
          }
        }
      }
    } catch (Exception ex) {
      j2log(null,ex);
    }
    return list;
  }

  /**
   * filesSort 文件排序(默认升序)
   *
   * @param parentPath
   * @param subList
   * @return
   */
  private static File[] filesSort(File[] inFiles,boolean asc) {
    List<String> files = new ArrayList();
    List<String> dirs = new ArrayList();
    for (File subFile : inFiles) {
      if (subFile.isDirectory()) {
        dirs.add(subFile.getPath());
      } else if (subFile.isFile()) {
        files.add(subFile.getPath());
      }
    }
    String[] fileArray = {};
    if (files.size() > 0) {
      fileArray = list2Array(files);
      Arrays.sort(fileArray);
      if (!asc) {
        Arrays.sort(fileArray,Collections.reverseOrder());
      }
    }
    String[] dirArray = {};
    if (dirs.size() > 0) {
      dirArray = list2Array(dirs);
      Arrays.sort(dirArray);
      if (!asc) {
        Arrays.sort(dirArray,Collections.reverseOrder());
      }
    }
    return concat2FileArray(fileArray,dirArray);
  }

  /**
   * concat2FileArray 合并文件数组
   *
   * @param old1
   * @param old2
   * @return
   */
  private static File[] concat2FileArray(String[] old1,String[] old2) {
    File[] newArray = new File[old1.length + old2.length];
    for (int i = 0,n = old1.length; i < n; i++) {
      newArray[i] = new File(old1[i]);
    }
    for (int i = 0,j = old1.length,n = (old1.length + old2.length); j < n; i++,j++) {
      newArray[j] = new File(old2[i]);
    }
    return newArray;
  }

  /**
   * read 读取文本文件
   *
   * @param filePath
   * @param fileName
   * @param charset
   * @return
   */
  public static StringBuilder read(String filePath,String fileName,String charset) {
    StringBuilder sb = new StringBuilder();
    try {
      String fullPath = combainPath(filePath,fileName);
      File file = new File(fullPath);
      sb.append(FileTools.tail(file,false,charset));
    } catch (Exception ex) {
      j2log(null,ex);
    }
    return sb;
  }

  /**
   * read 读取文本文件
   *
   * @param fullPath
   * @param charset
   * @return
   */
  public static StringBuilder read(String fullPath,String charset) {
    StringBuilder sb = new StringBuilder();
    try {
      File file = new File(fullPath);
      sb.append(FileTools.tail(file,ex);
    }
    return sb;
  }

  /**
   * read 读取文本文件
   *
   * @param file
   * @param charset
   * @return
   */
  public static StringBuilder read(File file,String charset) {
    StringBuilder sb = new StringBuilder();
    try {
      sb.append(FileTools.tail(file,ex);
    }
    return sb;
  }

  /**
   * find 读取文本文件指定行
   *
   * @param filePath
   * @param fileName
   * @param line
   * @param charset
   * @return
   */
  public static StringBuilder find(String filePath,int line,true,line,ex);
    }
    return sb;
  }

  /**
   * find 读取文本文件指定行
   *
   * @param fullPath
   * @param line
   * @param charset
   * @return
   */
  public static StringBuilder find(String fullPath,ex);
    }
    return sb;
  }

  /**
   * find 读取文本文件指定行
   *
   * @param file
   * @param line
   * @param charset
   * @return
   */
  public static StringBuilder find(File file,ex);
    }
    return sb;
  }

  /**
   * tail 读取文本文件
   *
   * @param filePath
   * @param fileName
   * @param charset
   * @param find
   * @param line
   * @return
   */
  public static StringBuilder tail(String filePath,boolean find,find,ex);
    }
    return sb;
  }

  /**
   * tail 读取文本文件
   *
   * @param fullPath
   * @param charset
   * @param find
   * @param line
   * @return
   */
  public static StringBuilder tail(String fullPath,ex);
    }
    return sb;
  }

  /**
   * tail 读取文本文件
   *
   * @param file
   * @param charset
   * @param find
   * @param line
   * @return
   */
  public static StringBuilder tail(File file,String charset) {
    StringBuilder sb = new StringBuilder();
    BufferedReader bufferReader = null;
    if (null == charset || "".equals(charset)) {
      charset = "UTF-8";
    }
    try {
      if (!file.exists() || file.isDirectory()) {
        throw new FileNotFoundException();
      }
      String fullPath = file.getPath();
      bufferReader = new BufferedReader(new InputStreamReader(new FileInputStream(fullPath),charset));
      String temp;
      for (int i = 0; (temp = bufferReader.readLine()) != null; i++) {
        if (!find || line == i) {
          sb.append(temp);
        }
      }
    } catch (Exception ex) {
      j2log(null,ex);
    } finally {
      if (null != bufferReader) {
        try {
          bufferReader.close();
        } catch (IOException ex) {
          j2log(null,ex);
        }
      }
    }
    return sb;
  }

  /**
   * sed 读取文本文件
   *
   * @param filePath
   * @param fileName
   * @param charset
   * @return
   */
  public static List<String> sed(String filePath,String charset) {
    List<String> list = new ArrayList();
    try {
      String fullPath = combainPath(filePath,fileName);
      File file = new File(fullPath);
      list.addAll(FileTools.sed(file,ex);
    }
    return list;
  }

  /**
   * sed 读取文本文件
   *
   * @param fullPath
   * @param charset
   * @return
   */
  public static List<String> sed(String fullPath,String charset) {
    List<String> list = new ArrayList();
    try {
      File file = new File(fullPath);
      list.addAll(FileTools.sed(file,ex);
    }
    return list;
  }

  /**
   * sed 读取文本文件
   *
   * @param file
   * @param charset
   * @return
   */
  public static List<String> sed(File file,String charset) {
    List<String> list = new ArrayList();
    BufferedReader bufferReader = null;
    if (null == charset || "".equals(charset)) {
      charset = "UTF-8";
    }
    try {
      if (!file.exists() || file.isDirectory()) {
        throw new FileNotFoundException();
      }
      String fullPath = file.getPath();
      bufferReader = new BufferedReader(new InputStreamReader(new FileInputStream(fullPath),charset));
      String temp;
      for (int i = 0; (temp = bufferReader.readLine()) != null; i++) {
        list.add(temp);
      }
    } catch (Exception ex) {
      j2log(null,ex);
        }
      }
    }
    return list;
  }

  /**
   * cat 读取文本文件
   *
   * @param filePath
   * @param fileName
   * @return
   */
  public static byte[] cat(String filePath,String fileName) {
    byte[] output = {};
    try {
      String fullPath = combainPath(filePath,fileName);
      File file = new File(fullPath);
      output = FileTools.cat(file);
    } catch (Exception ex) {
      j2log(null,ex);
    }
    return output;
  }

  /**
   * cat 读取文本文件
   *
   * @param fullPath
   * @return
   */
  public static byte[] cat(String fullPath) {
    byte[] output = {};
    try {
      File file = new File(fullPath);
      output = FileTools.cat(file);
    } catch (Exception ex) {
      j2log(null,ex);
    }
    return output;
  }

  /**
   * cat 读取文本文件
   *
   * @param file
   * @return
   */
  public static byte[] cat(File file) {
    InputStream in = null;
    byte[] output = {};
    try {
      if (!file.exists() || file.isDirectory()) {
        throw new FileNotFoundException();
      }
      String fullPath = file.getPath();
      long length = du(file,false);
      long _2M = 2097152;
      byte[] bytes = new byte[(int) length];
      in = new FileInputStream(fullPath);
      for (int count = 0; count != -1;) {
        if (length > 16 * _2M) {
          length = 4 * _2M;
        } else if (length > 8 * _2M) {
          length = 2 * _2M;
        } else if (length > 4 * _2M) {
          length = _2M;
        } else if (length > 2 * _2M) {
          length = _2M / 2;
        } else if (length > _2M) {
          length = _2M / 4;
        } else {
          length = 4096;
        }
        bytes = new byte[(int) length];
        count = in.read(bytes);
        output = concatArray(bytes,output);
        length = in.available();
      }
    } catch (Exception ex) {
      j2log(null,ex);
    } finally {
      if (null != in) {
        try {
          in.close();
        } catch (Exception ex) {
          j2log(null,ex);
        }
      }
    }
    return output;
  }

  /**
   * 合并数组
   *
   * @param old1
   * @param old2
   * @return
   */
  private static byte[] concatArray(byte[] old1,byte[] old2) {
    byte[] newArray = new byte[old1.length + old2.length];
    System.arraycopy(old1,newArray,old1.length);
    System.arraycopy(old2,old1.length,old2.length);
    return newArray;
  }

  /**
   * dd 写入文件fullPath内容content
   *
   * @param filePath
   * @param fileName
   * @param content
   * @param isAppend
   */
  public static void dd(String filePath,byte[] content,boolean isAppend) {
    try {
      String fullPath = combainPath(filePath,fileName);
      File file = new File(fullPath);
      FileTools.dd(file,content,isAppend);
    } catch (Exception ex) {
      j2log(null,ex);
    }
  }

  /**
   * dd 写入文件fullPath内容content
   *
   * @param fullPath
   * @param content
   * @param isAppend
   */
  public static void dd(String fullPath,boolean isAppend) {
    try {
      File file = new File(fullPath);
      FileTools.dd(file,ex);
    }
  }

  /**
   * dd 写入文件fullPath内容content
   *
   * @param file
   * @param content
   * @param isAppend
   */
  public static void dd(File file,boolean isAppend) {
    FileOutputStream fileOutputStream = null;
    try {
      if (!file.exists()) {
        file.createNewFile();
      }
      fileOutputStream = new FileOutputStream(file,isAppend);
      fileOutputStream.write(content);
    } catch (Exception ex) {
      j2log(null,ex);
    } finally {
      try {
        if (null != fileOutputStream) {
          fileOutputStream.close();
        }
      } catch (IOException ex) {
        j2log(null,ex);
      }
    }
  }

  /**
   * write 写文件内容content到文件fullPath
   *
   * @param filePath
   * @param fileName
   * @param content
   */
  public static void write(String filePath,String content) {
    try {
      String fullPath = combainPath(filePath,fileName);
      File file = new File(fullPath);
      FileTools.write(file,true);
    } catch (Exception ex) {
      j2log(null,ex);
    }
  }

  /**
   * write 写文件内容content到文件fullPath
   *
   * @param fullPath
   * @param content
   */
  public static void write(String fullPath,String content) {
    try {
      File file = new File(fullPath);
      FileTools.write(file,ex);
    }
  }

  /**
   * write 写文件内容content到文件fullPath
   *
   * @param file
   * @param content
   */
  public static void write(File file,String content) {
    try {
      FileTools.write(file,ex);
    }
  }

  /**
   * write 写(追加)文件内容content到文件fullPath
   *
   * @param filePath
   * @param fileName
   * @param content
   * @param isAppend
   */
  public static void write(String filePath,String content,ex);
    }
  }

  /**
   * write 写(追加)文件内容content到文件fullPath
   *
   * @param fullPath
   * @param content
   * @param isAppend
   */
  public static void write(String fullPath,boolean isAppend) {
    try {
      File file = new File(fullPath);
      FileTools.write(file,ex);
    }
  }

  /**
   * write 写(追加)文件内容content到文件fullPath
   *
   * @param file
   * @param content
   * @param isAppend
   */
  public static void write(File file,boolean isAppend) {
    FileWriter fileWriter = null;
    try {
      if (!file.exists()) {
        file.createNewFile();
      }
      fileWriter = new FileWriter(file.getPath(),isAppend);
      fileWriter.write(content);
    } catch (Exception ex) {
      j2log(null,ex);
    } finally {
      if (null != fileWriter) {
        try {
          fileWriter.close();
        } catch (IOException ex) {
          j2log(null,ex);
        }
      }
    }
  }

  /**
   * tail 添加文件内容content到文件的index位置
   *
   * @param filePath
   * @param fileName
   * @param content
   * @param index
   */
  public static void tail(String filePath,long index) {
    try {
      String fullPath = combainPath(filePath,fileName);
      File file = new File(fullPath);
      FileTools.tail(file,index);
    } catch (Exception ex) {
      j2log(null,ex);
    }
  }

  /**
   * tail 添加文件内容content到文件的index位置
   *
   * @param fullPath
   * @param content
   * @param index
   */
  public static void tail(String fullPath,long index) {
    try {
      File file = new File(fullPath);
      FileTools.tail(file,ex);
    }
  }

  /**
   * tail 添加文件内容content到文件的index位置
   *
   * @param file
   * @param content
   * @param index
   */
  public static void tail(File file,long index) {
    RandomAccessFile randomAccessFile = null;
    try {
      if (!file.exists()) {
        file.createNewFile();
      }
      randomAccessFile = new RandomAccessFile(file.getPath(),"rw");
      randomAccessFile.seek(index);
      randomAccessFile.writeBytes(content);
    } catch (Exception ex) {
      j2log(null,ex);
    } finally {
      if (null != randomAccessFile) {
        try {
          randomAccessFile.close();
        } catch (Exception ex) {
          j2log(null,ex);
        }
      }
    }
  }

  /**
   * mkdir 创建目录
   *
   * @param filePath
   * @param fileName
   * @return
   */
  public static File mkdir(String filePath,String fileName) {
    File file = null;
    try {
      String fullPath = combainPath(filePath,fileName);
      file = new File(fullPath);
      file = mkdir(file);
    } catch (Exception ex) {
      j2log(null,ex);
    }
    return file;
  }

  /**
   * mkdir 创建目录
   *
   * @param fullPath
   * @return
   */
  public static File mkdir(String fullPath) {
    File file = null;
    try {
      file = new File(fullPath);
      file = mkdir(file);
    } catch (Exception ex) {
      j2log(null,ex);
    }
    return file;
  }

  /**
   * mkdir 创建目录
   *
   * @param file
   * @return
   */
  public static File mkdir(File file) {
    try {
      if (!file.exists()) {
        file.mkdir();//如果文件夹不存在则创建
      }
    } catch (Exception ex) {
      j2log(null,ex);
    }
    return file;
  }

  /**
   * touch 创建文件
   *
   * @param filePath
   * @param fileName
   */
  public static void touch(String filePath,String fileName) {
    try {
      String fullPath = combainPath(filePath,fileName);
      File file = new File(fullPath);
      touch(file);
    } catch (Exception ex) {
      j2log(null,ex);
    }
  }

  /**
   * touch 创建文件
   *
   * @param fullPath
   */
  public static void touch(String fullPath) {
    try {
      File file = new File(fullPath);
      touch(file);
    } catch (Exception ex) {
      j2log(null,ex);
    }
  }

  /**
   * touch 创建文件
   *
   * @param file
   */
  public static void touch(File file) {
    try {
      if (!file.exists()) {
        file.createNewFile();//如果文件不存在则创建
      }
    } catch (Exception ex) {
      j2log(null,ex);
    }
  }

  /**
   * rm 删除文件
   *
   * @param filePath
   * @param fileName
   */
  public static void rm(String filePath,fileName);
      File file = new File(fullPath);
      rm(file);
    } catch (Exception ex) {
      j2log(null,ex);
    }
  }

  /**
   * rm 删除文件
   *
   * @param fullPath
   */
  public static void rm(String fullPath) {
    try {
      File file = new File(fullPath);
      rm(file);
    } catch (Exception ex) {
      j2log(null,ex);
    }
  }

  /**
   * rm 删除文件
   *
   * @param file
   */
  public static void rm(File file) {
    try {
      if (!file.exists()) {
        throw new FileNotFoundException();
      }
      if (file.isFile()) {
        file.delete();
      }
    } catch (Exception ex) {
      j2log(null,ex);
    }
  }

  /**
   * rmdir 删除目录
   *
   * @param filePath
   * @param fileName
   * @param loop
   */
  public static void rmdir(String filePath,boolean loop) {
    try {
      String fullPath = combainPath(filePath,fileName);
      File dir = new File(fullPath);
      rmdir(dir,loop);
    } catch (Exception ex) {
      j2log(null,ex);
    }
  }

  /**
   * rmdir 删除目录
   *
   * @param fullPath
   * @param loop
   */
  public static void rmdir(String fullPath,boolean loop) {
    try {
      File dir = new File(fullPath);
      rmdir(dir,ex);
    }
  }

  /**
   * rmdir 删除目录
   *
   * @param dir
   * @param loop
   */
  public static void rmdir(File dir,boolean loop) {
    try {
      if (!dir.exists()) {
        throw new FileNotFoundException();
      }
      if (dir.isDirectory()) {
        File[] files = dir.listFiles();
        int length = files.length;
        for (int i = 0; i < length && loop; i++) {
          if (files[i].isDirectory()) {
            rmdir(files[i],loop);
          } else {
            rm(files[i]);
          }
        }
        if (loop || length == 0) {
          dir.delete();
        }
      }
    } catch (Exception ex) {
      j2log(null,ex);
    }
  }

  /**
   * du 获取文件实际大小
   *
   * @param filePath
   * @param fileName
   * @param loop
   * @return
   */
  public static long du(String filePath,boolean loop) {
    long size = 0;
    try {
      String fullPath = combainPath(filePath,fileName);
      File file = new File(fullPath);
      size = du(file,ex);
    }
    return size;
  }

  /**
   * du 获取文件实际大小
   *
   * @param filePath
   * @param fileName
   * @return
   */
  public static long du(String filePath,String fileName) {
    long size = 0;
    try {
      String fullPath = combainPath(filePath,false);
    } catch (Exception ex) {
      j2log(null,ex);
    }
    return size;
  }

  /**
   * du 获取文件实际大小
   *
   * @param fullPath
   * @return
   */
  public static long du(String fullPath) {
    long size = 0;
    try {
      File file = new File(fullPath);
      size = du(file,ex);
    }
    return size;
  }

  /**
   * du 获取文件实际大小
   *
   * @param file
   * @return
   */
  public static long du(File file) {
    long size = 0;
    try {
      size = du(file,ex);
    }
    return size;
  }

  /**
   * du 获取文件实际大小
   *
   * @param fullPath
   * @param loop
   * @return
   */
  public static long du(String fullPath,boolean loop) {
    long size = 0;
    try {
      File file = new File(fullPath);
      size = du(file,ex);
    }
    return size;
  }

  /**
   * du 获取文件实际大小
   *
   * @param file
   * @param loop
   * @return
   */
  public static long du(File file,boolean loop) {
    FileChannel fileChannel = null;
    long size = 0;
    try {
      if (!file.exists()) {
        throw new FileNotFoundException();
      }
      if (file.isFile()) {
        FileInputStream fis = new FileInputStream(file);
        fileChannel = fis.getChannel();
        size = fileChannel.size();
      } else if (file.isDirectory()) {
        File[] files = file.listFiles();
        int length = files.length;
        for (int i = 0; i < length && loop; i++) {
          if (files[i].isDirectory()) {
            du(files[i],loop);
          } else {
            size += du(files[i],false);
          }
        }
      }
    } catch (Exception ex) {
      j2log(null,ex);
    } finally {
      if (null != fileChannel) {
        try {
          fileChannel.close();
        } catch (Exception ex) {
          j2log(null,ex);
        }
      }
    }
    return size;
  }
}

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 目录 连接 连接池产生原因 连接池实现原理 小结 TEMPERANCE:Eat not to dullness;drink not to elevation.节制
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 一个优秀的工程师和一个普通的工程师的区别,不是满天飞的架构图,他的功底体现在所写的每一行代码上。-- 毕玄 1. 命名风格 【书摘】类名用 UpperCamelC
今天犯了个错:“接口变动,伤筋动骨,除非你确定只有你一个人在用”。哪怕只是throw了一个新的Exception。哈哈,这是我犯的错误。一、接口和抽象类类,即一个对象。先抽象类,就是抽象出类的基础部分,即抽象基类(抽象类)。官方定义让人费解,但是记忆方法是也不错的 —包含抽象方法的类叫做抽象类。接口
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:BYSocketFaceBook:BYSocketTwitter :BYSocket一、引子文件,作为常见的数据源。关于操作文件的字节流就是 —FileInputStream&amp;FileOutputStream。
作者:泥沙砖瓦浆木匠网站:http://blog.csdn.net/jeffli1993个人签名:打算起手不凡写出鸿篇巨作的人,往往坚持不了完成第一章节。交流QQ群:【编程之美 365234583】http://qm.qq.com/cgi-bin/qm/qr?k=FhFAoaWwjP29_Aonqz
本文目录 线程与多线程 线程的运行与创建 线程的状态 1 线程与多线程 线程是什么? 线程(Thread)是一个对象(Object)。用来干什么?Java 线程(也称 JVM 线程)是 Java 进程内允许多个同时进行的任务。该进程内并发的任务成为线程(Thread),一个进程里至少一个线程。 Ja
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:BYSocketFaceBook:BYSocketTwitter :BYSocket在面向对象编程中,编程人员应该在意“资源”。比如?1String hello = &quot;hello&quot;; 在代码中,我们
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 这是泥瓦匠的第103篇原创 《程序兵法:Java String 源码的排序算法(一)》 文章工程:* JDK 1.8* 工程名:algorithm-core-le
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 目录 一、父子类变量名相同会咋样? 有个小故事,今天群里面有个人问下面如图输出什么? 我回答:60。但这是错的,答案结果是 40 。我知错能改,然后说了下父子类变
作者:泥瓦匠 出处:https://www.bysocket.com/2021-10-26/mac-create-files-from-the-root-directory.html Mac 操作系统挺适合开发者进行写代码,最近碰到了一个问题,问题是如何在 macOS 根目录创建文件夹。不同的 ma
作者:李强强上一篇,泥瓦匠基础地讲了下Java I/O : Bit Operation 位运算。这一讲,泥瓦匠带你走进Java中的进制详解。一、引子在Java世界里,99%的工作都是处理这高层。那么二进制,字节码这些会在哪里用到呢?自问自答:在跨平台的时候,就凸显神功了。比如说文件读写,数据通信,还
1 线程中断 1.1 什么是线程中断? 线程中断是线程的标志位属性。而不是真正终止线程,和线程的状态无关。线程中断过程表示一个运行中的线程,通过其他线程调用了该线程的 方法,使得该线程中断标志位属性改变。 深入思考下,线程中断不是去中断了线程,恰恰是用来通知该线程应该被中断了。具体是一个标志位属性,
Writer:BYSocket(泥沙砖瓦浆木匠)微博:BYSocket豆瓣:BYSocketReprint it anywhere u want需求 项目在设计表的时候,要处理并发多的一些数据,类似订单号不能重复,要保持唯一。原本以为来个时间戳,精确到毫秒应该不错了。后来觉得是错了,测试环境下很多一
纯技术交流群 每日推荐 - 技术干货推送 跟着泥瓦匠,一起问答交流 扫一扫,我邀请你入群 纯技术交流群 每日推荐 - 技术干货推送 跟着泥瓦匠,一起问答交流 扫一扫,我邀请你入群 加微信:bysocket01
Writer:BYSocket(泥沙砖瓦浆木匠)微博:BYSocket豆瓣:BYSocketReprint it anywhere u want.文章Points:1、介绍RESTful架构风格2、Spring配置CXF3、三层初设计,实现WebService接口层4、撰写HTTPClient 客户
Writer :BYSocket(泥沙砖瓦浆木匠)什么是回调?今天傻傻地截了张图问了下,然后被陈大牛回答道“就一个回调…”。此时千万个草泥马飞奔而过(逃哈哈,看着源码,享受着这种回调在代码上的作用,真是美哉。不妨总结总结。一、什么是回调回调,回调。要先有调用,才有调用者和被调用者之间的回调。所以在百
Writer :BYSocket(泥沙砖瓦浆木匠)一、什么大小端?大小端在计算机业界,Endian表示数据在存储器中的存放顺序。百度百科如下叙述之:大端模式,是指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中,这样的存储模式有点儿类似于把数据当作字符串顺序处理:地址由小向大增加
What is a programming language? Before introducing compilation and decompilation, let&#39;s briefly introduce the Programming Language. Programming la
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:BYSocketFaceBook:BYSocketTwitter :BYSocket泥瓦匠喜欢Java,文章总是扯扯Java。 I/O 基础,就是二进制,也就是Bit。一、Bit与二进制什么是Bit(位)呢?位是CPU
Writer:BYSocket(泥沙砖瓦浆木匠)微博:BYSocket豆瓣:BYSocket一、前言 泥瓦匠最近被项目搞的天昏地暗。发现有些要给自己一些目标,关于技术的目标:专注很重要。专注Java 基础 + H5(学习) 其他操作系统,算法,数据结构当成课外书博览。有时候,就是那样你越是专注方面越