Java Io:fileoutputstream

Source: Internet
Author: User

Original link Author: Jakob Jenkov Translator: Li Jing ([email protected])

FileOutputStream can write a byte stream to a file, which is a subclass of OutputStream, so you can use FileOutputStream as you would with OutputStream.

This is an example of a fileoutputstream:

01 OutputStream output = newFileOutputStream("c:\\data\\output-text.txt");
02
03 while(moreData) {
04
05     intdata = getMoreData();
06
07     output.write(data);
08
09 }
10
11 output.close();

Note that in order to be clear, the necessary exception handling is ignored. For more information about exception handling, refer to Java IO exception handling.

The write () method of FileOutputStream takes an int variable containing the bytes to be written (translator note: Low 8-bit data) as a parameter.

FileOutputStream also has other constructors that allow you to write files in different ways. Please refer to the official documentation for more information.

Overwrite of file content override vs Append appending

When you create a fileoutputstream that points to an existing file, you can choose to overwrite the entire file, or append the content to the end of the file. Different purposes can be achieved by using different constructors.

One of the constructors takes a filename as a parameter, overwriting any file that this file name points to.

1 OutputStream output = newFileOutputStream("c:\\data\\output-text.txt");

Another constructor takes 2 parameters: A file name and a Boolean value that indicates whether you need to overwrite the files. Here is an example of a constructor function:

1 OutputStream output = newFileOutputStream("c:\\data\\output-text.txt", true); //appends to file
2
3 OutputStream output = newFileOutputStream("c:\\data\\output-text.txt", false); //overwrites file
Write byte array

Since FileOutputStream is a subclass of OutputStream, you can also write byte arrays to FileOutputStream without having to write only one byte at a time. Refer to my OutputStream tutorial for more information on writing byte arrays.

Flush ()

When you write data to FileOutputStream, the data is likely to be cached in memory. At some later time, for example, only x data is writable each time, or FileOutputStream is closed, it is actually written to disk. When FileOutputStream is not closed and you want to make sure that the data written to FileOutputStream is written to disk, you can call the flush () method, This method guarantees that all data written to FileOutputStream will be written to disk.

original articles, reproduced please specify: reproduced from the Concurrent programming network –ifeve.com This article link address: Java io:fileoutputstream

Java Io:fileoutputstream

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.