java文件处理工具类详解
程序员文章站
2024-02-23 22:13:04
本文实例为大家分享了java文件处理工具类的具体代码,供大家参考,具体内容如下
import java.io.bufferedinputstream;
impo...
本文实例为大家分享了java文件处理工具类的具体代码,供大家参考,具体内容如下
import java.io.bufferedinputstream; import java.io.bufferedoutputstream; import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.bytearrayinputstream; import java.io.bytearrayoutputstream; import java.io.file; import java.io.filefilter; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.objectinputstream; import java.io.objectoutput; import java.io.objectoutputstream; import java.io.outputstream; import java.io.outputstreamwriter; import java.io.writer; import java.net.url; import java.net.urlencoder; import java.text.decimalformat; import java.util.arraylist; import java.util.collections; import java.util.enumeration; import java.util.list; import java.util.properties; import javax.servlet.servletoutputstream; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory; public class fileutil { private static log logger = logfactory.getlog(fileutil.class); public static void writefile(string paramstring1, string paramstring2) { writefile(paramstring1, paramstring2, "utf-8"); } public static void writefile(string paramstring1, string paramstring2, string paramstring3) { try { createfolder(paramstring1, true); bufferedwriter localbufferedwriter = new bufferedwriter(new outputstreamwriter(new fileoutputstream(paramstring1), paramstring3)); localbufferedwriter.write(paramstring2); localbufferedwriter.close(); } catch (ioexception localioexception) { localioexception.printstacktrace(); } } public static void writefile(string paramstring, inputstream paraminputstream) throws ioexception { fileoutputstream localfileoutputstream = new fileoutputstream(paramstring); byte[] arrayofbyte = new byte['Ȁ']; int i = 0; while ((i = paraminputstream.read(arrayofbyte)) != -1) { localfileoutputstream.write(arrayofbyte, 0, i); } paraminputstream.close(); localfileoutputstream.close(); } public static string readfile(string paramstring) throws ioexception { try { file localfile = new file(paramstring); string str1 = getcharset(localfile); stringbuffer localstringbuffer = new stringbuffer(); bufferedreader localbufferedreader = new bufferedreader(new inputstreamreader(new fileinputstream(paramstring), str1)); string str2; while ((str2 = localbufferedreader.readline()) != null) { localstringbuffer.append(str2 + "\r\n"); } localbufferedreader.close(); return localstringbuffer.tostring(); } catch (ioexception localioexception) { throw localioexception; } } public static boolean isexistfile(string paramstring) { boolean bool = false; file localfile = new file(paramstring); if (localfile.isdirectory()) { file[] arrayoffile = localfile.listfiles(); if ((arrayoffile != null) && (arrayoffile.length != 0)) { bool = true; } } return bool; } public static string getcharset(file paramfile) { string str = "gbk"; byte[] arrayofbyte = new byte[3]; try { int i = 0; bufferedinputstream localbufferedinputstream = new bufferedinputstream(new fileinputstream(paramfile)); localbufferedinputstream.mark(0); int j = localbufferedinputstream.read(arrayofbyte, 0, 3); if (j == -1) { return str; } if ((arrayofbyte[0] == -1) && (arrayofbyte[1] == -2)) { str = "utf-16le"; i = 1; } else if ((arrayofbyte[0] == -2) && (arrayofbyte[1] == -1)) { str = "utf-16be"; i = 1; } else if ((arrayofbyte[0] == -17) && (arrayofbyte[1] == -69) && (arrayofbyte[2] == -65)) { str = "utf-8"; i = 1; } localbufferedinputstream.reset(); if (i == 0) { int k = 0; while ((j = localbufferedinputstream.read()) != -1) { k++; if ((j < 240) && ((128 > j) || (j > 191))) { if ((192 <= j) && (j <= 223)) { j = localbufferedinputstream.read(); if (128 > j) { break; } if (j > 191) { break; } } else if ((224 <= j) && (j <= 239)) { j = localbufferedinputstream.read(); if ((128 <= j) && (j <= 191)) { j = localbufferedinputstream.read(); if ((128 <= j) && (j <= 191)) { str = "utf-8"; } } } } } } localbufferedinputstream.close(); } catch (exception localexception) { localexception.printstacktrace(); } return str; } public static byte[] readbyte(inputstream paraminputstream) { try { byte[] arrayofbyte = new byte[paraminputstream.available()]; paraminputstream.read(arrayofbyte); return arrayofbyte; } catch (filenotfoundexception localfilenotfoundexception) { logger.error("文件路径不存在:" + localfilenotfoundexception.getmessage()); } catch (exception localexception) { localexception.printstacktrace(); } return null; } public static byte[] readbyte(string paramstring) { try { fileinputstream localfileinputstream = new fileinputstream(paramstring); byte[] arrayofbyte = new byte[localfileinputstream.available()]; localfileinputstream.read(arrayofbyte); localfileinputstream.close(); return arrayofbyte; } catch (filenotfoundexception localfilenotfoundexception) { logger.error("文件路径不存在:" + paramstring); } catch (exception localexception) { localexception.printstacktrace(); } return null; } public static boolean writebyte(string paramstring, byte[] paramarrayofbyte) { try { bufferedoutputstream localbufferedoutputstream = new bufferedoutputstream(new fileoutputstream(paramstring)); localbufferedoutputstream.write(paramarrayofbyte); localbufferedoutputstream.close(); return true; } catch (exception localexception) { localexception.printstacktrace(); } return false; } public static boolean deletedir(file paramfile) { if (paramfile.isdirectory()) { string[] arrayofstring = paramfile.list(); for (int i = 0; i < arrayofstring.length; i++) { boolean bool = deletedir(new file(paramfile, arrayofstring[i])); if (!bool) { return false; } } } return paramfile.delete(); } public static void serializetofile(object paramobject, string paramstring) { try { objectoutputstream localobjectoutputstream = new objectoutputstream(new fileoutputstream(paramstring)); localobjectoutputstream.writeobject(paramobject); localobjectoutputstream.close(); } catch (ioexception localioexception) { localioexception.printstacktrace(); } } public static object deserializefromfile(string paramstring) { try { file localfile = new file(paramstring); objectinputstream localobjectinputstream = new objectinputstream(new fileinputstream(localfile)); object localobject = localobjectinputstream.readobject(); localobjectinputstream.close(); return localobject; } catch (exception localexception) { localexception.printstacktrace(); } return null; } public static string inputstream2string(inputstream paraminputstream, string paramstring) throws ioexception { bufferedreader localbufferedreader = new bufferedreader(new inputstreamreader(paraminputstream, paramstring)); stringbuffer localstringbuffer = new stringbuffer(); string str = ""; while ((str = localbufferedreader.readline()) != null) { localstringbuffer.append(str + "\n"); } return localstringbuffer.tostring(); } public static string inputstream2string(inputstream paraminputstream) throws ioexception { return inputstream2string(paraminputstream, "utf-8"); } public static file[] getfiles(string paramstring) { file localfile = new file(paramstring); return localfile.listfiles(); } public static void createfolderfile(string paramstring) { createfolder(paramstring, true); } public static void createfolder(string paramstring, boolean paramboolean) { if (paramboolean) { paramstring = paramstring.substring(0, paramstring.lastindexof(file.separator)); } file localfile = new file(paramstring); if (!localfile.exists()) { localfile.mkdirs(); } } public static void createfolder(string paramstring1, string paramstring2) { paramstring1 = stringutil.trimsufffix(paramstring1, file.separator) + file.separator + paramstring2; file localfile = new file(paramstring1); localfile.mkdir(); } public static void renamefolder(string paramstring1, string paramstring2) { file localfile = new file(paramstring1); if (localfile.exists()) { localfile.renameto(new file(paramstring2)); } } public static arraylist<file> getdiretoryonly(file paramfile) { arraylist localarraylist = new arraylist(); if ((paramfile != null) && (paramfile.exists()) && (paramfile.isdirectory())) { file[] arrayoffile = paramfile.listfiles(new filefilter() { public boolean accept(file paramanonymousfile) { return paramanonymousfile.isdirectory(); } }); for (int i = 0; i < arrayoffile.length; i++) { localarraylist.add(arrayoffile[i]); } } return localarraylist; } public arraylist<file> getfileonly(file paramfile) { arraylist localarraylist = new arraylist(); file[] arrayoffile = paramfile.listfiles(new filefilter() { public boolean accept(file paramanonymousfile) { return paramanonymousfile.isfile(); } }); for (int i = 0; i < arrayoffile.length; i++) { localarraylist.add(arrayoffile[i]); } return localarraylist; } public static boolean deletefile(string paramstring) { file localfile = new file(paramstring); return localfile.delete(); } public static boolean copyfile(string paramstring1, string paramstring2) { file localfile1 = new file(paramstring1); file localfile2 = new file(paramstring2); fileinputstream localfileinputstream = null; fileoutputstream localfileoutputstream = null; try { localfileinputstream = new fileinputstream(localfile1); localfileoutputstream = new fileoutputstream(localfile2); byte[] arrayofbyte = new byte['က']; int i; while ((i = localfileinputstream.read(arrayofbyte)) != -1) { localfileoutputstream.write(arrayofbyte, 0, i); } localfileoutputstream.flush(); localfileoutputstream.close(); localfileinputstream.close(); } catch (ioexception localioexception) { localioexception.printstacktrace(); return false; } return true; } public static void backupfile(string paramstring) { string str = paramstring + ".bak"; file localfile = new file(str); if (localfile.exists()) { localfile.delete(); } copyfile(paramstring, str); } public static string getfileext(file paramfile) { if (paramfile.isfile()) { return getfileext(paramfile.getname()); } return ""; } public static string getfileext(string paramstring) { int i = paramstring.lastindexof("."); if (i > -1) { return paramstring.substring(i + 1).tolowercase(); } return ""; } public static void copydir(string paramstring1, string paramstring2) throws ioexception { new file(paramstring2).mkdirs(); file[] arrayoffile = new file(paramstring1).listfiles(); for (int i = 0; i < arrayoffile.length; i++) { if (arrayoffile[i].isfile()) { string str1 = arrayoffile[i].getabsolutepath(); string str2 = paramstring2 + "/" + arrayoffile[i].getname(); copyfile(str1, str2); } if (arrayoffile[i].isdirectory()) { copydirectiory(paramstring1 + "/" + arrayoffile[i].getname(), paramstring2 + "/" + arrayoffile[i].getname()); } } } private static void copydirectiory(string paramstring1, string paramstring2) throws ioexception { new file(paramstring2).mkdirs(); file[] arrayoffile = new file(paramstring1).listfiles(); for (int i = 0; i < arrayoffile.length; i++) { if (arrayoffile[i].isfile()) { string str1 = arrayoffile[i].getabsolutepath(); string str2 = paramstring2 + "/" + arrayoffile[i].getname(); copyfile(str1, str2); } if (arrayoffile[i].isdirectory()) { copydirectiory(paramstring1 + "/" + arrayoffile[i].getname(), paramstring2 + "/" + arrayoffile[i].getname()); } } } public static string getfilesize(file paramfile) throws ioexception { if (paramfile.isfile()) { fileinputstream localfileinputstream = new fileinputstream(paramfile); int i = localfileinputstream.available(); localfileinputstream.close(); return getsize(i); } return ""; } public static string getsize(double paramdouble) { decimalformat localdecimalformat = new decimalformat("0.00"); double d; if (paramdouble > 1048576.0d) { d = paramdouble / 1048576.0d; return localdecimalformat.format(d) + " m"; } if (paramdouble > 1024.0d) { d = paramdouble / 1024.0d; return localdecimalformat.format(d) + " kb"; } return paramdouble + " bytes"; } public static void downloadfile(httpservletrequest paramhttpservletrequest, httpservletresponse paramhttpservletresponse, string paramstring1, string paramstring2) throws ioexception { servletoutputstream localservletoutputstream = paramhttpservletresponse.getoutputstream(); file localfile = new file(paramstring1); if (localfile.exists()) { paramhttpservletresponse.setcontenttype("application/octet-stream"); string str1 = getfileext(paramstring1); if ((!validation.isempty(str1)) && (str1.tolowercase().equals("apk"))) { paramhttpservletresponse.setcontenttype("application/vnd.android.package-archive"); } string str2 = paramstring2; string str3 = paramhttpservletrequest.getheader("user-agent"); if ((str3 != null) && (str3.indexof("msie") == -1)) { try { string localobject1 = transcharacter(paramhttpservletrequest, str2); paramhttpservletresponse.setheader("content-disposition", "attachment; filename=" + (string)localobject1); } catch (exception localexception1) { localexception1.printstacktrace(); } } else { str2 = urlencoder.encode(str2, "utf-8"); paramhttpservletresponse.addheader("content-disposition", "attachment;filename=" + str2); } object localobject1 = null; try { localservletoutputstream = paramhttpservletresponse.getoutputstream(); localobject1 = new fileinputstream(paramstring1); byte[] arrayofbyte = new byte['Ѐ']; int i = 0; while ((i = ((fileinputstream)localobject1).read(arrayofbyte)) > 0) { localservletoutputstream.write(arrayofbyte, 0, i); } localservletoutputstream.flush(); } catch (exception localexception2) { localexception2.printstacktrace(); } finally { if (localobject1 != null) { ((fileinputstream)localobject1).close(); localobject1 = null; } if (localservletoutputstream != null) { localservletoutputstream.close(); localservletoutputstream = null; paramhttpservletresponse.flushbuffer(); } } } else { localservletoutputstream.write("文件不存在!".getbytes("utf-8")); } } private static string transcharacter(httpservletrequest paramhttpservletrequest, string paramstring) throws exception { if (paramhttpservletrequest.getheader("user-agent").tolowercase().indexof("msie") > 0) { return urlencoder.encode(paramstring, "utf-8"); } if (paramhttpservletrequest.getheader("user-agent").tolowercase().indexof("firefox") > 0) { return new string(paramstring.getbytes("utf-8"), "iso8859-1"); } return new string(paramstring.getbytes("gbk"), "iso8859-1"); } public static string getparentdir(string paramstring1, string paramstring2) { file localfile = new file(paramstring2); string str1 = localfile.getparent(); string str2 = str1.replace(paramstring1, ""); return str2.replace(file.separator, "/"); } public static string getclassespath() { string str = stringutil.trimsufffix(apputil.getrealpath("/"), file.separator) + "\\web-inf\\classes\\".replace("\\", file.separator); return str; } public static string getrootpath() { string str = stringutil.trimsufffix(apputil.getrealpath("/"), file.separator) + file.separator; return str; } public static string readfromproperties(string paramstring1, string paramstring2) { string str1 = ""; bufferedinputstream localbufferedinputstream = null; try { localbufferedinputstream = new bufferedinputstream(new fileinputstream(paramstring1)); properties localproperties = new properties(); localproperties.load(localbufferedinputstream); str1 = localproperties.getproperty(paramstring2); string str2 = str1; return str2; } catch (exception localexception) { localexception.printstacktrace(); } finally { if (localbufferedinputstream != null) { try { localbufferedinputstream.close(); } catch (ioexception localioexception3) { localioexception3.printstacktrace(); } } } return str1; } public static boolean saveproperties(string paramstring1, string paramstring2, string paramstring3) { stringbuffer localstringbuffer = new stringbuffer(); int i = 0; bufferedreader localbufferedreader = null; try { localbufferedreader = new bufferedreader(new inputstreamreader(new fileinputstream(paramstring1), "utf-8")); string str; while ((str = localbufferedreader.readline()) != null) { if (str.startswith(paramstring2)) { localstringbuffer.append(paramstring2 + "=" + paramstring3 + "\r\n"); i = 1; } else { localstringbuffer.append(str + "\r\n"); } } if (i == 0) { localstringbuffer.append(paramstring2 + "=" + paramstring3 + "\r\n"); } writefile(paramstring1, localstringbuffer.tostring(), "utf-8"); boolean bool1 = true; return bool1; } catch (exception localexception) { localexception.printstacktrace(); boolean bool2 = false; return bool2; } finally { if (localbufferedreader != null) { try { localbufferedreader.close(); } catch (ioexception localioexception3) { localioexception3.printstacktrace(); } } } } public static boolean delproperties(string paramstring1, string paramstring2) { stringbuffer localstringbuffer = new stringbuffer(); bufferedreader localbufferedreader = null; try { localbufferedreader = new bufferedreader(new inputstreamreader(new fileinputstream(paramstring1), "utf-8")); string str; while ((str = localbufferedreader.readline()) != null) { if (!str.startswith(paramstring2)) { localstringbuffer.append(str + "\r\n"); } } writefile(paramstring1, localstringbuffer.tostring(), "utf-8"); boolean bool1 = true; return bool1; } catch (exception localexception) { localexception.printstacktrace(); boolean bool2 = false; return bool2; } finally { if (localbufferedreader != null) { try { localbufferedreader.close(); } catch (ioexception localioexception3) { localioexception3.printstacktrace(); } } } } public static list<class<?>> getallclassesbyinterface(class<?> paramclass, boolean paramboolean) throws ioexception, classnotfoundexception, illegalstateexception { if (!paramclass.isinterface()) { throw new illegalstateexception("class not a interface."); } classloader localclassloader = paramclass.getclassloader(); string str = paramboolean ? paramclass.getpackage().getname() : "/"; return findclasses(paramclass, localclassloader, str); } private static list<class<?>> findclasses(class<?> paramclass, classloader paramclassloader, string paramstring) throws ioexception, classnotfoundexception { arraylist localarraylist = new arraylist(); string str = paramstring.replace(".", "/"); object localobject; if (!str.equals("/")) { localobject = paramclassloader.getresources(str); while (((enumeration)localobject).hasmoreelements()) { url localurl = (url)((enumeration)localobject).nextelement(); localarraylist.addall(findresources(paramclass, new file(localurl.getfile()), paramstring)); } } else { localobject = paramclassloader.getresource("").getpath(); localarraylist.addall(findresources(paramclass, new file((string)localobject), paramstring)); } return localarraylist; } private static list<class<?>> findresources(class<?> paramclass, file paramfile, string paramstring) throws classnotfoundexception { arraylist localarraylist = new arraylist(); if (!paramfile.exists()) { return collections.empty_list; } file[] arrayoffile1 = paramfile.listfiles(); for (file localfile : arrayoffile1) { if (localfile.isdirectory()) { if (!localfile.getname().contains(".")) { if (!paramstring.equals("/")) { localarraylist.addall(findresources(paramclass, localfile, paramstring + "." + localfile.getname())); } else { localarraylist.addall(findresources(paramclass, localfile, localfile.getname())); } } } else if (localfile.getname().endswith(".class")) { class localclass = null; if (!paramstring.equals("/")) { localclass = class.forname(paramstring + "." + localfile.getname().substring(0, localfile.getname().length() - 6)); } else { localclass = class.forname(localfile.getname().substring(0, localfile.getname().length() - 6)); } if ((paramclass.isassignablefrom(localclass)) && (!paramclass.equals(localclass))) { localarraylist.add(localclass); } } } return localarraylist; } public static object cloneobject(object paramobject) throws exception { bytearrayoutputstream localbytearrayoutputstream = new bytearrayoutputstream(); objectoutputstream localobjectoutputstream = new objectoutputstream(localbytearrayoutputstream); localobjectoutputstream.writeobject(paramobject); bytearrayinputstream localbytearrayinputstream = new bytearrayinputstream(localbytearrayoutputstream.tobytearray()); objectinputstream localobjectinputstream = new objectinputstream(localbytearrayinputstream); return localobjectinputstream.readobject(); } public static boolean isfiletype(string paramstring1, string paramstring2) { boolean bool = false; if (("image".equals(paramstring2)) && ((paramstring1.touppercase().equals("jpg")) || (paramstring1.touppercase().equals("png")) || (paramstring1.touppercase().equals("gif")) || (paramstring1.touppercase().equals("jpeg")))) { bool = true; } return bool; } public static boolean isfiletype(string paramstring, string[] paramarrayofstring) { boolean bool = false; if ((paramarrayofstring != null) && (paramarrayofstring.length > 0)) { for (int i = 0; i < paramarrayofstring.length; i++) { if (paramstring.touppercase().equals(paramarrayofstring[i].touppercase())) { return true; } } } return bool; } public static boolean iserrorfiletype(string paramstring) { string[] arrayofstring = null; string str = appconfigutil.get("file_filter_val"); if ((appconfigutil.geticache("filefilter_key") == null) && (str != null)) { arrayofstring = str.split("\\|"); appconfigutil.seticache("filefilter_key", arrayofstring); } else { arrayofstring = (string[])appconfigutil.geticache("filefilter_key"); } return isfiletype(paramstring, arrayofstring); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: MySQL使用游标批量处理进行表操作