1. Export the CSV file
/** * @Author Lin.hongwen * @Date 2018/05/29 * Export the CVS file to the specified directory * * @param the file CSV file (path + file name), the CSV file does not exist from Create * @param dataList data * @return*/ Public StaticBoolean exportcsvfile (file file, list<string>dataList) {Boolean issucess=false; FileOutputStream out=NULL; OutputStreamWriter OSW=NULL; BufferedWriter BW=NULL; Try { out=Newfileoutputstream (file); OSW=NewOutputStreamWriter ( out); Bw=NewBufferedWriter (OSW); if(datalist!=NULL&&!Datalist.isempty ()) { for(String data:datalist) {bw.append (data). Append ("\ r"); }} issucess=true; } Catch(Exception e) {issucess=false; }finally{ if(bw!=NULL){ Try{bw.close (); Bw=NULL; } Catch(IOException e) {e.printstacktrace (); } } if(osw!=NULL){ Try{osw.close (); OSW=NULL; } Catch(IOException e) {e.printstacktrace (); } } if( out!=NULL){ Try { out. Close (); out=NULL; } Catch(IOException e) {e.printstacktrace (); } } } returnissucess; }
2. Export download files from the specified directory
OutputStream out=NULL; FileInputStreaminch=NULL; Try{File File=NewFile (filepath+fileName); //take the file name by UTF-8 and press Iso8859-1 code, to ensure that the file name in the pop-up window is not garbled, Chinese not too much, support up to 17 Chinese, because the header has 150 bytes limit. FileName =NewString (Filename.getbytes ("UTF-8"),"iso8859-1"); //tells the browser that the output content is a streamResponse.setcontenttype ("Application/octet-stream"); //The type specified in content-disposition is the extension of the file, and the file type picture in the pop-up download dialog box is displayed according to the file's extension, and after the point is saved, the file is named with the value of filename, and the save type is set in Content. Note: Be sure to set the Content-type header field before you set the Content-disposition header field. Response.AddHeader ("content-disposition","attachment;filename="+fileName); //Set Content LengthResponse.setheader ("Content-length", String.valueof (File.length ())); out=Response.getoutputstream (); inch=Newfileinputstream (file); byte[] B =New byte[1024x768]; intN; while((n=inch. Read (b))!=-1){ out. Write (b,0, N); } } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally { Try { inch. Close (); inch=NULL; } Catch(IOException e) {e.printstacktrace (); } Try { out. Close (); out=NULL; } Catch(IOException e) {e.printstacktrace (); } }
Java file Operations