Java input and output stream

Source: Internet
Author: User
Tags object serialization throw exception

Overview:

Various stream types (classes and abstract classes) are located in the Java.io package, each of which inherits one of four abstract streams:

Type BYTE stream Character Stream
Input stream InputStream Reader
Output stream OutputStream Writer

Generic domains and methods should be placed in the superclass, and for methods that cannot be specifically defined as abstract methods that are not implemented, serve as placeholder roles, implemented in subclasses. Classes that contain one or more abstract methods should also be declared as abstract classes, and abstract classes cannot be instantiated, but abstract class object variables can refer to objects of non-abstract subclasses to implement polymorphism.

Characters in Java (char type) in Unicode character set, standard Unicode encoding scheme is utf-16, each character occupies two bytes

For the English 16 byte high point is 0, with the byte stream reading is not a problem, but for Chinese should read the entire character output makes sense

Stream end is a byte stream, for the program, from the external data equivalent to the data input into the program, it is called the stream of reading data is the input stream

Another node stream/process stream (connected to an existing stream, through the processing of the data to provide more powerful read and write function to the program)

A stream that inherits from Inpustream such as Fileinpustream,datainpustream,objectinpustream ...

Basic methods:

int read () throws IOException//reads a byte and returns as an integer (0~255) If 1 is returned to the end of the input stream

int read (byte[] buffer) throws IOException//reads a series of bytes and stores it to an array of buffer, returning the actual number of bytes read, if return 1 means the end of the input stream

int read (byte[] buffer, int offset, int length) throws IOException//reads the byte stream from the offset position and stores the length byte to an array buffer, returning the actual number of bytes read, If 1 is returned to the end of the input stream

void Close () throws IOException//close stream frees memory resources

The corresponding OutputStream basic method (for convenience, each method omitted throws IOException):

void Write ()

void Write (byte[] buffer)

void Write (byte[] buffer, int offset, int length)

void Close ()//The Flush method should be called before close in case the buffer is not full, data that is not automatically written by the program cannot be written to the file after the stream is closed

void flush ()//Writes all the data in the output stream to the destination

Reader and InputStream are basically the same, with only the characters (16bit) as the reading units. such as BufferedReader, InputStreamReader, FileReader (inherited from InputStreamReader)

The basic method is the same, except that the parameter type byte should be changed to Char

There are many ways to write a string in the basic writer method:

void Write (String string) throws IOException//is actually called by the String class itself ToCharArray method

void Write (string string, int offset, int length) throws IOException

node Flow:

character stream
file filereader
Filew Riter
memory Array chararrayreader
Chararraywriter
memory string< /td> stringreader
StringWriter

__

pipe pipereader
PIPEWR ITER

Main note Four file streams

Public fileoutputstream (File file, boolean append) throws FileNotFoundException The second argument is true when appending instead of overwriting

The output stream re-write operation automatically creates a file for you without a file, but does not create a folder, so the folder hierarchy in the file path name should exist

Processing Flow:

Processing type Character Stream BYTE stream
Buffering BufferedReader
BufferedWriter
Bufferedinputstream
Bufferedoutputstream
Filtering Fileterreader
Filterwriter
FilterInputStream
Filteroutstream
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

Buffer stream: Improve read and write efficiency

BufferedReader (Reader in) buffered input stream its parent class provides the mark (), Reset () method, and the ReadLine () method for reading a line

BufferedWriter (Writer out) buffered output stream uses the Flush method to write out the number of buffers immediately, providing newline () writes a line delimiter

Bufferedinputstream (InputStream in) buffered input stream the parent class provides the mark (), Reset () method

Bufferedoutputstream (OutputStream out) buffered output stream uses the Flush method to write buffer data immediately

Conversion stream: The byte stream is converted to a character stream, which can read and write bytes/byte arrays, and then read and write strings, Chinese ...

InputStreamReader (InputStream in)//conversion stream can specify its encoding set at construction time

Outputstreamwrite (OutputStream out)//conversion stream can specify its encoding set at construction time

Note: There are two fields in the Java.lang.System class, the standard input and output stream, which is turned on by default and accepts keyboard input and monitor output at any time.

public static final InputStream in

public static final PrintStream out

Data flow: Provides a machine-independent way to read Java raw data types (WRITEINT,WRITEDOUBLE,WRITEBOOLEAN,WRITEUTF, etc.)

DataInputStream (InputStream in)//write first read it first

DataOutputStream (OutputStream out)

Queue: FIFO, different type corresponding to different byte length

Print Flow: Add print functionality to other streams, providing various print methods to easily print various forms of data. Advantages of no-throw exception and automatic flush

PrintWriter ()//have multiple constructors

PrintStream ()

Object flow: Serializes, writes an object directly or reads it. A class that implements the Serializable interface (no method defined, a labeled interface that tells the compiler that the class can be serialized) can be serialized as a series of bytes saved

ObjectInputStream ()
ObjectOutputStream ()

Java input and output 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.