Java Io:outputstream

Source: Internet
Author: User

Original link Author: Jakob Jenkov Translator: Li Jing ([email protected])

The OutputStream class is the base class for all output streams in the Java IO API. Subclasses include Bufferedoutputstream,fileoutputstream and so on. Refer to the table at the bottom of this section of Java IO Overview to browse the complete list of subclasses.

output stream and target medium

Output streams are often associated with target vectors for certain data, such as files, network connections, pipelines, and so on. For more details, refer to Java IO Overview. When the data written to the output stream is gradually output, the target medium is the place where all the data is to be attributed.

Write (Byte)

The Write (byte) method is used to write a single byte to the output stream. The Write (byte) method of OutputStream writes an int variable containing the data to be written as a parameter. Only the first byte of the int type is written, and the remaining bits are ignored. (Translator Note: Write low 8-bit, ignoring high 24-bit).

Subclasses of OutputStream may contain alternative methods for the Write () method. For example, DataOutputStream allows you to use Writeboolean (), writedouble () and other methods to write variables such as the underlying type Int,long,float,double,boolean.

This is an example of a outputstream write () method:

01 OutputStream output = newFileOutputStream("c:\\data\\output-text.txt");
02
03 while(hasMoreData()) {
04
05     intdata = getMoreData();
06
07     output.write(data);
08
09 }
10
11 output.close();

This example first creates the FileOutputStream to be written. After entering the while loop, the loop is judged by the return value of the Hasmoredata () method. The implementation of the Hasmoredata () method is not shown, please interpret this function as: Returns True if there is any remaining writable data, otherwise false.

Note that in order to be clear, the necessary exception handling is ignored. For more information about exception handling, refer to Java IO exception handling.

write (byte[])

The OutputStream also contains methods for writing all or part of the data in the byte data to the output stream, respectively, write (byte[]) and write (byte[], int offset, int length).

Write (byte[]) writes all the data in the byte array to the output stream.

Write (byte[], int offset, int length) starts from the offset position in the byte data, and the length byte of the data is written to the output stream.

Flush ()

The flush () method of the OutputStream flushes all data written to outputstream into the corresponding target medium. For example, if the output stream is FileOutputStream, then the data written to it may not actually be written to disk. Even if all the data is written to FileOutputStream, it is possible that the data will remain in the buffer of memory. By calling the flush () method, the data in the buffer can be flushed to disk (or network, and any other form of target medium).

Close ()

When you are finished writing data, you need to close outputstream. This can be achieved by calling close (). Because the various write () methods of OutputStream may throw IO exceptions, you need to execute the close operator of the call Close () in the finally block. This is an example of a outputstream call to close ():

01 OutputStream output = null;
02
03 try{
04
05     output = newFileOutputStream("c:\\data\\output-text.txt");
06
07     while(hasMoreData()) {
08
09         intdata = getMoreData();
10
11         output.write(data);
12
13     }
14
15 } finally{
16
17     if(output != null) {
18
19         output.close();
20
21     }
22
23 }

This example calls the close () method in the finally block. While this approach ensures that the outputstream is closed, it is not a perfect exception-handling scheme. I explored the exception handling of IO in more detail in the Java IO exception handling article.

original articles, reproduced please specify: reproduced from the Concurrent programming network –ifeve.com This article link address: Java io:outputstream

Java Io:outputstream

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.