复制代码代码如下:
นำเข้า Java.io.BufferedInputStream;
นำเข้า Java.io.BufferedOutputStream;
นำเข้า java.io.bufferedreader;
นำเข้า Java.io.File;
นำเข้า Java.io.FileInputStream;
นำเข้า java.io.filenotfoundException;
นำเข้า Java.io.FileOutputStream;
นำเข้า java.io.filewriter;
นำเข้า java.io.ioException;
นำเข้า Java.io.InputStream;
นำเข้า Java.io.InputStreamReader;
นำเข้า Java.io.OutputStream;
นำเข้า java.text.dateFormat;
นำเข้า java.text.messageFormat;
นำเข้า java.util.arraylist;
นำเข้า java.util.date;
นำเข้า Java.util.enumeration;
นำเข้า java.util.list;
นำเข้า java.util.locale;
นำเข้า java.util.stringTokenizer;
นำเข้า java.util.zip.zipentry;
นำเข้า java.util.zip.zipfile;
-
-
* @author IBM
-
-
คลาสสาธารณะ FileUtil {
สตริงคงที่สาธารณะ dirsplit = "//"; // linux windows
-
* บันทึกไฟล์สะสมไปยัง infomation ไดเรกทอรีทางกายภาพ
-
* @param physicaldir
* ไดเรกทอรีทางกายภาพ
* @param fname
* ชื่อไฟล์ปลายทาง
* @param istream
* อินพุตสตรีมของไฟล์ปลายทาง
* @กลับ
-
Public Static Boolean SaveFileByphysicalDir (String PhysicalPath
อินพุตสตรีมสตรีม) {
ธงบูลีน = เท็จ;
พยายาม {
OutputStream OS = ใหม่ FileOutputStream (PhysicalPath);
int readbytes = 0;
บัฟเฟอร์ไบต์ [] = ไบต์ใหม่ [8192];
ในขณะที่ ((readbytes = istream.read (บัฟเฟอร์, 0, 8192))! = -1) {
OS.WRITE (บัฟเฟอร์, 0, readbytes);
-
os.close ();
ธง = จริง;
} catch (filenotfoundexception e) {
E.PrintStackTrace ();
} catch (ioexception e) {
E.PrintStackTrace ();
-
ธงกลับ;
-
Public Static Boolean CreateTirectory (String Dir) {
ไฟล์ f = ไฟล์ใหม่ (dir);
ถ้า (! f.exists ()) {
f.mkdirs ();
-
กลับมาจริง;
-
โมฆะคงที่สาธารณะ SaveAsFileOutputStream (String PhysicalPath เนื้อหาสตริง) {
ไฟล์ไฟล์ = ไฟล์ใหม่ (PhysicalPath);
บูลีน b = file.getParentFile (). isDirectory ();
ถ้า (! b) {
ไฟล์ tem = ไฟล์ใหม่ (file.getParent ());
// tem.getParentFile (). setWatable (จริง);
Tem.Mkdirs (); // 创建目录
-
//log.info(file.getParent ()+";"+file.getParentFile (). isDirectory ());
fileOutputStream foutput = null;
พยายาม {
foutput = ใหม่ fileOutputStream (physicalpath);
foutput.write (content.getBytes ("UTF-8"));
//foutput.write (content.getBytes ());
} catch (ioexception ex) {
Ex.PrintStackTrace ();
โยน RuntimeException ใหม่ (Ex);
}ในที่สุด{
พยายาม {
foutput.flush ();
foutput.close ();
} catch (ioexception ex) {
Ex.PrintStackTrace ();
โยน RuntimeException ใหม่ (Ex);
-
-
//log.info ("文件保存成功:"+ PhysicalPath);
-
-
* คัดลอก文件
* @param srcfile String
* @param desfile String
* @return boolean
-
Public Boolean CopyTofile (String srcfile, String desfile) {
ไฟล์ scrfile = ไฟล์ใหม่ (srcfile);
if (scrfile.isfile () == true) {
ความยาว int;
FileInputStream FIS = NULL;
พยายาม {
FIS = ใหม่ FileInputStream (SCRFILE);
-
Catch (filenotfoundException ex) {
Ex.PrintStackTrace ();
-
ไฟล์ desfile = ไฟล์ใหม่ (desfile);
fileOutputStream fos = null;
พยายาม {
FOS = ใหม่ fileOutputStream (desfile, false);
-
Catch (filenotfoundException ex) {
Ex.PrintStackTrace ();
-
desfile = null;
ความยาว = (int) scrfile.length ();
ไบต์ [] b = ไบต์ใหม่ [ความยาว];
พยายาม {
fis.read (b);
fis.close ();
fos.write (b);
fos.close ();
-
จับ (ioexception e) {
E.PrintStackTrace ();
-
} อื่น {
scrfile = null;
กลับเท็จ;
-
scrfile = null;
กลับมาจริง;
-
-
* คัดลอก文件夹
* @Param Sourcedir String
* @param String String
* @return boolean
-
Public Boolean CopyDir (String Sourcedir, String Destdir) {
File SourceFile = ไฟล์ใหม่ (sourcedir);
String Tempsource;
สตริง tempdest;
ชื่อไฟล์สตริง;
ไฟล์ [] files = sourcefile.listfiles ();
สำหรับ (int i = 0; i <files.length; i ++) {
filename = ไฟล์ [i] .getName ();
tempsource = sourcedir + "/" + ชื่อไฟล์;
tempdest = destDir + "/" + ชื่อไฟล์;
ถ้า (ไฟล์ [i] .isfile ()) {
CopyTofile (Tempsource, Tempdest);
} อื่น {
Copydir (Tempsource, Tempdest);
-
-
SourceFile = null;
กลับมาจริง;
-
-
* 删除指定目录及其中的所有内容。
* @param dir 要删除的目录
* @return 删除成功时返回 True, 否则返回เท็จ。
-
บูลีนสาธารณะ deletedirectory (ไฟล์ dir) {
ไฟล์ [] รายการ = dir.listfiles ();
int sz = entries.length;
สำหรับ (int i = 0; i <sz; i ++) {
ถ้า (รายการ [i] .isdirectory ()) {
if (! deletedirectory (รายการ [i])) {
กลับเท็จ;
-
} อื่น {
ถ้า (! รายการ [i] .delete ()) {
กลับเท็จ;
-
-
-
if (! dir.delete ()) {
กลับเท็จ;
-
กลับมาจริง;
-
-
* มีการตรวจสอบไฟล์
-
* @param sfilename ชื่อไฟล์
* @return boolean จริง - มีอยู่ <br>
* เท็จ - ไม่มีอยู่จริง
-
Public Static Boolean checkexist (String sfilename) {
ผลลัพธ์บูลีน = false;
พยายาม {
ไฟล์ f = ไฟล์ใหม่ (sfilename);
// ถ้า (f.exists () && f.isfile () && f.canread ()) {
if (f.exists () && f.isfile ()) {
ผลลัพธ์ = จริง;
} อื่น {
ผลลัพธ์ = เท็จ;
-
} catch (Exception e) {
ผลลัพธ์ = เท็จ;
-
/* กลับ */
ผลการกลับมา;
-
-
* รับขนาดไฟล์
-
* @param sfilename ชื่อไฟล์
* @return ขนาดไฟล์ยาว (ไบต์) เมื่อไม่มีไฟล์ส่งคืน -1
-
Public Static Long Getsize (String sfilename) {
lsize ยาว = 0;
พยายาม {
ไฟล์ f = ไฟล์ใหม่ (sfilename);
//มีอยู่
ถ้า (f.exists ()) {
if (f.isfile () && f.canread ()) {
lsize = f.length ();
} อื่น {
lsize = -1;
-
// ไม่มีอยู่จริง
} อื่น {
lsize = 0;
-
} catch (Exception e) {
lsize = -1;
-
/* กลับ */
กลับ lsize;
-
-
* การลบไฟล์
-
* @param sfilename ไฟล์ nmae
* @return บูลีนจริง - ลบความสำเร็จ <br>
* เท็จ - ลบล้มเหลว
-
บูลีนแบบคงที่สาธารณะ deleeteFromName (String sfilename) {
บูลีน Breturn = true;
พยายาม {
ไฟล์ของไฟล์ = ไฟล์ใหม่ (sfilename);
//มีอยู่
ถ้า (OFILE.EXISTS ()) {
// ลบไฟล์
บูลีน bresult = ofile.delete ();
// ลบล้มเหลว
ถ้า (bresult == false) {
breturn = false;
-
// ไม่มีอยู่จริง
} อื่น {
-
} catch (Exception e) {
breturn = false;
-
//กลับ
กลับ Breturn;
-
-
* File UNZIP
-
* @param stopath unzip directory path
* @param szipfile unzip ชื่อไฟล์
-
@suppresswarnings ("Rawtypes")
โมฆะคงที่สาธารณะ Releasezip (String Stopath, String SzipFile) โยนข้อยกเว้น {
if (null == stopath || (""). เท่ากับ (stopath.trim ())) {
ไฟล์ objzipfile = ไฟล์ใหม่ (szipfile);
stopath = objzipfile.getParent ();
-
zipfile zfile = zipfile ใหม่ (szipfile);
การแจงนับ zlist = zfile.entries ();
Zipentry Ze = null;
ไบต์ [] buf = ไบต์ใหม่ [1024];
ในขณะที่ (zlist.hasmoreElements ()) {
ze = (zipentry) zlist.nextelement ();
if (ze.isdirectory ()) {
ดำเนินการต่อ;
-
OutputStream OS =
ใหม่ bufferedOutputStream (
ใหม่ fileOutputStream (getRealFileName (stopath, ze.getName ())));
inputStream คือ = ใหม่ bufferedInputStream (zfile.getInputStream (ze));
int readlen = 0;
ในขณะที่ ((readlen = is.read (buf, 0, 1024))! = -1) {
OS.WRITE (buf, 0, readlen);
-
is.close ();
os.close ();
-
zfile.close ();
-
-
* getRealFileName
-
* ไดเรกทอรีรากที่ใช้ @param
* @param absfilename ชื่อไฟล์สัมปณ์ Absolute
* @return java.io.file return file
-
@suppresswarnings ({"rawtypes", "unchected"})
ไฟล์สแตติกส่วนตัว getRealFileName (String achedir, String absfilename) โยนข้อยกเว้น {
ไฟล์ ret = null;
รายการ dirs = new ArrayList ();
StringTokenizer ST = new StringTokenizer (absfilename, system.getProperty ("file.Separator"));
ในขณะที่ (St.Hasmoretokens ()) {
dirs.add (st.nexttoken ());
-
ret = ไฟล์ใหม่ (พื้นฐาน);
if (dirs.size ()> 1) {
สำหรับ (int i = 0; i <dirs.size () - 1; i ++) {
ret = ไฟล์ใหม่ (ret, (string) dirs.get (i));
-
-
if (! ret.exists ()) {
ret.mkdirs ();
-
ret = ไฟล์ใหม่ (ret, (string) dirs.get (dirs.size () - 1));
return ret;
-
-
* CopyFile
-
* @Param Srcfile Source File
* @Param TargetFile Target File
-
@suppresswarnings ("ทรัพยากร")
โมฆะสาธารณะคงที่ copyfile (String srcfile, String targetFile) พ่น IOException
-
fileInputStream reader = new FileInputStream (SRCFile);
fileOutputStream writer = ใหม่ fileOutputStream (TargetFile);
ไบต์ [] บัฟเฟอร์ = ไบต์ใหม่ [4096];
int len;
พยายาม
-
reader = ใหม่ fileInputStream (srcfile);
Writer = ใหม่ fileOutputStream (TargetFile);
ในขณะที่ ((len = reader.read (บัฟเฟอร์))> 0)
-
Writer.write (บัฟเฟอร์, 0, len);
-
-
จับ (ioexception e)
-
โยน e;
-
ในที่สุด
-
if (writer! = null) writer.close ();
if (reader! = null) reader.close ();
-
-
-
* RenameFile
-
* @Param Srcfile Source File
* @Param TargetFile Target File
-
โมฆะสาธารณะคงที่ renamefile (String srcfile, String targetFile) พ่น IOException
-
พยายาม {
CopyFile (srcfile, TargetFile);
DELETEFROMNAME (SRCFILE);
} catch (ioexception e) {
โยน e;
-
-
โมฆะคงที่สาธารณะเขียน (String tivolimsg, String logfilename) {
พยายาม{
ไบต์ [] bmsg = tivolimsg.getBytes ();
fileOutputStream fout = ใหม่ fileOutputStream (logFileName, true);
fout.write (BMSG);
fout.close ();
} catch (ioexception e) {
// โยนข้อยกเว้น
-
-
-
* วิธีนี้ใช้เพื่อบันทึกข้อความด้วยการประทับเวลารหัสข้อผิดพลาดและรายละเอียดวิธีการ
* @param errorcd string
* @param classname string
* @param methodname string
* @param msg string
-
โมฆะคงที่สาธารณะ writelog (สตริง logfile, String batchid, String ExceptionInfo) {
DateFormat DF = DateFormat.getDateTimeInstance (DateFormat.default, DateFormat.default, locale.japanese);
Object args [] = {df.format (วันที่ใหม่ ()), batchid, exceptioninfo};
สตริง fmtmsg = messageFormat.format ("{0}: {1}: {2}", args);
พยายาม {
ไฟล์ logfile = ไฟล์ใหม่ (logfile);
if (! logfile.exists ()) {
logfile.createnewfile ();
-
FileWriter FW = ใหม่ fileWriter (logfile, true);
fw.write (fmtmsg);
fw.write (system.getProperty ("line.separator"));
fw.flush ();
fw.close ();
} catch (Exception e) {
-
-
String String สาธารณะ readTextFile (String RealPath) โยนข้อยกเว้น {
ไฟล์ไฟล์ = ไฟล์ใหม่ (RealPath);
if (! file.exists ()) {
System.out.println ("ไม่มีไฟล์!");
คืนค่า null;
-
bufferedReader br = new bufferedReader (ใหม่ inputStreamReader (ใหม่ FileInputStream (RealPath), "UTF-8"));
String temp = "";
String txt = "";
ในขณะที่ ((temp = br.readline ())! = null) {
txt+= อุณหภูมิ;
-
br.close ();
กลับ txt;
-
-