Java IO (iii)-processing flow

Source: Internet
Author: User

Process Flow

Processing type Character Stream BYTE stream
Buffering

Bufferreader

Bufferwriter

Bufferedinputstream

Bufferedoutputstream

converting between

Bytes and character

InputStreamReader

OutputStreamWriter

Object serialization ---

ObjectInputStream

ObjectOutputStream

Data Conversion ---

DataInputStream

DataOutputStream

Counting LineNumberReader Linenumberinputstream
Peeking ahead Pushbackreader

Pushbackinputstream

Printing PrintWriter PrintStream

1, buffered stream

Buffer flow to "socket" on the corresponding node stream, the data read and write to provide a buffer function, improve the efficiency of reading and writing, while adding a number of new methods.

J2SDK provides four buffer streams, which are commonly used to construct methods:

Bufferedreaderbufferedreader (Reader in) creates a buffering character-input Stream that uses adefault-sized input buffer. BufferedReader (Reader in,intSZ) Creates a buffering character-input stream that uses an input buffer of the specified size. Bufferedwriterbufferedwriter (Writer out) creates a buffered character-output Stream that uses adefault-sized output buffer. BufferedWriter (Writer out,intSZ) Creates aNewBuffered character-output stream that uses an output buffer of the given size. Bufferedinputstreambufferedinputstream (InputStream in) creates a bufferedinputstream and saves its argument, the input Stream in, forlater use. Bufferedinputstream (InputStream in,intsize) Creates a bufferedinputstream with the specified buffer size, and saves it argument, the input stream in, forlater use. Bufferedoutputstreambufferedoutputstream (OutputStream out) creates aNewbuffered output stream to write data to the specified underlying output stream. Bufferedoutputstream (OutputStream out,intsize) Creates aNewBuffered output stream to write data to the specified underlying output stream with the specified buffer size.
The buffered input stream supports the mark and reset methods of its parent class BufferedReader provides the ReadLine method for reading a line of strings (separated by \ R or \ n) BufferedWriter provides the newline method for writing a row delimiter for the buffered stream of output, the data written out is cached in memory first, and the Flush method will make the in-memory data immediately available.

Example One

     Public Static voidMain (string[] args) {Try{FileInputStream fis=NewFileInputStream ("D:\\share\\java\\helloworld.java"); Bufferedinputstream bis=NewBufferedinputstream (FIS); intc = 0;            System.out.println (Bis.read ());            System.out.println (Bis.read ()); Bis.mark (100);  for(inti = 0; I <= && (C=bis.read ())! =-1; i++) {System.out.print (Char) C + "");            } System.out.println ();            Bis.reset ();  for(inti = 0; I <= && (C=bis.read ())! =-1; i++) {System.out.print (Char) C + "");        } bis.close (); } Catch(IOException e) {e.printstacktrace (); }    }

Results:

47
a l; s K J D F;

Example Two,

     Public Static voidMain (string[] args) {Try{bufferedwriter bw=NewBufferedWriter (NewFileWriter ("D:\\dat2.txt")); BufferedReader BR=NewBufferedReader (NewFileReader ("D:\\dat2.txt")); String s=NULL;  for(inti = 1; i<=100; i++) {s=string.valueof (Math.random ());                Bw.write (s);            Bw.newline ();            } bw.flush ();  while((s = br.readline ())! =NULL{//Read line System.out.println (s);            } bw.close ();        Br.close (); } Catch(IOException e) {e.printstacktrace (); }    }

Results:

Dat2.txt file:

Console output:

Java IO (iii)-processing flow

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.