Package Com.saicfc.pmpf.internal.manage.utils;import Java.io.file;import Java.io.filewriter;import Java.io.ioexception;import Java.util.iterator;import Java.util.linkedhashmap;import Java.util.List;import com.saicfc.pmpf.common.enums.channelcodeenums;/** * Generate TXT file * @author Lizhiyong * @version $Id: Txtutils.java, v 0.12014 years September 11 Morning 8:40:02 EXP $ */public class Txtutils {@SuppressWarnings ("rawtypes") public static File Writetxtfile (String Channelcode, List ContentList, Linkedhashmap Map, String FilePath, String readstr, Str ing FileName) throws IOException { File File = new file (FilePath); if (!file.exists ()) {File.mkdir (); System.out.println ("folder created"); }//define file name format and create file txtfile = file.createtempfile (filename, ". txt", new file (FilePath)); Read the contents of the original file, and then write the operation FileWriter writer = null; if (Commonutils.isnotempty (map)) {//write header information String Filein = ""; for (Iterator tileiterator = Map.entryset (). Iterator (); Tileiterator.hasnext ();) {Java.util.Map.Entry propertyentry = (java.util.Map.Entry) tileiterator.next (); Filein + = (String) propertyentry.getvalue () = = null? "": (String) propertyentry. GetValue (); if (Tileiterator.hasnext ()) {Filein = Filein + ""; }} try {writer = new FileWriter (txtfile, true); Writer.write (Filein); } catch (IOException E1) {e1.printstacktrace (); } finally {if (writer! = null) {try {writer.close (); } catch (IOException E2) {e2.printstacktrace (); } } } } Write details int num = 1; for (Iterator Iterator = Contentlist.iterator (); Iterator.hasnext ();) {Linkedhashmap Txtmap = (linkedhashmap) iterator.next (); String filein1 = ""; for (Iterator propertyiterator = Txtmap.entryset (). Iterator (); propertyiterator. Hasnext ();) {Java.util.Map.Entry propertyentry = (java.util.Map.Entry) propertyiterator.next (); Filein1 + = (String) propertyentry.getvalue () = = null? "": (String) propertyentry. GetValue (); if (Propertyiterator.hasnext ()) {filein1 = filein1 + readstr; }} try {System.out.println ("Data filein1:" + filein1); Generate serial number for CCB if (ChannelCodeEnums.CCB.getChannelCode (). Equals (Channelcode)) {filein1 = Str Ing.valueof (num) + filein1; } writer = new FileWriter (txtfile, true); Writer.write (filein1); Bank of China ends with a line break if (ChannelCodeEnums.BOC.getChannelCode (). Equals (Channelcode)) {WRITER.WR ITE ("\ r \ n"); } else if (ChannelCodeEnums.CIB.getChannelCode (). Equals (Channelcode)) {writer.write ("\ r \ n"); } else {if (num < contentlist.size ()) {writer.write ("\ r \ n"); } num++; }} catch (IOException E1) {e1.printstacktrace (); } finally {if (writer! = null) {try {writer.close (); } catch (IOException E2) {e2.printstacktrace (); }}}} return txtfile; }}
BigDecimal amountdecimal = new BigDecimal (0); for (Iterator Iterator = Exportdata.iterator (); Iterator.hasnext ();) {Linkedhashmap Cebmap = (linkedhashmap) iterator.next (); String amount = (string) cebmap.get ("Amount"); BigDecimal AmountDecimal2 = bigdecimal.valueof (double.parsedouble (amount)). Setscale (2, Bigdecimal.roun D_HALF_UP); Amountdecimal = Amountdecimal.add (AMOUNTDECIMAL2); } int cebdatasize = Exportdata.size (); Linkedhashmap Amounthashmap = new Linkedhashmap (); Amounthashmap.put ("Amount", amountdecimal.toplainstring ()); Exportdata.add (0, Amounthashmap); Linkedhashmap Sizehashmap = new Linkedhashmap (); Sizehashmap.put ("TotalCount", String.valueof (cebdatasize)); Exportdata.add (1, Sizehashmap); String readstr = ","; String fileName = "Everbright Bank (CEB) refund data"; Create File FilE file = Txtutils.writetxtfile (Channelcode, ExportData, map, FilePath, Readstr, fileName); Download file DownLoadFile (response, FilePath, file);
/** * Download file * @param response * @param filePath file path * @param files file * @throws ioexception< c8/>*/public void DownLoadFile (httpservletresponse response, String filePath, file file) throws IOException { String fileName = File.getname (); Download file filemanageutils.exportfile (response, FilePath + filename, filename); Delete a single file filemanageutils.deletefile (FilePath, fileName); }
Package Com.saicfc.pmpf.internal.manage.utils;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.inputstream;import Java.io.OutputStream; Import javax.servlet.http.httpservletresponse;/** * File Management * @author Lizhiyong * @version $Id: Filemanageutils.java, v 0.120 September 11, 14 morning 9:37:47 EXP $ */public class Filemanageutils {/** * Download file * @param response * @param csvfilepath * File path * @param filename * file name * @throws IOException */public static V OID Exportfile (httpservletresponse response, String Csvfilepath, String fileName) Throws IOException {Response.setcontenttype ("Applicatio N/CSV;CHARSET=GBK "); Response.setheader ("Content-disposition", "attachment; Filename= "+ New String (Filename.getbytes (" GBK ")," iso8859-1 ")); UrlenCoder.encode (FileName, "GBK") inputstream in = null; try {in = new FileInputStream (Csvfilepath); int len = 0; byte[] buffer = new byte[1024]; Response.setcharacterencoding ("GBK"); OutputStream out = Response.getoutputstream (); while (len = in.read (buffer)) > 0) {//out.write (new byte[] {(byte) 0xEF, (Byte) 0xBB, (byte) 0xBF}); Out.write (buffer, 0, Len); }} catch (FileNotFoundException e) {System.out.println (e); } finally {if (in! = null) {try {in.close (); } catch (Exception e) {throw new RuntimeException (e); }}}}/** * Delete all files under this directory FilePath * @param filePath * File directory path */Publ IC static void Deletefiles (String filePath) {File File = new file (FilePath); if (File.exists ()){file[] files = file.listfiles (); for (int i = 0; i < files.length; i++) {if (Files[i].isfile ()) {files[i].delete (); }}}}/** * Delete individual files * @param filePath * file directory path * @param fi Lename * File name */public static void DeleteFile (String filePath, String fileName) {File File = N EW File (FilePath); if (file.exists ()) {file[] files = file.listfiles (); for (int i = 0; i < files.length; i++) {if (Files[i].isfile ()) {if (Files[i].getnam E (). Equals (FileName) {files[i].delete (); Return } } } } }}
Java generate TXT file and download