Java input and Output stream summary

Source: Internet
Author: User

1. Use Bufferedinputstream to read the file stream and Bufferedoutputstream write the file stream:

protected Static voidwriteFile2 (String InputPath, String outputPath) {Bufferedinputstream bis=NULL; Bufferedoutputstream Bos=NULL; Try{bis=NewBufferedinputstream (NewFileInputStream (InputPath)); Bos=NewBufferedoutputstream (NewFileOutputStream (OutputPath)); intBuffer = 1024 * 1024; byte[] data =New byte[buffer]; intLen;  while(len = bis.read (data))! =-1) {bos.write (data,0, Len); }        } Catch(Exception e) {e.printstacktrace (); } finally {            Try {                if(bis! =NULL) {bis.close (); }                if(Bos! =NULL) {bos.close (); }            } Catch(IOException e) {} }}

2. BufferedReader read file stream and BufferedWriter write file stream:

protected Staticstring ReadFile (String inputpath) {BufferedReader br=NULL; Try{br=NewBufferedReader (NewInputStreamReader (NewFileInputStream (InputPath)); StringBuffer result=NewStringBuffer (); String lineString=NULL;  while((lineString = Br.readline ())! =NULL) {result.append (lineString); Result.append ("\ n"); }            returnresult.tostring (); } Catch(IOException e) {e.printstacktrace (); return NULL; } finally {            Try {                if(BR! =NULL) {br.close (); }            } Catch(IOException e) {} }}
protected Static voidWriteFile (String inputstring, String outputPath) {BufferedWriter bw=NULL; Try{BW=NewBufferedWriter (NewFileWriter (OutputPath));        Bw.write (inputstring); } Catch(IOException e) {e.printstacktrace (); } finally {            Try {                if(BW! =NULL) {bw.close (); }            } Catch(IOException e) {} }}

Java input and Output stream summary

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.