"Java Foundation" IO stream

Source: Internet
Author: User

Package Com.pachira.c;import Java.io.bufferedreader;import Java.io.file;import java.io.fileinputstream;import                                               Java.io.ioexception;import Java.io.inputstreamreader;import java.util.scanner;/* * +----> [c] bufferedreader------> [c] LineNumberReader * | | ---> [c] inputstreamreader------> [c] filereader * |----> [c] Str                        Ingreader * +---> [c] Reader-------+----> [c] Pipedreader * |                        |----> [C] CharArrayReader * |              +----> [c] filterreader------> [C] pushbackreader * +---> Character stream--+ * |                        |              +----> [C] bufferedwriter * |                        |              |----> [c] outputstreamwriter------> [c] FileWriter * |              |          |----> [C] printerwriter * |                                       +---> [c] Writer-------+----> [c] StringWriter * |                                       |----> [C] pipedwriter * |                                       |----> [C] chararraywriter * |                                       +----> [C] filterwriter * IO--+ * |                                       +----> [c] fileinputstream +----> [c] Bufferedinputstream * |                                       |----> [c] filterinputstream--|----> [c] DataInputStream * |              |----> [c] objectinputstream +----> [c] Pushbackinputstream * |              +---> [c] inputstream--+----> [c] PipedInputStream * |                        |              |----> [C] Sequenceinputstream * |                        |              |----> [C] StringBufferInputStream * |                        | +----[C] Bytearrayinputstream * +---> Byte stream--+ * |                        +----> [c] fileoutputstream +----> [c] Bufferedoutputstream * | |----> [c] filteroutputstream--|----> [c] dataoutputstream * +---> [c] outputstream-+----& Gt [C] ObjectOutputStream +----> [c] printstream * |----> [c] Pipedoutpu TStream * +----> [C] bytearrayoutputstream * * *io Stream Classification * *). Data type: Character stream and byte flow). Data flow: input stream and Output stream * * character stream and Byte stream * *). The origin of the character stream: Because the data is encoded differently, there is a stream object that efficiently operates on the character. The essence is actually based on the byte stream reading, to check the specified code table. *). The difference between a byte stream and a character stream: * Read and write units are different: bytes stream in bits (8bit), character stream in characters, according to the Code table mapping characters, can read multiple bytes at a time. * Processing objects are different: the byte stream can handle all types of data (slices, audio, etc.), whereas a character stream can only handle data of the type of characters. * Conclusion: As long as the processing of plain text data, the use of character streams is preferred, in addition to the use of byte stream.  * * Input stream and output stream * Only read the input stream, only write to the output stream, the program needs to use different streams according to the different characteristics of the data to be transmitted. * *inputstream). InputStream is the parent class of all input byte streams, which is an abstract class; Bytearrayinputstream, StrinGbufferinputstream, FileInputStream are three basic media streams that read data from byte arrays, StringBuffer, and local files, respectively. PipedInputStream is the data read from a pipeline shared with other threads; ObjectInputStream and all FilterInputStream subclasses are decorative streams (the protagonist of the adorner pattern); * *outputstream). OutputStream is the parent class of all output byte streams, which is an abstract class; Bytearrayoutputstream and FileOutputStream are two basic media streams, which write data to byte array and local file respectively; ). PipedOutputStream is writing data to a pipeline that is shared with other threads; ObjectOutputStream and all filteroutputstream subclasses are decorative streams; * *reader). Reader is the parent class of all the input character streams, which is an abstract class; Charreader, StringReader, are two basic media streams that read data from a char array, a string, and a different one. Pipedreader is the data read from a pipeline shared with other threads; BufferedReader is obviously an adorner, and its subclasses are responsible for decorating other reader objects; FilterReader is the parent of all custom concrete adornment streams whose subclasses pushbackreader the reader object, adding a line number; *6). InputStreamReader is a bridge between the byte stream and the character stream, which transforms the stream of bytes into character streams. FileReader can be said to achieve this function, commonly used tool class, * in its source code is clearly used to convert FileInputStream to reader method; * *writer). Writer is the parent of all the streams of output characters, which is an abstract class; Chararraywriter, StringWriter are two basic media streams, which write data to a char array, a string, respectively. PipedWriter is writing data to a pipeline that is shared with other threads; BufferedWriter is an adorner that provides buffering function for writer; PrintWriter and PrintStream PolesThey are similar in function and use very similar; *6). OutputStreamWriter is outputstream to writer Transformation Bridge, its subclass filewriter is actually a concrete class to implement this function; * */public class IO {public static void Main (string[] args) throws IOException {//character stream string path = "Data/data"; BufferedReader br = new BufferedReader (new InputStreamReader (New FileInputStream (path), "Utf-8")); String line = "", while (line = Br.readline ())! = null) {System.out.println (line);} Br.close ();/* Byte stream * UTF-8: is a variable-length encoding that can represent a symbol using 1~4 bytes; * gbk:a character is encoded as 1 or 2 bytes */path = "Data/data2"; FileInputStream fis = new FileInputStream (path); byte[] bs = new Byte[2];while (fis.available () > 0) {fis.read (BS); string tmp = new string (BS, "GBK"); for (byte b:bs) {System.out.print (b + "");} SYSTEM.OUT.PRINTLN (tmp + ""); bs = new byte[2];} Fis.close ();/* * Common Tools * This ((Readablebytechannel) (new FileInputStream (source). Getchannel ()), charsetname); */scanner sc = new Scanner (new File (path), "GBK"), while (Sc.hasnext ()) {String tline = Sc.nextline (); System.out.println (Tline);}}}

  

"Java Foundation" IO stream

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.