您好,欢迎访问一九零五行业门户网

java文件操作的工具类

/* * 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 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; } }
其它类似信息

推荐信息