"Javase" day09_ node stream and advanced flow

Source: Internet
Author: User

"Javase" day09_ node stream and advanced flow


1.FileOutputStream

1) Java.io.FileOutputStream

* The byte output stream of the file used to write out bytes to the file. is a low-level stream.


2) The FOS created by default is an overwrite write operation.

* The overwrite operation clears all data from the original file before writing, and then re-writes the new content.


3) also supports an overloaded construction method, the second parameter passes in a Boolean value, and if the value is true, it is an append-write operation.

* That is, the data written through this stream will write at the end of the current file. Instead of erasing the original data.

See code for specific implementations:


Package Day02;import java.io.fileoutputstream;import java.io.ioexception;/** * java.io.FileOutputStream * file byte output stream, Used to write out bytes to the file. is a low-level stream. * */public class FOSDemo1 {public static void main (string[] args) throws IOException {/* * * The FOS created by default is an overwrite write operation. * The overwrite operation clears all data from the original file before writing, and then re-writes the new content. *///fileoutputstream fos = new FileOutputStream ("Fos.txt");/* also supports an overloaded construction method, the second parameter passes in a * Boolean value, and if the value is true, the append write operation, * That is, the data written out through the stream is written at the end of the current file. * Instead of erasing the original data. */fileoutputstream fos = new FileOutputStream ("Fos.txt", true); String str = "One Step two steps, one step two steps like a minion, like the Devil's pace"; byte[] data = Str.getbytes (); fos.write (data);/* * The stream is used to close, release resources! */fos.close ();}}

2.FileInputStream

1) fileinputstream----Low-level stream, which is used to read the stream of file data.

Code:

Package Day02;import java.io.fileinputstream;import java.io.ioexception;/** * FileInputStream * Low stream, stream for reading file data * */ public class FISDemo1 {public static void main (string[] args) throws IOException {FileInputStream fis = new Fileinputstrea M ("Fos.txt"); byte[] data = new Byte[200];int len = fis.read (data); String str = new string (Data,0,len); System.out.println (str); Fis.close ();}}

3.BufferedOutputStream and Bufferedinputstream

See the code for details:

Package Day02;import Java.io.bufferedoutputstream;import Java.io.fileoutputstream;import java.io.IOException;/** *  Buffered stream Write Data considerations: */public class Bosdemo {public static void main (string[] args) throws IOException {FileOutputStream fos = new FileOutputStream ("Bos.txt"); Bufferedoutputstream BOS = new Bufferedoutputstream (FOS); String str = "I love Beijing Tian ' an gate"; byte[] data = Str.getbytes (); bos.write (data);/* * void flush () * This method forces the data in the buffer to be written out once, regardless of whether the buffer * is filled 。 Frequent calls to the flush () method will increase the number of writes written from * to less efficient, but will guarantee the timeliness of writing. */bos.flush ();/* * When the buffer stream is closed, it will first call flush () to write out the remaining data in the buffer * before closing the stream it handles before shutting itself down. */bos.close ();}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Javase" day09_ node stream and advanced flow

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.