Java input/output stream
Pre-knowledge preparation
1. Basic Java Syntax
Basic principle:
While the program is running, it may be necessary to read the required data from an external storage medium or other program, which requires the use of an input stream object. The input stream points to the source called it, and the program reads the source data from the input stream that points to the source. On the other hand, after processing the data, the program may need to write processing results to a permanent storage medium or to other applications, which requires the use of an output stream object. The point of the output stream is called its destination, and the program transmits the data to the destination by writing data to the output stream.
( This post only gives the file byte stream, the file character stream, the buffered stream instance )
File byte stream:
FileInputStream parent class InputStream
FileOutputStream Parent class OutputStream
File inch Putstream constructor function File inch Putstream ( String name )
File inch Putstream ( File File )
FileOutputStream constructor fileoutputstream(String name )
FileOutputStream ( File File )
File byte Stream instance:
<p><span style= "Color:rgb (127,0,85);" >import</span> java.io.fileoutputstream;</p><p><span style= "Color:rgb (127,0,85);" >public</span> <span style= "Color:rgb (127,0,85);" >class</span> study {</p><p><span style= "Color:rgb (127,0,85);" >public</span> <span style= "Color:rgb (127,0,85);" >static</span> <span style= "Color:rgb (127,0,85);" >void</span> main (String [] <span style= "Color:rgb (106,62,62);" >args</span>) {</p><p><span style= "Color:rgb (127,0,85);" >byte</span>[] <span style= "Color:rgb (106,62,62);" >a</span> = <span style= "Color:rgb (42,0,255);" > "I am 110</span><span style=" Color:rgb (42,0,255); >,</span><span style= "Color:rgb (42,0,255);" > "</span>.getbytes (); </p><p><span style=" Color:rgb (127,0,85); " >byte</span>[] <span style= "Color:rgb (106,62,62); " >b</span> = <span style= "Color:rgb (42,0,255);" > "You are 120</span><span style=" Color:rgb (42,0,255); >,</span><span style= "Color:rgb (42,0,255);" > "</span>.getbytes (); </p><p><span style=" Color:rgb (127,0,85); " >try</span> {</p><p>fileoutputstream <span style= "Color:rgb (106,62,62);" >out</span> = <span style= "Color:rgb (127,0,85);" >new</span> fileoutputstream (<span style= "Color:rgb (42,0,255);" > "Happy.txt" </span> </p><p><span style= "Color:rgb (106,62,62);" >out</span>.write (<span style= "Color:rgb (106,62,62);" >a</span>); </p><p><span style= "Color:rgb (106,62,62);" >out</span>.write (<span style= "Color:rgb (106,62,62);" >b</span>, 0, <span style= "Color:rgb (106,62,62);" >b</span>.<span style= "Color:rgb (0,0,192);" >length</span>); </P><p><span style= "Color:rgb (106,62,62);" >out</span>.close (); </p><p>} <span style= "Color:rgb (127,0,85);" >catch</span> (Exception <span style= "Color:rgb (106,62,62);" >e</span>) {</p><p><span style= "Color:rgb (106,62,62);" >e</span>.printstacktrace ();</p><p>}</p><p>}</p><p>}</p>
Output Result:
File character stream:
FileReader Parent Reader
FileWriter The parent class Writer
FileReader Constructor filereader (String filename)
FileReader (File filename)
FileWriter Constructor FileWriter (String filename)
FileWriter (File filename)
File character Stream instance:
Import Java.io.file;import Java.io.filereader;import Java.io.filewriter;public class Study1 {public static void main ( string [] args) {string str = "Welcome attention, UCKY.K's blog."; try {//write from cache to destination file F = new file ("Hello.txt"); FileWriter out = new FileWriter (f); Out.write (Str,0,str.length ()); Out.close ();//read from source cache FileReader in = new FileReader (f) ; char[] sr = new Char[20];if (In.read (SR)!=-1) {System.out.println (SR);} In.close ();} catch (Exception e) {e.printstacktrace ();}}}
Output Result:
Buffered streams:
BufferedReader Parent Reader
BufferedWriter The parent class Writer
BufferedReader constructor function BufferedReader (Reader in)
bufferedwriter constructor function BufferedWriter (Writer out)
BufferedReader stream can read lines of text by ReadLine ()
You can call BufferedReader and BufferedWriter as the upper stream , and refer to the stream of characters they point to as the underlying flow . Java uses caching techniques to connect the upper and lower streams. The underlying character input stream first reads the data into the cache, the BufferedReader stream then reads the data from the cache, the Bufferedwirter stream writes the data to the cache, and the underlying character output stream continuously writes the data from the cache to the destination.
Buffered Stream instances:
Package Study;import Java.io.bufferedreader;import Java.io.bufferedwriter;import java.io.file;import Java.io.filereader;import Java.io.filewriter;public class Study2 {public static void main (string[] args) {File File = new F Ile ("Student.txt"); String content[]={"Merchandise list", "Cherry: 10 Yuan/kg", "Strawberry: 5 kg/catty", "Mango: 8 kg"};try {bufferedwriter out = new BufferedWriter (New FileWriter ( file)); for (String str:content) {out.write (str); Out.newline ();} Out.close (); BufferedReader in = new BufferedReader (new FileReader (file)); String s = Null;while ((S=in.readline ())!=null) {System.out.print (s);}} catch (Exception e) {e.printstacktrace ();}}}
Output Result:
Java input/output stream (byte stream, character stream, buffered stream)