Java output stream

Source: Internet
Author: User

The two main types of output streams are divided by how they are written to data: one is written in human habits and the other is written for later by a datainputstream. Randomaccessfile is independent, although its data format is compatible with DataInputStream and DataOutputStream.

5. Saving and restoring data
PrintStream can format data so that it can be read in our habits. But in order to output data for recovery by another data stream, a dataoutputstream is required to write the data and a datainputstream to recover (get) the data. Of course, these data streams can be anything, but here's a file and a buffer to speed up reading and writing.
Note that the string is written with writebytes (), not WriteChars (). If you use the latter, you are writing 16-bit Unicode characters. Because there is no supplemental "ReadChars" method in DataInputStream, you have to use Readchar () to fetch one character at a time. So it is more convenient for ASCII to write a character as a byte, followed by a new line, and then ReadLine () to read the character as a normal ASCII line.
Writedouble () saves the double number to the data stream and restores it with the supplemental readdouble (). However, in order to ensure that any Read method works correctly, you must know exactly where the data item is in the stream, because it is possible to read the saved double data as a simple byte sequence, or as a char or other format. You must either use a fixed format for the data in the file or save the extra information in a file so that you can correctly determine where the data is stored.

6. Read-Write random Access file
As noted earlier, Randomaccessfile is almost completely isolated from the rest of the IO hierarchy, although it also implements the Datainput and DataOutput interfaces. Therefore, it cannot be associated with any part of the InputStream and OutputStream subclasses. Although it may be possible to treat a bytearrayinputstream as a random access element, only a single file can be opened with Randomaccessfile. It must be assumed that the randomaccessfile has been properly buffered because we cannot choose for ourselves.
The second builder parameter can be selected by itself: You can decide to open a Randomaccessfile file in read-only (R) or read-write (rw) mode.
When using Randomaccessfile, it is similar to the combination of using DataInputStream and DataOutputStream (because it implements the equivalent interface). In addition, you can see that the program uses Seek () in order to move around in the file and make changes to a value.

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.