欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  Java

java文件操作的工具类

程序员文章站 2022-03-22 15:29:39
...
/*
 * 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 cp(String source, String destination, boolean loop) {
    List 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 cp(File source, File destination, boolean loop) {
    List 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, 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, 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()  (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  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, 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, 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, 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, null, ex);
    }
    return extension;
  }
 
  /**
   * ls 遍历文件
   *
   * @param filePath
   * @param loop
   * @return
   */
  public static List ls(String filePath, boolean loop) {
    List list = new ArrayList();
    try {
      File file = new File(filePath);
      list.addAll(ls(file, loop));
    } catch (Exception ex) {
      j2log(null, null, ex);
    }
    return list;
  }
 
  /**
   * ls 遍历文件
   *
   * @param file
   * @param loop
   * @return
   */
  public static List ls(File file, boolean loop) {
    List 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, null, ex);
    }
    return list;
  }
 
  /**
   * filesSort 文件排序(默认升序)
   *
   * @param parentPath
   * @param subList
   * @return
   */
  private static File[] filesSort(File[] inFiles, boolean asc) {
    List files = new ArrayList();
    List 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  sed(String filePath, String fileName, String charset) {
    List list = new ArrayList();
    try {
      String fullPath = combainPath(filePath, fileName);
      File file = new File(fullPath);
      list.addAll(FileTools.sed(file, charset));
    } catch (Exception ex) {
      j2log(null, null, ex);
    }
    return list;
  }
 
  /**
   * sed 读取文本文件
   *
   * @param fullPath
   * @param charset
   * @return
   */
  public static List sed(String fullPath, String charset) {
    List list = new ArrayList();
    try {
      File file = new File(fullPath);
      list.addAll(FileTools.sed(file, charset));
    } catch (Exception ex) {
      j2log(null, null, ex);
    }
    return list;
  }
 
  /**
   * sed 读取文本文件
   *
   * @param file
   * @param charset
   * @return
   */
  public static List sed(File file, String charset) {
    List 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, null, ex);
    } finally {
      if (null != bufferReader) {
        try {
          bufferReader.close();
        } catch (IOException ex) {
          j2log(null, 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, 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, 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, null, ex);
    } finally {
      if (null != in) {
        try {
          in.close();
        } catch (Exception ex) {
          j2log(null, 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, 0, newArray, 0, old1.length);
    System.arraycopy(old2, 0, newArray, old1.length, old2.length);
    return newArray;
  }
 
  /**
   * dd 写入文件fullPath内容content
   *
   * @param filePath
   * @param fileName
   * @param content
   * @param isAppend
   */
  public static void dd(String filePath, String fileName, 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, null, ex);
    }
  }
 
  /**
   * dd 写入文件fullPath内容content
   *
   * @param fullPath
   * @param content
   * @param isAppend
   */
  public static void dd(String fullPath, byte[] content, boolean isAppend) {
    try {
      File file = new File(fullPath);
      FileTools.dd(file, content, isAppend);
    } catch (Exception ex) {
      j2log(null, null, ex);
    }
  }
 
  /**
   * dd 写入文件fullPath内容content
   *
   * @param file
   * @param content
   * @param isAppend
   */
  public static void dd(File file, byte[] content, boolean isAppend) {
    FileOutputStream fileOutputStream = null;
    try {
      if (!file.exists()) {
        file.createNewFile();
      }
      fileOutputStream = new FileOutputStream(file, isAppend);
      fileOutputStream.write(content);
    } catch (Exception ex) {
      j2log(null, null, ex);
    } finally {
      try {
        if (null != fileOutputStream) {
          fileOutputStream.close();
        }
      } catch (IOException ex) {
        j2log(null, null, ex);
      }
    }
  }
 
  /**
   * write 写文件内容content到文件fullPath
   *
   * @param filePath
   * @param fileName
   * @param content
   */
  public static void write(String filePath, String fileName, String content) {
    try {
      String fullPath = combainPath(filePath, fileName);
      File file = new File(fullPath);
      FileTools.write(file, content, true);
    } catch (Exception ex) {
      j2log(null, 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, content, true);
    } catch (Exception ex) {
      j2log(null, null, ex);
    }
  }
 
  /**
   * write 写文件内容content到文件fullPath
   *
   * @param file
   * @param content
   */
  public static void write(File file, String content) {
    try {
      FileTools.write(file, content, true);
    } catch (Exception ex) {
      j2log(null, null, ex);
    }
  }
 
  /**
   * write 写(追加)文件内容content到文件fullPath
   *
   * @param filePath
   * @param fileName
   * @param content
   * @param isAppend
   */
  public static void write(String filePath, String fileName, String content, boolean isAppend) {
    try {
      String fullPath = combainPath(filePath, fileName);
      File file = new File(fullPath);
      FileTools.write(file, content, isAppend);
    } catch (Exception ex) {
      j2log(null, null, ex);
    }
  }
 
  /**
   * write 写(追加)文件内容content到文件fullPath
   *
   * @param fullPath
   * @param content
   * @param isAppend
   */
  public static void write(String fullPath, String content, boolean isAppend) {
    try {
      File file = new File(fullPath);
      FileTools.write(file, content, isAppend);
    } catch (Exception ex) {
      j2log(null, null, ex);
    }
  }
 
  /**
   * write 写(追加)文件内容content到文件fullPath
   *
   * @param file
   * @param content
   * @param isAppend
   */
  public static void write(File file, String content, 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, null, ex);
    } finally {
      if (null != fileWriter) {
        try {
          fileWriter.close();
        } catch (IOException ex) {
          j2log(null, null, ex);
        }
      }
    }
  }
 
  /**
   * tail 添加文件内容content到文件的index位置
   *
   * @param filePath
   * @param fileName
   * @param content
   * @param index
   */
  public static void tail(String filePath, String fileName, String content, long index) {
    try {
      String fullPath = combainPath(filePath, fileName);
      File file = new File(fullPath);
      FileTools.tail(file, content, index);
    } catch (Exception ex) {
      j2log(null, null, ex);
    }
  }
 
  /**
   * tail 添加文件内容content到文件的index位置
   *
   * @param fullPath
   * @param content
   * @param index
   */
  public static void tail(String fullPath, String content, long index) {
    try {
      File file = new File(fullPath);
      FileTools.tail(file, content, index);
    } catch (Exception ex) {
      j2log(null, null, ex);
    }
  }
 
  /**
   * tail 添加文件内容content到文件的index位置
   *
   * @param file
   * @param content
   * @param index
   */
  public static void tail(File file, String content, 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, null, ex);
    } finally {
      if (null != randomAccessFile) {
        try {
          randomAccessFile.close();
        } catch (Exception ex) {
          j2log(null, 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, 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, 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, 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, null, ex);
    }
  }
 
  /**
   * touch 创建文件
   *
   * @param fullPath
   */
  public static void touch(String fullPath) {
    try {
      File file = new File(fullPath);
      touch(file);
    } catch (Exception ex) {
      j2log(null, null, ex);
    }
  }
 
  /**
   * touch 创建文件
   *
   * @param file
   */
  public static void touch(File file) {
    try {
      if (!file.exists()) {
        file.createNewFile();//如果文件不存在则创建
      }
    } catch (Exception ex) {
      j2log(null, null, ex);
    }
  }
 
  /**
   * rm 删除文件
   *
   * @param filePath
   * @param fileName
   */
  public static void rm(String filePath, String fileName) {
    try {
      String fullPath = combainPath(filePath, fileName);
      File file = new File(fullPath);
      rm(file);
    } catch (Exception ex) {
      j2log(null, null, ex);
    }
  }
 
  /**
   * rm 删除文件
   *
   * @param fullPath
   */
  public static void rm(String fullPath) {
    try {
      File file = new File(fullPath);
      rm(file);
    } catch (Exception ex) {
      j2log(null, 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, null, ex);
    }
  }
 
  /**
   * rmdir 删除目录
   *
   * @param filePath
   * @param fileName
   * @param loop
   */
  public static void rmdir(String filePath, String fileName, boolean loop) {
    try {
      String fullPath = combainPath(filePath, fileName);
      File dir = new File(fullPath);
      rmdir(dir, loop);
    } catch (Exception ex) {
      j2log(null, null, ex);
    }
  }
 
  /**
   * rmdir 删除目录
   *
   * @param fullPath
   * @param loop
   */
  public static void rmdir(String fullPath, boolean loop) {
    try {
      File dir = new File(fullPath);
      rmdir(dir, loop);
    } catch (Exception ex) {
      j2log(null, null, 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