最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java文件操作工具類

 更新時間:2016年05月18日 16:10:00   作者:s.watson  
這篇文章主要為大家介紹了一個非常詳細的java文件操作工具類,具有很強的實用性,感興趣的小伙伴們可以參考一下

最近為了修改大量收藏的美劇文件名,用swing寫了個小工具,代碼是文件處理部分,具體內(nèi)容如下

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 轉(zhuǎn)義文件目錄
 *
 * @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("http://", "/");
 }
 return dori;
 }
 
 /**
 * list2Array 列表轉(zhuǎn)換數(shù)組
 *
 * @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());
 }
 }
 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 合并文件數(shù)組
 *
 * @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, 0, charset));
 } catch (Exception ex) {
 j2log(null, 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, false, 0, charset));
 } catch (Exception ex) {
 j2log(null, null, 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, false, 0, charset));
 } catch (Exception ex) {
 j2log(null, null, ex);
 }
 return sb;
 }
 
 /**
 * find 讀取文本文件指定行
 *
 * @param filePath
 * @param fileName
 * @param line
 * @param charset
 * @return
 */
 public static StringBuilder find(String filePath, String fileName, int line, String charset) {
 StringBuilder sb = new StringBuilder();
 try {
 String fullPath = combainPath(filePath, fileName);
 File file = new File(fullPath);
 sb.append(FileTools.tail(file, true, line, charset));
 } catch (Exception ex) {
 j2log(null, null, ex);
 }
 return sb;
 }
 
 /**
 * find 讀取文本文件指定行
 *
 * @param fullPath
 * @param line
 * @param charset
 * @return
 */
 public static StringBuilder find(String fullPath, int line, String charset) {
 StringBuilder sb = new StringBuilder();
 try {
 File file = new File(fullPath);
 sb.append(FileTools.tail(file, true, line, charset));
 } catch (Exception ex) {
 j2log(null, null, ex);
 }
 return sb;
 }
 
 /**
 * find 讀取文本文件指定行
 *
 * @param file
 * @param line
 * @param charset
 * @return
 */
 public static StringBuilder find(File file, int line, String charset) {
 StringBuilder sb = new StringBuilder();
 try {
 sb.append(FileTools.tail(file, true, line, charset));
 } catch (Exception ex) {
 j2log(null, null, ex);
 }
 return sb;
 }
 
 /**
 * tail 讀取文本文件
 *
 * @param filePath
 * @param fileName
 * @param charset
 * @param find
 * @param line
 * @return
 */
 public static StringBuilder tail(String filePath, String fileName, boolean find, int line, String charset) {
 StringBuilder sb = new StringBuilder();
 try {
 String fullPath = combainPath(filePath, fileName);
 File file = new File(fullPath);
 sb.append(FileTools.tail(file, find, line, charset));
 } catch (Exception ex) {
 j2log(null, null, ex);
 }
 return sb;
 }
 
 /**
 * tail 讀取文本文件
 *
 * @param fullPath
 * @param charset
 * @param find
 * @param line
 * @return
 */
 public static StringBuilder tail(String fullPath, boolean find, int line, String charset) {
 StringBuilder sb = new StringBuilder();
 try {
 File file = new File(fullPath);
 sb.append(FileTools.tail(file, find, line, charset));
 } catch (Exception ex) {
 j2log(null, null, ex);
 }
 return sb;
 }
 
 /**
 * tail 讀取文本文件
 *
 * @param file
 * @param charset
 * @param find
 * @param line
 * @return
 */
 public static StringBuilder tail(File file, boolean find, int line, 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, null, ex);
 } finally {
 if (null != bufferReader) {
 try {
  bufferReader.close();
 } catch (IOException ex) {
  j2log(null, null, ex);
 }
 }
 }
 return sb;
 }
 
 /**
 * sed 讀取文本文件
 *
 * @param filePath
 * @param fileName
 * @param charset
 * @return
 */
 public static List<String> sed(String filePath, String fileName, String charset) {
 List<String> 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<String> sed(String fullPath, String charset) {
 List<String> 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<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, 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;
 }
 
 /**
 * 合并數(shù)組
 *
 * @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內(nèi)容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內(nèi)容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內(nèi)容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 寫文件內(nèi)容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 寫文件內(nèi)容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 寫文件內(nèi)容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 寫(追加)文件內(nèi)容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 寫(追加)文件內(nèi)容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 寫(追加)文件內(nèi)容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 添加文件內(nèi)容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 添加文件內(nèi)容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 添加文件內(nèi)容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 創(chuàng)建目錄
 *
 * @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 創(chuàng)建目錄
 *
 * @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 創(chuàng)建目錄
 *
 * @param file
 * @return
 */
 public static File mkdir(File file) {
 try {
 if (!file.exists()) {
 file.mkdir();//如果文件夾不存在則創(chuàng)建
 }
 } catch (Exception ex) {
 j2log(null, null, ex);
 }
 return file;
 }
 
 /**
 * touch 創(chuàng)建文件
 *
 * @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 創(chuàng)建文件
 *
 * @param fullPath
 */
 public static void touch(String fullPath) {
 try {
 File file = new File(fullPath);
 touch(file);
 } catch (Exception ex) {
 j2log(null, null, ex);
 }
 }
 
 /**
 * touch 創(chuàng)建文件
 *
 * @param file
 */
 public static void touch(File file) {
 try {
 if (!file.exists()) {
 file.createNewFile();//如果文件不存在則創(chuàng)建
 }
 } 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 < 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, null, ex);
 }
 }
 
 /**
 * du 獲取文件實際大小
 *
 * @param filePath
 * @param fileName
 * @param loop
 * @return
 */
 public static long du(String filePath, String fileName, boolean loop) {
 long size = 0;
 try {
 String fullPath = combainPath(filePath, fileName);
 File file = new File(fullPath);
 size = du(file, loop);
 } catch (Exception ex) {
 j2log(null, null, 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, fileName);
 File file = new File(fullPath);
 size = du(file, false);
 } catch (Exception ex) {
 j2log(null, 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, false);
 } catch (Exception ex) {
 j2log(null, null, ex);
 }
 return size;
 }
 
 /**
 * du 獲取文件實際大小
 *
 * @param file
 * @return
 */
 public static long du(File file) {
 long size = 0;
 try {
 size = du(file, false);
 } catch (Exception ex) {
 j2log(null, null, 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, loop);
 } catch (Exception ex) {
 j2log(null, null, 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, null, ex);
 } finally {
 if (null != fileChannel) {
 try {
  fileChannel.close();
 } catch (Exception ex) {
  j2log(null, null, ex);
 }
 }
 }
 return size;
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助。

相關(guān)文章

  • Java數(shù)據(jù)結(jié)構(gòu)之鏈表的增刪查改詳解

    Java數(shù)據(jù)結(jié)構(gòu)之鏈表的增刪查改詳解

    在這篇文章中,小編將帶大家了解一下Java數(shù)據(jù)結(jié)構(gòu)中鏈表的增刪查改(以下結(jié)果均在IDEA中編譯)希望在方便自己復習的同時也能幫助到大家
    2022-09-09
  • MyBatis Generator去掉生成的注解

    MyBatis Generator去掉生成的注解

    這篇文章主要介紹了MyBatis Generator去掉生成的注解的相關(guān)資料,非常不錯具有一定的參考借鑒價值,需要的朋友可以參考下
    2016-11-11
  • JavaWeb基于Session實現(xiàn)的用戶登陸注銷方法示例

    JavaWeb基于Session實現(xiàn)的用戶登陸注銷方法示例

    為了安全起見,session常常用來保存用戶的登錄信息。那么服務器是怎么來實現(xiàn)的呢?下面這篇文章就來給大家介紹了關(guān)于JavaWeb基于Session實現(xiàn)的用戶登陸注銷的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。
    2017-12-12
  • 如何使用Spring Validation優(yōu)雅地校驗參數(shù)

    如何使用Spring Validation優(yōu)雅地校驗參數(shù)

    這篇文章主要介紹了如何使用Spring Validation優(yōu)雅地校驗參數(shù),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • Netty網(wǎng)絡編程零基礎入門

    Netty網(wǎng)絡編程零基礎入門

    Netty是一個異步的、基于事件驅(qū)動的網(wǎng)絡應用框架,用于快速開發(fā)可維護、高性能的網(wǎng)絡服務器和客戶端,如果你還不了解它的使用,就趕快繼續(xù)往下看吧
    2022-08-08
  • Java設計模式之java策略模式詳解

    Java設計模式之java策略模式詳解

    這篇文章主要介紹了Java經(jīng)典設計模式之策略模式,簡單說明了策略模式的概念、原理并結(jié)合實例形式分析了java策略模式的具有用法與相關(guān)注意事項,需要的朋友可以參考下
    2021-09-09
  • Java讀取Excel、docx、pdf和txt等文件萬能方法舉例

    Java讀取Excel、docx、pdf和txt等文件萬能方法舉例

    在Java開發(fā)中處理文件是常見需求,本文以實際代碼示例詳述如何使用ApachePOI庫及其他工具讀取和寫入Excel、Word、PDF等文件,介紹了ApachePOI、ApachePDFBox和EasyExcel等庫的使用方法,幫助開發(fā)者有效讀取不同格式文件,需要的朋友可以參考下
    2024-09-09
  • Java游戲開發(fā)拼圖游戲經(jīng)典版

    Java游戲開發(fā)拼圖游戲經(jīng)典版

    這篇文章主要介紹了Java游戲開發(fā)拼圖游戲經(jīng)典版,對這方面感興趣的同學可以跟著教程試下
    2021-01-01
  • Java中byte、byte數(shù)組與int、long的轉(zhuǎn)換詳解

    Java中byte、byte數(shù)組與int、long的轉(zhuǎn)換詳解

    這篇文章分別給大家介紹了Java中byte和int之間的轉(zhuǎn)換、Java中 byte數(shù)組和int之間的轉(zhuǎn)換、Java中byte數(shù)組和long之間的轉(zhuǎn)換以及整理了整體工具類的源碼,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-02-02
  • java中枚舉原來還可以這么用

    java中枚舉原來還可以這么用

    這篇文章主要給大家介紹了關(guān)于java枚舉原來還可以這么用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用java具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-09-09

最新評論

阆中市| 绍兴市| 岳池县| 会理县| 舞钢市| 时尚| 上杭县| 六枝特区| 祁门县| 江津市| 镇宁| 呼和浩特市| 武宣县| 定兴县| 克什克腾旗| 八宿县| 楚雄市| 宁南县| 武宣县| 梅河口市| 新乡市| 出国| 尼勒克县| 且末县| 南华县| 洪泽县| 石楼县| 宝山区| 鲁甸县| 常德市| 庆安县| 叙永县| 海城市| 增城市| 巍山| 永州市| 安泽县| 荆门市| 乌苏市| 孟津县| 出国|