复制代码代码如下:
impor java.io.bufferedInputStream;
impor java.io.bufferedoutputStream;
impor java.io.bufferedReader;
impor java.io.file;
impor java.io.fileInputStream;
impor java.io.filenotfoundException;
impor java.io.fileoutputStream;
impor java.io.filewriter;
impor java.io.ioException;
impor java.io.inputstream;
impor java.io.inputStreamReader;
impor java.io.outputStream;
impor java.text.dateFormat;
impor java.text.messageFormat;
impor java.util.arraylist;
impor java.util.date;
impor java.util.enumeration;
impor java.util.list;
impor java.util.locale;
impor java.util.stringtokenizer;
impor java.util.zip.zipentry;
impor java.util.zip.zipfile;
/**
*
* @Author IBM
*
*/
Fileutil kelas publik {
PUBLIK PUBLIK STATIS DIRSPLIT = "//"; // Linux Windows
/**
* Simpan file yang diaktifkan ke Infomation Direktori Fisik
*
* @param Physicdir
* Direktori Fisik
* @param fname
* Nama file tujuan
* @param istream
* Input aliran file tujuan
* @kembali
*/
public static boolean savefilebyphysicaldir (string physicalpath,
Inputstream istream) {
bendera boolean = false;
mencoba {
OutputStream OS = FileOutputStream baru (PhysicalPath);
int readbytes = 0;
byte buffer [] = byte baru [8192];
while ((readbytes = istream.read (buffer, 0, 8192))! = -1) {
os.write (buffer, 0, readbytes);
}
os.close ();
bendera = true;
} catch (FileNotFoundException e) {
e.printstacktrace ();
} catch (ioException e) {
e.printstacktrace ();
}
pengembalian bendera;
}
Public Static Boolean CreateReRectory (String dir) {
File f = file baru (dir);
if (! f.exists ()) {
f.mkdirs ();
}
Kembali Benar;
}
public static void saveAsfileOutputStream (String PhysicalPath, String Content) {
File file = file baru (PhysicalPath);
boolean b = file.getParentFile (). isDirectory ();
if (! b) {
File tem = file baru (file.getParent ());
// tem.getParentFile (). SetWritable (true);
tem.mkdirs (); // 创建目录
}
//Log.info(file.getParent ()+";"+file.getParentFile (). IsDirectory ());
FileOutputStream foutput = null;
mencoba {
foutput = FileOutputStream baru (PhysicalPath);
foutput.write (content.getBytes ("UTF-8"));
//foutput.write (content.getbytes ());
} catch (ioException ex) {
Ex.PrintStackTrace ();
Lempar RuntimeException baru (EX);
}Akhirnya{
mencoba {
foutput.flush ();
foutput.close ();
} catch (ioException ex) {
Ex.PrintStackTrace ();
Lempar RuntimeException baru (EX);
}
}
//Log.info ("文件保存成功:"+ FisikPath);
}
/**
* Salin 文件
* @param srcfile string
* @param desfile string
* @return boolean
*/
public boolean copytofile (String srcfile, string desfile) {
File scrfile = file baru (srcfile);
if (scrfile.isfile () == true) {
panjang int;
FileInputStream fis = null;
mencoba {
fis = FileInputStream baru (scrfile);
}
Catch (FileNotFoundException Ex) {
Ex.PrintStackTrace ();
}
File desfile = file baru (desfile);
FileOutputStream fos = null;
mencoba {
fos = FileOutputStream baru (desfile, false);
}
Catch (FileNotFoundException Ex) {
Ex.PrintStackTrace ();
}
desfile = null;
panjang = (int) scrfile.length ();
byte [] b = byte baru [panjang];
mencoba {
fis.read (b);
fis.close ();
fos.write (b);
fos.close ();
}
Catch (ioException e) {
e.printstacktrace ();
}
} kalau tidak {
scrfile = null;
mengembalikan false;
}
scrfile = null;
Kembali Benar;
}
/**
* Salin 文件夹
* @param Sourcedir String
* @param destdir string
* @return boolean
*/
Public Boolean copyDir (String Sourcedir, String destdir) {
File sourceFile = file baru (Sourcedir);
String tempsource;
String tempdest;
Nama file string;
File [] file = sourceFile.listFiles ();
untuk (int i = 0; i <file.length; i ++) {
fileName = file [i] .getName ();
Tempsource = Sourcedir + "/" + nama file;
tempdest = destdir + "/" + nama file;
if (file [i] .isfile ()) {
copytofile (Tempsource, Tempdest);
} kalau tidak {
copydir (Tempsource, Tempdest);
}
}
SourceFile = null;
Kembali Benar;
}
/**
* 删除指定目录及其中的所有内容。
* @param dir 要删除的目录
* @return 删除成功时返回 true , false。
*/
public boolean deletedirectory (file dir) {
File [] entri = dir.listFiles ();
int sz = entri.length;
untuk (int i = 0; i <sz; i ++) {
if (entri [i] .isdirectory ()) {
if (! deletedirectory (entri [i])) {
mengembalikan false;
}
} kalau tidak {
if (! Entries [i] .delete ()) {
mengembalikan false;
}
}
}
if (! dir.delete ()) {
mengembalikan false;
}
Kembali Benar;
}
/**
* File ada cek
*
* nama file @param sfilename
* @return boolean true - ada <br>
* false - tidak ada
*/
Checkexist boolean statis publik (string sfilename) {
Hasil boolean = false;
mencoba {
File f = file baru (SFileName);
// if (f.exists () && f.isfile () && f.canread ()) {
if (f.exists () && f.isfile ()) {
Hasil = Benar;
} kalau tidak {
hasil = false;
}
} catch (Exception e) {
hasil = false;
}
/* kembali */
hasil pengembalian;
}
/**
* Dapatkan ukuran file
*
* nama file @param sfilename
* @return Long File's Size (byte) saat file tidak ada pengembalian -1
*/
public static long getSize (string sfileName) {
LSIZE Long = 0;
mencoba {
File f = file baru (SFileName);
//ada
if (f.exists ()) {
if (f.isfile () && f.canread ()) {
lsize = f.length ();
} kalau tidak {
lsize = -1;
}
// tidak ada
} kalau tidak {
lsize = 0;
}
} catch (Exception e) {
lsize = -1;
}
/* kembali */
mengembalikan lsize;
}
/**
* File Delete
*
* @param sfilename file nmae
* @return boolean true - hapus kesuksesan <br>
* false - hapus gagal
*/
public static boolean deletefromname (string sfileName) {
Boolean Breturn = true;
mencoba {
File ofile = file baru (sfileName);
//ada
if (ofile.exists ()) {
// Hapus file
Boolean Bresult = Ofile.Delete ();
// hapus gagal
if (bresult == false) {
Breturn = false;
}
// tidak ada
} kalau tidak {
}
} catch (Exception e) {
Breturn = false;
}
//kembali
Kembalikan Breturn;
}
/**
* File Unzip
*
* @param Stopath Unzip Directory Path
* @param szipfile nama file unzip
*/
@SuppressWarnings ("RawTypes")
public static void releasezip (string stopath, string szipfile) melempar Exception {
if (null == stopath || (""). Equals (stopath.trim ())) {
File objzipfile = file baru (szipfile);
stopath = objzipfile.getParent ();
}
Zipfile zfile = zipfile baru (szipfile);
Enumeration zlist = zfile.entries ();
Zipentry ze = null;
byte [] buf = byte baru [1024];
while (zlist.hasmoreElements ()) {
ze = (zipentry) zlist.nextelement ();
if (ze.isdirectory ()) {
melanjutkan;
}
OutputStream OS =
BufferedOutputStream baru (
FileOutputStream baru (getRealFileName (stopath, ze.getName ())));
InputStream IS = BufferedInputStream baru (zfile.getInputStream (ZE));
int readlen = 0;
while ((readlen = is.read (buf, 0, 1024))! = -1) {
os.write (buf, 0, readlen);
}
is.close ();
os.close ();
}
zfile.close ();
}
/**
* GetRealFileName
*
* Direktori Root Berbasis @param
* @param absfilename nama file direktori absolut
* @return java.io.file file return
*/
@SuppressWarnings ({"RawTypes", "Uncecked"})
File statis pribadi getRealFileName (String Basedir, String AbsfileName) melempar Exception {
File ret = null;
Daftar dir = arraylist baru ();
StringTokenizer st = new StringTokenizer (absfileName, System.getProperty ("File.Separator"));
while (st.hasmoretokens ()) {
Dirs.add (St.NextToken ());
}
ret = file baru (berbasis);
if (dirs.size ()> 1) {
untuk (int i = 0; i <dirs.size () - 1; i ++) {
ret = file baru (ret, (string) dirs.get (i));
}
}
if (! ret.exists ()) {
ret.mkdirs ();
}
ret = file baru (ret, (string) dirs.get (dirs.size () - 1));
Return Ret;
}
/**
* CopyFile
*
* file sumber @param srcfile
* file target target @param
*/
@Suppresswarnings ("sumber daya")
statis public void copyFile (String Srcfile, String TargetFile) melempar IOException
{
FileInputStream Reader = FileInputStream baru (SRCFile);
FileOutputStream writer = baru fileOutputStream (targetFile);
byte [] buffer = byte baru [4096];
int len;
mencoba
{
reader = FileInputStream baru (srcfile);
writer = FileOutputStream baru (TargetFile);
while ((len = reader.read (buffer))> 0)
{
writer.write (buffer, 0, len);
}
}
Catch (ioException e)
{
lempar e;
}
Akhirnya
{
if (writer! = null) writer.close ();
if (reader! = null) reader.close ();
}
}
/**
* RenameFile
*
* file sumber @param srcfile
* file target target @param
*/
statis public void renameFile (String srcfile, string targetfile) melempar ioException
{
mencoba {
CopyFile (SrcFile, TargetFile);
deletefromname (srcfile);
} catch (ioException e) {
lempar e;
}
}
public static void write (string tivolimsg, string logfileName) {
mencoba{
byte [] bmsg = tivolimsg.getbytes ();
FileOutputStream fout = new FileOutputStream (logFileName, true);
fout.write (bmsg);
fout.close ();
} catch (ioException e) {
// Lemparkan pengecualian
}
}
/**
* Metode ini digunakan untuk mencatat pesan dengan cap waktu, kode kesalahan dan detail metode
* @param errorcd string
* @param classname string
* @param MethodName String
* @param msg string
*/
public static void writelog (String LogFile, String Batchid, String ExceptionInfo) {
DateFormat df = dateFormat.getDateTimeInstance (dateFormat.default, dateFormat.default, locale.japanese);
Objek args [] = {df.format (tanggal baru ()), batchid, exceptionInfo};
String fmtmsg = messageFormat.Format ("{0}: {1}: {2}", args);
mencoba {
File logfile = file baru (logfile);
if (! logfile.exists ()) {
logfile.createNewFile ();
}
FileWriter fw = FileWriter baru (logfile, true);
fw.write (fmtmsg);
fw.write (System.getProperty ("line.separator"));
fw.flush ();
fw.close ();
} catch (Exception e) {
}
}
Public Static String ReadTextFile (String RealPath) melempar Exception {
File file = file baru (realpath);
if (! file.exists ()) {
System.out.println ("File tidak ada!");
kembali nol;
}
BufferedReader BR = BufferedReader baru (InputStreamReader baru (FileInputStream baru (RealPath), "UTF-8")));
String temp = "";
String txt = "";
while ((temp = br.readline ())! = null) {
txt+= temp;
}
br.close ();
mengembalikan txt;
}
}