Java file read/write

Source: Internet
Author: User

Io flow simply means that the input stream and output stream are primarily used to process data transfer between devices

Case 1 is a byte stream, Case 2 is a character stream, the difference between the byte stream and the character stream is very large, and the reader needs to use it according to his own scene.

Case 1:

Use byte stream and package stream (readers can understand the packaging flow, mainly using the adorner mode) to read and write files.

 Public classFileCopy { Public Static voidCopyFile (File src,file Res)throwsexception{InputStream in=NewBufferedinputstream (NewFileInputStream (SRC)); OutputStream out=NewBufferedoutputstream (NewFileOutputStream (res)); byte[] bt=New byte[1024];  while( -1!=(In.read (BT))) {Out.write (BT,0, bt.length);        } out.flush ();        Out.close ();    In.close (); }         Public Static voidMain (string[] args) {File src=NewFile ("D:/tmp/test.xls"); File Res=NewFile ("D:/tmp/b.xls"); Try{copyfile (src, res); } Catch(Exception e) {e.printstacktrace (); }    }}

Case 2:

File read and write using wrapper stream and character stream

 Public classFileCopy { Public Static voidMain (string[] args)throwsException {bufferedreader br=NewBufferedReader (NewInputStreamReader (NewBufferedinputstream (NewFileInputStream (NewFile ("D:/tmp/a.txt")), "GBK")); BufferedWriter out=NewBufferedWriter (NewOutputStreamWriter (NewBufferedoutputstream (NewFileOutputStream (NewFile ("D:/tmp/b.txt")), "GBK")); String Line=NULL;  while(NULL! = (line=Br.readline ()))            {Out.write (line);        Out.newline ();        } out.flush ();        Out.close ();    Br.close (); }}

Java file read/write

Related Article

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.