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

java文件操作工具类

程序员文章站 2024-03-11 15:52:49
最近为了修改大量收藏的美剧文件名,用swing写了个小工具,代码是文件处理部分,具体内容如下 package datei.steuern; impor...

最近为了修改大量收藏的美剧文件名,用swing写了个小工具,代码是文件处理部分,具体内容如下

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());
 }
 }
 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, 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;
 }
 
 /**
 * 合并数组
 *
 * @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 < 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;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助。