Stream output stream

Source: Internet
Author: User

The basic output stream class of Java is: Java. Io. outputstream:

public abstract class OutputStream

This class provides the basic methods required to write data. These methods include:

public abstract void write(int b) throws IOExceptionpublic void write(byte[] data) throws IOExceptionpublic void write(byte[] data,int offset,int length) throws IOExceptionpublic void flush() throws IOExceptionpublic void close() throws IOException

The subclass of outputstream uses these methods to write data to a specific media. For example, fileoutputstream writes data to a file using these methods. Telnetoutputstream uses these methods to write data to a network connection. Bytearrayoutputstream uses these methods to write data to an extended byte array. However, no matter which media is written, most of them use the same five methods.

The basic method of outputstream is write (int B ). This method takes an integer between 0 and 255 as the parameter and writes the corresponding bytes to the output stream. This method is declared as an abstract method, because each subclass needs to modify this method to process specific media. For example, bytearrayoutputstream can use pure Java code to copy bytes to the array. Different from this, fileoutputstream uses native code to learn how to write data to files on the host platform.

Note that although this method accepts an int as a parameter, it will actually write an unsigned byte. Java does not have an unsigned byte data type, so int should be used here. The only real difference between unsigned bytes and signed Bytes: They all consist of eight binary digits. When you write an int into a network connection using write (int B, only eight binary digits are placed on the cable. If a request exceeds 0 ~ 255 int passed into write (int B), will write the lowest number of bytes, the other three bytes will be ignored (this is the result of forcibly converting int to byte ). Tip: however, in rare cases, you may see some problematic third-party classes, which are written in excess of 0 ~ When the value is 255, their practices are different. For example, illegalargumentexception is thrown or 255 is always written, so try to avoid writing more than 0 ~ 255 Int.

To be continued ....................................

Stream 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.