Java IO input and output flow detailed

Source: Internet
Author: User

Java streams are divided into character streams and byte streams on processing.

A character stream is a 2-byte Unicode character that operates on a character, an array of characters, or a string, and a byte-throttle processing unit of 1 bytes (byte), manipulating bytes and byte arrays.

In Java, the character is stored in Unicode encoding, and the character stream processing class is responsible for converting other encoded character streams from outside to the stream of Unicode characters in Java. The class InputStreamReader and OutputStreamWriter handle the conversion of character streams and byte streams (the byte stream is encapsulated into character streams, such as New InputStreamReader (system.in)). Character streams (one buffer at a time) is more efficient than a byte stream (one bytes at a time).

(i) byte-oriented stream------Inputstream/outputstream

InputStream

The Java.io.InputStream class is the base class for other byte input streams and is an abstract class. , one byte reads the data into the unit. System.in also belongs to this kind of expansion

The method provided by it has


Method Summary
Int available ()
Returns the number of estimated bytes of a method call that can be read (or skipped) from this input stream without blocking.
void Close ()
Closes this input stream and releases all system resources associated with the stream.
void Mark (int readlimit)
Mark the current position in this input stream.
Boolean marksupported ()
Test whether this input stream supports the mark and reset methods.
abstract int Read ()
Reads the next byte of data from the input stream.
Int Read (byte[] b)
Reads a certain number of bytes from the input stream and stores them in buffer array B.
Int Read (byte[] B, int off, int len)
Reads up to Len data bytes in the input stream into a byte array.
void Reset ()
Reposition this stream to the last time the Mark method was called on this input stream.
Long Skip (Long N)
Skips and discards the N bytes of data in this input stream.

1.1

Bytearrayinputstream--Use a buffer in memory as a inputstream.

Construct---

A Bytearrayinputstream (byte[]) creates a new byte array input stream (Bytearrayinputstream) that reads data from the specified byte array (using byte as its buffer array)

(B)---bytearrayinputstream (byte[], int, int) creates a new byte array input stream that reads data from the specified byte array.

---mark:: The byte array is not replicated.

1.2

StringBufferInputStream--Takes a String object as a inputstream.

Construct---

StringBufferInputStream (string) creates an input stream string that reads data from the specified string.

Note: Using the StringBufferInputStream method is not recommended. This class cannot convert a character to a byte correctly.

Similar to JDK version 1.1, the best way to create a stream from a string is to use the StringReader class.

1.3

FileInputStream--A file as a inputstream, to achieve read operations on the file

Construct---

A FileInputStream (file name) creates an input file stream that reads data from the specified file object.

B FileInputStream (filedescriptor) creates an input file stream that reads data from the specified file descriptor.

(C)-fileinputstream (String name) creates an input file stream that reads data from a file of the specified name.

Method----Read () reads one byte of data from the current input stream.

Read (byte[]) reads the b.length byte data in the current input stream into a byte array.

Read (byte[], int, int) reads the Len byte data in the input stream into a byte array.

1.4

PipedInputStream: Implements the concept of pipe, which is used in the main thread. Pipeline inflow refers to the receiving end of a communication pipeline.

One thread sends data through the pipe output stream, and another thread reads the data through the pipeline, which enables communication between two threads.

Construct---

PipedInputStream () creates a pipe input stream that is not yet connected to a pipe output stream.

PipedInputStream (PipedOutputStream) creates a pipe input stream that is connected to a pipe output stream.

1.5

Sequenceinputstream: Combine multiple inputstream into one inputstream. The sequence input stream class allows an application to combine several input streams continuously,

And make them appear like a single input stream. Each input stream is read sequentially until the end of the stream is reached.

Then the sequence input stream class closes the stream and automatically switches to the next input stream.

Construct---

Sequenceinputstream (enumeration) creates a new sequence input stream and initializes it with an enumeration value of the specified input stream.

Sequenceinputstream (InputStream, InputStream) creates a new sequence input stream initialized to read the input stream first S1 and then reads the input stream s2.

OutputStream

