Java streams, files, and IO

Source: Internet
Author: User

Read and write files

A stream is defined as a data series. The input stream is used to read data from the source, and the output stream is used to write data to the target.

The class hierarchy diagram for the input stream and output stream.

FileInputStream

FileInputStream is used to read data from a file, and its objects can be created with the keyword new,

InputStream file = new FileInputStream ("D:\BI\201603\MyBi");

Or

File File = new file ("D:\BI\201603\MyBi");

InputStream file = new FileInputStream (f);

InputStream provides methods for the operation of convection:

Serial Number method and Description
1 public void Close () throws ioexception{}
Closes this file input stream and frees all system resources related to this stream. Throws a IOException exception.
2 protected void Finalize () throws IOException {}
This method clears the connection to the file. Make sure that the Close method is called when the file input stream is no longer referenced. Throws a IOException exception.
3 public int read (int r) throws ioexception{}
This method reads the specified byte of data from the InputStream object. Returns an integer value. Returns the next byte of data, or 1 if it has reached the end.
4 public int read (byte[] r) throws ioexception{}
This method reads bytes of r.length length from the input stream. Returns the number of bytes read. Returns 1 if it is the end of the file.
5 public int available () throws ioexception{}
Returns the number of bytes that the next method invoked on this input stream can be read from this input stream without blocking. Returns an integer value.
FileOutputStream

FileInputStream is used to create a file and write data to the file. If the destination file does not exist, the stream is created back to the file.

OutputStream f = new FileOutputStream ("D:\BI\201603\MyBi");

Or

File File = new file ("D:\BI\201603\MyBi");

OutputStream f = new FileOutputStream (file);

The OutputStream class provides a way to manipulate files

Serial Number method and Description
1 public void Close () throws ioexception{}
Closes this file input stream and frees all system resources related to this stream. Throws a IOException exception.
2 protected void Finalize () throws IOException {}
This method clears the connection to the file. Make sure that the Close method is called when the file input stream is no longer referenced. Throws a IOException exception.
3 public void Write (int w) throws ioexception{}
This method writes the specified bytes to the output stream.
4 public void Write (byte[] W)
Writes bytes of w.length length in the specified array to outputstream.

Java streams, files, and IO

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.