Dataoutputstream and datainputstream

Source: Internet
Author: User
Dataoutputstream dos = new dataoutputstream (New bufferedoutputstream (New fileoutputstream ("a.txt"); dos. writeint (5); dos. writeboolean (true); dos. flush (); dos. close (); datainputstream Dis = new datainputstream (New bufferedinputstream (New fileinputstream ("a.txt"); system. out. println (DIS. readint (); system. out. println (DIS. readboolean (); DIS. close (); // learn how to close the stream bufferedreader BR = NULL; inputstream Reader in = NULL; try {In = new inputstreamreader (filestreamtest. class. getresourceasstream ("a.txt"), "UTF-8"); BR = new bufferedreader (in); // read by row BR. readline ()} catch (exception e) {e. printstacktrace ();} finally {If (BR! = NULL) {try {BR. close (); // Why in. close () is not written in to prevent BR. close () throws an exception, in. close () cannot be closed.} Catch (exception e) {e. printstacktrace () ;}} if (in! = NULL) {try {in. Close () ;}catch (exception e) {e. printstacktrace ();}}}}


All Java stream classes are located in the Java. Io package and inherit the following four abstract stream types.

  Byte stream Ghost stream
Input stream Inputstream Reader
Output stream Outputstream Writer

1. streams inherited from inputstream and outputstream are used to input/output data to the program, and the data units are all bytes (byte = 8 bit). The dark streams are node streams, the light color is the processing stream.

 

2. streams inherited from Reader/writer are used to input/output data to the program, and the data units are all characters (2 byte = 16 bit). The dark streams are node streams, the light color is the processing stream.

Common node stream types include:

Filereader/filewriter are used for file operations, and fileinputstream/fileoutputstream are used for byte streams.

Common stream processing types include:

Buffer stream: the buffer stream needs to be connected to the corresponding node stream, providing a buffer function for read and write data, improving read and write efficiency, and adding some new methods for colleagues.

The Byte buffer streams include bufferedinputstream/bufferedoutputstream, and the character buffer streams include bufferedreader/bufferedwriter,The character buffer stream provides the Readline and newline methods for reading and writing a row respectively.

For the buffer stream in the output location, the written data is first written into the memory, and then flush the data in the memory to the hard disk. Therefore, when using character buffer streams, you must first flush and then close to avoid data loss.

Conversion stream: used to convert byte data to character data.

Only the inputstreamreader and outputstreamwriter streams are available. Inputstreamreader and inputstream must be connected. outputstreamwriter and outputstream must be connected ".

Data Stream: provides the ability to read and write basic data types in Java.

Datainputstream and dataoutputstream inherit from inputstream and outputstream respectively, and must be connected to inputstream and outputstream.

Object stream: used to write objects directly.

Stream classes include objectinputstream and objectoutputstream. It is required that the written object must implement the serializable interface to declare that it can be serialized. Otherwise, the object stream cannot be used for read/write.

Another keyword is important. Transient modifies the class attributes that implement the serializable interface. This field is ignored when output in the form of an object 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.