The relative outputstream is the parent of all byte output streams, which is also an abstract class that outputs data in bytes, and provides methods such as:

 void Close ()  
            closes this output stream and releases all system resources associated with this stream.
 void flush ()  
            refreshes this output stream and forces a write out of all buffered output bytes.
 void write (byte[] b)  
     ;       writes the  b.length  bytes from the specified byte array to this output stream.
 void Write (byte[] b, Int off, Int len)   The
           will specify the  len from the offset  off  in the byte array   bytes are written to this output stream.
abstract  void write (int b)  
    ;        writes the specified byte to this output stream.

2.1

Bytearrayoutputstream: Deposit information in a buffer in memory. This class implements an output stream that writes data as a byte array.

When data is written to a buffer, it expands automatically. Data can be retrieved using Tobytearray () and toString ().

Constructor

(a)---bytearrayoutputstream () creates a new byte array output stream.

(B)---bytearrayoutputstream () creates a new byte array output stream.

(C)---bytearrayoutputstream (int) Creates a new byte array output stream with a buffer capacity of the specified size byte.

ToString (String) Converts the contents of the buffer to a string based on the specified character encoding and converts the byte to a character.

Write (byte[], int, int) writes Len bytes from the specified byte array starting at offset off to the byte array output stream.

Write (int) writes the specified byte to the byte array output stream.

WriteTo (OutputStream) invokes the write method of the output stream with Out.write (buf, 0, Count) to write the entire contents of the byte array output stream to the specified output stream parameter.

2.2

FileOutputStream: The file output stream is an output stream that outputs data to file or FileDescriptor.

Constructor

A FileOutputStream (file name) creates a file output stream that outputs data to the specified file object.

B FileOutputStream (filedescriptor) creates a file output stream that outputs data to the specified file descriptor.

C FileOutputStream (String name) creates a file output stream that outputs data to a file of the specified name.

D FileOutputStream (String, Boolean) creates an output file with the file name of the specified system.

2.3

PipedOutputStream: The pipeline output stream is the sender of a communication pipeline. A thread sends data through the pipe output stream,

Another thread reads data through the pipeline, which enables communication between two threads.

Constructor

A PipedOutputStream () creates a pipeline output stream that has not yet been connected to a pipeline inflow.

B PipedOutputStream (PipedInputStream) creates a pipe output stream that is connected to a pipe input stream.

3. Conversion between two different-oriented stream types

3.1

InputStreamReader and Outputstreamreader:

Converts a byte-oriented stream into a character-oriented stream.

The InputStreamReader class is a bridge from byte stream to character streams: it reads bytes and converts them to character streams according to the specified encoding.

The encoding used may be specified by name, or by the default encoding that the platform accepts.

Each invocation of one of the InputStreamReader read () methods may cause one or more bytes to be read from the base byte input stream.

To achieve greater efficiency, consider using the BufferedReader package InputStreamReader,

BufferedReader in = new BufferedReader (new InputStreamReader (system.in));//(This is the Java traditional way to read keyboard input)

For example://Implementation input an integer from the keyboard[Java]  View Plain Copy string s = null;               Inputstreamreader re = new inputstreamreader (system.in);                 bufferedreader br  = new bufferedreader (re);                  try {                  s = br.readline ();                  system.out.println ("s= "  + integer.parseint (s)) ;                 br.close ();                  }                 catch  (IOException e)                  {                  e.printstacktrace ();                  }                  catch  (numberformatexception e)//   When an application attempts to convert a string to a numeric type, but the string cannot be converted to the appropriate format, the exception is thrown.                  {                 system.out.println ("  Input is not a number   ");                  }  

InputStreamReader (InputStream) creates a inputstreamreader using the default character encoding.

InputStreamReader (InputStream, String) creates a inputstreamreader with a named character encoding.

OutputStreamWriter writes multiple characters to an output stream, converting multiple characters to bytes based on the specified character encoding.

Each outputstreamwriter merges its own chartobyteconverter, thus is the bridge from the character stream to the word stream.

(iii) General principles of Use of Java IO:

Classification by data Source (whereabouts):

1, is the file: FileInputStream, FileOutputStream, (Byte stream) FileReader, FileWriter (character)

2, is byte[]: Bytearrayinputstream, Bytearrayoutputstream (Word throttling)

3, is char[]: Chararrayrea

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.