<JAVA> byte stream, character stream

Source: Internet
Author: User

All the data in the program is transmitted or saved in a stream, and the program needs data to read the data using the input stream, and the output stream is used when the program needs to save some data.

This can be represented by the following input-output stream graph.

BYTE stream:

The byte stream mainly operates on the type data, in byte array, the main operation class is the OutputStream class and the InputStream class.

Where the OutputStream class is an abstract class, if you want to use this class, you first have to instantiate the object through subclasses. Assuming that you are working on a file now, you can use the FileOutputStream class, which can be instantiated by an upward transformation, and the main operations in the OutputStream class are as follows:

1 Public voidClose ()throwsIOException//turn off the output stream2 Public voidFlush ()throwsIOException//Flush Buffers3 Public voidWritebyte[] b)throwsIOException//writes a byte array to the data stream4 Public voidWritebyte[] B,intOffintLenthrowsIOException//writes a specified range of byte arrays to the data stream5 Public Abstract voidWriteintbthrowsIOException//writes a byte of data to the data stream

You must receive the file class instance at the time of operation to indicate the path of files to output.

InputStream is an input stream, which is the stream used to read the file, the abstract method read reads the next byte, and returns 1 when it reads to the end of the file. If there is no data in the stream, read will block until the data arrives or the exception occurs or the stream shuts down. This is a checked exception, and the specific caller must handle the exception. In addition to reading one byte at a time, InputStream also provides read (byte[]), which reads multiple bytes. Read (byte[]) is actually called by default read (byte b[], int off, int len) method, which means that each read a byte is placed in b[off++], a total of read Len bytes, but tend to appear in the stream the number of bytes less than Len, So the number of bytes actually read is returned.

 Public classTest_inputoroutput { Public Static voidMain (string[] args)throwsioexception{fileinputstream Fin=NewFileInputStream ("Hello.txt"); byte[] buffer =New byte[1024]; intx = Fin.read (buffer,0, buffer.length); String Str=NewString (buffer);            System.out.println (str);            SYSTEM.OUT.PRINTLN (x);    Fin.close (); }}

The above is the Read method.

<JAVA> byte stream, character 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.