Java IO byte stream and character stream-outputsteam and InputStream

Source: Internet
Author: User

The IO stream in Java is divided into byte stream and character stream, and each stream also divides the input stream and the output stream.

Let's start with the input and output streams: the input and output are for program memory, so the input is to write data to the memory, and the output is to write out the data from the program memory.

Byte stream uses bytes to manipulate data directly.

A character stream is a byte written to the cache, and then the data is manipulated through the buffer.

The parent class of the byte stream is InputStream (input stream) and OutputStream (output stream).

The direct subclasses of Intputstream are: Audioinputstream,bytearrayinputstream,fileinputstream,filterinputstream,inputstream, ObjectInputStream, PipedInputStream,

Sequenceinputstream, StringBufferInputStream

The direct subclasses of Outputsteam are: Bytearrayoutputstream,fileoutputstream,filteroutputstream,objectoutputstream,outputstream, PipedOutputStream

The parent class of a character stream is reader (input stream) and writer (output stream).

The direct subclasses of reader are: Bufferedreader,chararrayreader,filterreader,inputstreamreader,pipedreader, StringReader

The direct sub-categories of writer are: Bufferedwriter,chararraywriter,filterwriter,outputstreamwriter,pipedwriter, PrintWriter, StringWriter


The following is a detailed description of the byte stream and character stream:

One, the byte stream

1-byte output stream: OutputStream

OutputStream is the largest parent of the byte output stream in IO packets, an abstract class that implements the Closeable,flushable interface, and we must instantiate the object through its subclasses.

There are a few main and one way:

void Close () closes the secondary output stream and frees all related system resources

void Flush () refreshes this output stream and forces all buffered output bytes to be written out

void Write (byte[] b) writes B.length bytes from a specified byte array to the stream

void Write (byte[] b, int off, int len) writes Len bytes from offset off to the output stream in a byte array

void write (int b) writes the specified bytes to this output stream

Write a piece of data to the D-disk test.txt

Package Andy.io.test;import Java.io.file;import Java.io.fileoutputstream;import java.io.ioexception;import java.io.outputstream;/**  * @author zhang,tianyou * @version November 18, 2014 PM 11:44:06 */public class Outputstreamtest { public static void Main (string[] args) throws IOException {//writes the input stream to this file = "D:" + File.separator + "test.t XT "), outputstream outputstream  = null;//using subclass instantiation OutputStream = new FileOutputStream (file, true);//append string str = "Hello Andy!"; byte b[] = Str.getbytes ();//1 method one//outputstream.write (b);//2 method two for   (int i = 0; i < b.length; i++) {outputstream. Write (B[i]);} 3 Methods Three//outputstream.write (b, 0, b.length);//finish writing must close the output stream to release resources outputstream.close ();}}



2 bytes Input stream: InputStream

InputStream is the largest parent of the byte input stream in IO packets, which is an abstract class that implements the Closeable,flushable interface and must instantiate the object through its subclasses.

There are a few main and one way:

void Close () closes the secondary input stream and frees all related system resources

int read (byte[] b) reads a certain number of bytes from the input stream and stores it in an array of buffers b .

void Read (byte[] b, int off, int len) reads the maximum number of data bytes in the input stream len into a byte array.

int read () reads the next byte of data from the input stream.

Package Andy.io.test;import Java.io.file;import Java.io.fileinputstream;import java.io.ioexception;import java.io.inputstream;/**   * @author zhang,tianyou   * version:2014-11-19 morning 11:40:24   *   */public Class Inputstreamtest {/** * @param args * @throws ioexception  */public static void Main (string[] args) throws Ioexcep tion {//Read the file of Test.txt under the D drive = the "D:" +file.separator + "test.txt"), or the FileInputStream instance by InputStream input Stream = Null;inputstream = new FileInputStream (file);//define byte array byte[] b = new byte[(int) file.length ()];//first//int len = i Nputstream.read (b);//second/*for (int i=0; i<b.length; i++) {b[i] = (byte) inputstream.read ();} *///the third type of Inputstream.read (b, 0, b.length);//close the corresponding stream inputstream.close (); SYSTEM.OUT.PRINTLN ("Test content:" + new String (b));}}


The results of the operation are as follows:

The test content is: Hello Andy!




Java IO byte stream and character stream-outputsteam and InputStream

Related Article

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.