package com.citic.util.comm; import java.io.*; import java.nio.channels.FileLock; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Scanner; import com.citic.util.comm.*; import com.citic.util.MD5Util; import com.citic.util.XMLUtil; import com.sun.org.apache.xml.internal.serialize.LineSeparator; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class FileOperation implements{IConstants Private static int debuglevel = 0; // base config method banned in debuglevel, this variable is used here only for the unified use of public static void propertity2File (srcfile String, String desfile) { InputStream in = null ; BufferedInputStream the INPUT = null ; BufferedOutputStream The Output = null ; CommFun.log (debuglevel, SRCFILE); the try { iF ( new new file (desfile) .exists ()) { of System.out.print ( ? "profile already exists, whether the cover (y / n ) " ); Scanner scanner = new Scanner(System.in); String str = scanner.nextLine().toUpperCase(); System.out.println("str is [" + str + "]"); if ("".equals(str) || str == null || "N".equals(str)) { return; } } in = FileOperation.class.getResourceAsStream(srcfile); input = new BufferedInputStream(in); System.out.println("input.ava:"+input.available()); output=new BufferedOutputStream(new FileOutputStream(desfile)); int r; byte[] bytes=new byte[1024]; while((r=input.read(bytes))!=-1){ output.write(bytes, 0, r); CommFun.log(debuglevel,"input:"+r); } output.flush(); CommFun.log(debuglevel,desfile + "config file is created!"); } catch (Exception e) { throw new RuntimeException(e); } finally { try { if (output != null) output.close(); if (input != null) input.close(); if (in != null) in.close(); } catch (Exception e) { throw new RuntimeException(e); } } } public static void propertity2File1(String srcfile, String desfile) { InputStreamReader reader=null; //FileReader reader=null; BufferedReader breader=null; CommFun.log(debuglevel, srcfile); try { if (new File(desfile).exists()) { System.out.print("配置文件已存在,是否覆盖?(y/n)"); Scanner scanner = new Scanner(System.in); String str = scanner.nextLine().toUpperCase(); System.out.println("str is [" + str + "]"); if ("".equals(str) || str == null || "N".equals(str)) { return; } } reader=new InputStreamReader(FileOperation.class.getResourceAsStream(srcfile),"Unicode"); breader=new BufferedReader(reader); String tmps=null; while((tmps=breader.readLine())!=null){ System.out.println(tmps); } CommFun.log(debuglevel,desfile + "config file is created!"); } catch (Exception e) { throw new RuntimeException(e); } finally { // try { // if (output != null) // output.close(); // if (input != null) // input.close(); // if (reader != null) // reader.close(); // } catch (Exception e) { // a RuntimeException new new the throw (E); // } } } / ** * acquire exclusive profile lock, using the single-process * @return * / public static Boolean a GETLOCK () { String File = the System.getProperty ( "User. the dir. ") TRIM () + the File.separator . + + config" LCK " ; Boolean lockflag = to true ; // the FileLock LCK = null; // {the try // a FileOutputStream fos = new new a FileOutputStream (File); // LCK = fos .getChannel () tryLock ().; // CommFun.log(INFO, "对文件加锁成功[" + file + "]"); // } catch (Exception e) { // e.printStackTrace(); // throw new RuntimeException(); // } // return lck == null ? false : true; File flagFile=new File(file); try { if(false==flagFile.createNewFile()){ lockflag=false; } } catch (IOException e) { lockflag= To false ; e.printStackTrace (); } flagFile.deleteOnExit (); return lockflag; } / ** * the StringBuffer [] array is written to the specified file * * @param STB * @param filename * / public static void stringbuffer2file (the StringBuffer [] STB, String filename) { stringbuffer2file (STB, filename, null ); } / ** * @param STB * @param filename * @paramstrmd5 * added incoming strmd5, due to the calculated value is different, so it passed directly, MQUtil.java in * / public static void stringbuffer2file (the StringBuffer [] STB, String filename, String strmd5) { the try { String pthseparator = the System.getProperty ( "File.separator" ); // filename = filename.replace ( "/", "\\"); String localpth filename.substring = (0 , filename.lastIndexOf (pthseparator)); CommFun.log ( localpth); File F = new new File (localpth); IF (! f.exists ()) { f.mkdir (); } // FileWriter fw = new FileWriter(filename,true); // for (int i = 0; i < stb.length && stb[i] != null; i++) { // fw.write(stb[i].toString() + "\n"); // } // fw.flush(); // fw.close(); StringBuffer tmpstr1 = new StringBuffer(); for (int i = 0; i < stb.length && stb[i] != null; i++) { tmpstr1.append(stb[i]).append(System.getProperty("line.separator")); } //For XML create xml files go method, or the creation of 20,170,901 Zhang Mingwei use common file // new methods and determine IF (filename.matches (. "\\ *. Xml" )) { XMLUtil.xmlCreate (tmpstr1.toString (), filename ); } the else { the writeFile (tmpstr1.toString (), filename); } tmpstr1 = null ; IF (! ( new new file (filename) .length ()> 0 )) { CommFun.log (the INFO, "write files" + filename + "fail or no data!" ); return ; } CommFun.log (the INFO, "write to file" + filename + "success!"); if(strmd5!=null && !"".equals(strmd5) && ConfigFileUtil.getValue("md5")=="true"){ String filemd5=MD5Util.getMD5String(filename); String tmpstr="filename:"+filename+",strmd5:"+strmd5+",filemd5:"+filemd5; CommFun.log(tmpstr); String md5file=localpth+File.separator+"MD5.txt"; writeFile(tmpstr,md5file); } } catch (Exception e) { CommFun.log(ERR, "写入文件"+filename+"失败!"); throw new RuntimeException(e); } } /** * 一般文件写入 * @param tmpstr * @param filename */ public static void writeFile(String tmpstr,String filename){ try{ FileWriter fw1 = new FileWriter(filename,true); fw1.write(tmpstr + "\n"); fw1.flush(); fw1.close(); CommFun.log(debuglevel,"写入"+filename+"成功!"); }catch(Exception E) { e.printStackTrace (); } } public static String GetImageStr (String imgfile) { // the array image file into a string of bytes, Base64 encoding process and subjected // String imgfile = "D: 01.jpg \\ "; // be image processed the InputStream in = null ; byte [] Data = null ; // read byte array images the try { File FL = new new File (imgfile); CommFun.log (debuglevel, fl.getName ()); in = new new the FileInputStream (imgfile); Data = new new byte [in.available ()]; in.read (Data); in.close (); } the catch (IOException E) { e.printStackTrace (); } // byte array Base64 encoding BASE64Encoder encoder = new new Base64Encoder (); return encoder.encode (Data); // returns the byte array Base64 encoded string } public static Boolean GenerateImage (imgStr string, string filename) { // byte array Base64 string and decodes images generated IF (imgStr == null ) //The image data is empty return to false ; Base64Decoder Decoder = new new Base64Decoder (); the try { // the Base64 decoding byte [] B = decoder.decodeBuffer (imgStr); for ( int I = 0; I <to b.length; ++ I) { IF (B [I] <0) { // adjust abnormality data B [I] + = 256 ; } } the SimpleDateFormat. ft = new new the SimpleDateFormat ( "yyyyMMddHHmmssSSS" ); a Date dt =new Date(); // 生成jpeg图片 String imgFilePath = filename+"_"+ft.format(dt);// 新生成的图片 OutputStream out = new FileOutputStream(imgFilePath); out.write(b); out.flush(); out.close(); return true; } catch (Exception e) { return false; } } public static HashMap<String, String> loadMD5txt(String path) { HashMap<String, String> md5map = new HashMap<String, String>(); String md5file = path + File.separator + md5filename; CommFun.log(debuglevel, "MD5文件:" + md5file); if (new File(md5file).exists()) { CommFun.log(debuglevel, "MD5文件存在," + md5file); try { BufferedReader bufferreader = new BufferedReader( new FileReader(md5file)); String fline; while ((fline = bufferreader.readLine()) != null) { if (!"".equals(fline) && fline != null) { md5map.put(fline.split(",")[1].split(":")[1], ""); } } CommFun.log("共载入MD5[" + md5map.size() + "]条记录"); // CommFun.log(debuglevel,""+md5map); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return md5map; } }