JAVA streams and files

Source: Internet
Author: User
Tags stream api

Flow

InputStream and OutputStream are super classes for all input and output streams. Both of them are abstract classes .

Both the Read method and the Write method are blocking methods , which means that if it is not possible to write or read, for example, because of network problems, the front-thread will be blocked.

InputStream's API:

Method Summary

int

available ()
Returns the number of bytes that the next caller of this input stream method can read (or skip) from this input stream in a blocked manner.

void

Close ()
closes this input stream and frees all system resources associated with the stream.

void

mark (int readlimit)
marks the current position in this input stream.

Boolean

marksupported ()
tests whether this input stream supports the mark and reset methods.

Abstract int

Read ()
reads the next data byte from the input stream.

int

Read (byte[] b)
reads a certain number of bytes from the input stream and stores it in buffer array B.

int

read (byte[] b, int off, int len)
reads up to Len data bytes in the input stream into a byte array.

void

Reset ()
Repositions This stream to the location when the mark method was last called on this input stream.

Long

Skip (long N)
Skips and discards n bytes of data in this input stream.

OutStream's API:

Method Summary

void

close ()
closes this output stream and frees all system resources related to this stream.

void

flush ()
refreshes this output stream and forces all buffered output bytes to be written out.

void

Write (byte[] b)
writes B.length bytes from a specified byte array to this output stream.

void

write (byte[] b, int off, int len)
writes to this output stream the Len bytes in the specified byte array starting at offset off.

abstract void

Write (int b)
Writes the specified bytes to this output stream.

Reader and writer

Abstract class for reading and writing a stream of Unicode characters. Similar to the byte stream API.

Look at the following API, more than similar, part-time is the same. Because either the read byte or the Unicode character (which is actually a Unicode encoding unit, 2 bytes), the int type (4 bytes) is used as the parameter.

Method Summary

abstract void

Close ()
Closes the stream.

void

mark (int readaheadlimit)
marks the current position in the stream.

Boolean

Marksupported ()
Determines whether this stream supports the mark () operation.

Int

Read ()
Reads a single character.

int

Read (char[] cbuf)
reads the characters into the array.

Abstract int

Read (char[] cbuf, int off, int len)
reads a character into a portion of the array.

int

Read (Charbuffer target)
attempted to read a character into the specified character buffer.

Boolean

Ready ()
Determines whether the stream is ready to be read.

void

Reset ()
Resets the stream.

Long

Skip (long N)
Skips a character.

Combined Flow Filter

Use as follows:

DataInputStream dis = new DataInputStream (new Bufferedinputstream (New FileInputStream ("Person.dat"));

Datainputstream/dataoutstream

The data input stream allows the application to read basic Java data types from the underlying input stream in a machine-independent manner . The application can use the data output stream to write data that is later read by the data input stream .

It's a way to see its common use scenarios:

Bufferedinputstream/bufferedoutstream

By maintaining a buffer internally, each time the Read method is executed, it is actually reading a fixed number of bytes into the memory buffer (through the construction parameters can be configured each time the number of bytes read, that is, the size of the buffer), and then the next time you read, directly from the memory buffer to take, Different in the original way (such as a network or a disk).

Write is also written to the internal buffer only when the buffer is full to make a real write operation.

JAVA streams and files

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.