"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