Input and output streams for Java

Source: Internet
Author: User
Tags array length try catch

Overview:

  the IO stream in Java divides the input stream and the output stream, and each of them is divided into the byte stream and the character stream; the difference is mainly in the unit size of the read/write; Because Java uses the GBK encoding format, a character takes up 2 bytes, that is, the stream of characters is 16 bits, and the byte stream is 8 bits. There is also a division: node flow and processing flow, node flow: Direct and data source connection to transport data processing flow: is not directly, but to the node stream encapsulation and other uses (is a good way)!!!

the input stream has 2 abstract base classes: The InputStream and reader output streams have 2 abstract base classes: OutputStream and writer; among them, InputStream and OutputStream are 8 bits that belong to the byte stream Writer&reader is a character stream (2 bytes 16 bits)

Read (byte[] b,int off,int len) method for detecting InputStream streams
1 ImportJava.io.*;2 3 Public classhellojava{4 Public Static voidMain (string[] args)throwsIOException {5 Try{6FileInputStream fin1=NewFileInputStream ("/home/kylin/desktop/operation PG database. txt");7 byte[]buf1=New byte[1024];8 intHasread1=0;9 while((Hasread1=fin1.read (buf1,5,1019)) >0){TenSystem.out.println ("HasRead1:" +HasRead1); OneSystem.out.println (NewString (buf1,5, HasRead1)); A } - } - Catch(IOException ex) { the ex.printstacktrace (); - } - } -}
//Use a try catch to automatically call Close (), and call Close () to automatically call flush (), so you can automatically close the IO stream using the Try Catch method//Read (Buf,off,len) len length Max here must be less than Buf.length-off//Note: As Java takes GBK encoding, the length of the read setting (array length setting) should be as large as possible, or it may be garbled because of the length is not enough

The read (char []b,int off,int len) method in the abstract base class of reader is detected, and the effect of the above method is almost identical
1 ImportJava.io.*;2 3 Public classhellojava{4 Public Static voidMain (string[] args)throwsIOException {5 Try{6FileReader fin=NewFileReader ("/home/qilin/desktop/operation PG database. txt");7 intHasread=0;8 Char[]buf=New Char[1024];9 while((Hasread=fin.read (buf,5,100)) >0){TenSystem.out.println ("Hasread:" +hasread); OneSystem.out.println (NewString (BUF, 5, Hasread)); A } - } - Catch(IOException ex) { the ex.printstacktrace (); - } - } -}
1 ImportJava.io.*;2 3 //try to read the data in file a and write it to another file B4  Public classhellojava{5      Public Static voidMain (string[] args) {6        Try{7FileInputStream fin=NewFileInputStream ("/home/qilin/desktop/operation PG database. txt");8FileOutputStream fout=NewFileOutputStream ("/home/qilin/desktop/hello1.txt");9             intHasread=0;Ten             byte[]buf=New byte[1024]; One             //writes to the BUF array A              while((Hasread=fin.read (BUF)) >0){ -                 //output from the BUF array to the Hello1.txt file -Fout.write (buf, 0, hasread); the             } -         } -         Catch(IOException ex) { - ex.printstacktrace (); +         } -          +     } A}

ImportJava.io.*;//When I do not empty the buffer, and close the stream, even if it can be run, but does not write to the file!
Using a try catch is also not written to the file, only if it explicitly empties the buffer and closes the stream before it is written, does the call to try catch do not close the stream when it is written?? //The test uses writer's write to write the string to the file Public classhellojava{ Public Static voidMain (string[] args)throwsIOException {FileWriter fw=NewFileWriter ("/home/qilin/desktop/hello3.txt"); Fw.write ("Hello, everybody."); //Fw.write ("Happy New Year \ \", 0,6); Throws an exception because the actual is not so longFw.write ("Happy New Year", 2, 2); Fw.flush (); //Buffer writes to physical nodeFw.close ();//Close }}
It seems to be the manual to close the bar ~

Input and output streams for Java

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.