Java file Operations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.