Java SE learning _ fileinputstream and fileoutputstream

Source: Internet
Author: User

Fileinputstream and fileoutputstream

Fileinputstream:

It is used to access local files in sequence and read original byte streams such as data. It inherits methods such as read and close from the super-class inputstream, and does not support methods and methods. its two common constructor methods are:

Fileinputstream (string filepath/* full file path */);

Fileinputstream (File fileobj/* file object describing the file */);

  

You can use this method to construct the file input stream:

(1) fileinputstream F1 = new fileinputstream("test.txt & quot ;);
(2) file F = new file("test.txt & quot ;);

 

Fileinputstream & nbsp; F2 = new fileinputstream (f );
Fileinputstream overrides the Data Reading method of the abstract class inputstream:
Public int read // read a Data byte from the input stream

Public int read (byte [] B); // read data of up to B bytes from the input stream into a byte array
Public int read (byte [] B, int off, int Len); // reads data of up to len bytes into the byte array from the input stream

If the input stream ends when data is read,-1 is returned.


Fileoutputstream:

Fileoutputstream is used to write data to a text file. It is used to write the streams of raw bytes such as data. It inherits from the superclass outputstream and other methods. Its common construction methods are as follows:

Fileoutputstream (string filepath/* full file path */)
Fileoutputstream (File fileobj/* object describing the file */)

Fileoutputstream (string filepath, Boolean append/* if it is true, the file is opened in append mode without overwriting the content of the existing file. If it is false, it overwrites the content of the original file */)
Fileoutputstream (File fileobj, Boolean append)

The creation of fileoutputstream does not depend on whether the file exists. If the file indicated by filepath does not exist, fileoutputstream will be created before it is opened. If the file exists, open it and prepare to write content. If a read-only file is opened, an ioexception is thrown.

Related Article

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.