乐趣区

关于spring-boot-编程思想:新版-Spring-Boot双版本1521-打造企业级微信点餐系统

download:新版 Spring Boot 双版本 (1.5/2.1) 打造企业级微信点餐零碎

*

  • 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, 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() < 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, 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, 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, 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, null, ex);
}
return to;

}

/**

  • mv 挪动文件
    *
  • @param fileName
  • @param source
  • @param destination
  • @param cover
    */

public static void mv(String fileName, String source, String destination, 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, null, ex);
}

}

/**

  • mv 挪动文件
    *
  • @param source
  • @param destination
  • @param cover
    */

public static void mv(String source, String destination, boolean cover) {

try {if (!source.equals(destination)) {File oldFile = new File(source);
    File newFile = new File(destination);
    mv(oldFile, newFile, cover);
  }
} catch (Exception ex) {j2log(null, null, ex);
}

}

/**

  • mv 挪动文件
    *
  • @param source
  • @param destination
  • @param cover
    */

public static void mv(File source, File destination, 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, 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<File> ls(String filePath, boolean loop) {

List<File> 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<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, 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());
  }
}
退出移动版