1.What is stream?
Drain a very image of the concept, when the program needs to read the data, it will open a stream to the data source, the data source can be a file, memory, or network connection. Similarly, when a program needs to write data, it opens a stream to the destination.
In Java, the part of Io is large and consists of two main parts: byte stream and character stream. Concrete structure such as (image from the Internet):
2: What is a byte stream?
A stream that is transmitted in bytes. The input stream ancestor of the byte stream is: InputStream (the Data unit is byte (8bits)) The ancestor of the output is OutputStream (the data unit is byte (8bits));
"Here's a rule: whether it's sub-class or is Decoration class Are presented in Xxxinputstream and Xxxoutputstream, and the last constant Putstream determines that it is a byte stream .
FileInputStream and Fileoutstream are node streams, but they are inheritance relationships, and their construction methods allow the corresponding stream to be constructed by the path name of the file.
such as: FileInputStream infile = new Fileinputsream ("E:\\mytest");(note that the input stream must be present and readable when it is used, while the output stream must be present and overwritten.
);
Bufferedinputstream and Bufferoutputstream, they are filter flows, they are not inheritance relationships with ancestors, but are decorated and decorated with the relationship (actually used in decorator mode), the effect is to improve the efficiency of input and output, It provides a way of buffering;
DataInputStream and DataOutputStream are data flows and data output streams, and they are not inheritance relationships with their ancestors, but are decorated and decorated, mainly used in (decorative mode) It can be implemented in a machine-independent way to read and write Java programs, often used in network transmission.
3: What is a character stream
A stream of characters that is transmitted primarily as a character stream. The ancestors of the character stream input and output are the reader and writer data Units (16bits), respectively.
"Here's a rule: whether it's sub-class or is Decoration class are Xxxreader and xxxwriter, the last constant reader or writer determines that it is a character stream "
InputStreamReader and OutputStreamWriter. It is constructed using the specified charset to read the bytes and decode it as a character.
bufferreaded and bufferwriter have the same effect as the above byte stream, and they are not inherited from their ancestors, but are decorated and decorated (actually used in decorator mode) , the use of buffering greatly increases the efficiency of the input and output, in code reading and writing in the behavior unit.
Code:
Import java.io.*;p ublic class textread{public static void Main (string[] args) { File fin,fout; BufferedReader bf = null; PrintWriter pw = null; try{ fin = new File ("Zzc.txt");//Note that both the file and the program are in the same folder. Zzc.txt is the file to be copied. fout = new File ("Copyzzc.txt");//If it is not created automatically. bf = new BufferedReader (new FileReader (Fin)); PW = new PrintWriter (fout); PrintWriter is a print stream, or you can use BufferedWriter. String line = Bf.readline (); while (line!=null) { pw.println (line); line = Bf.readline (); } } catch (Exception e) { e.printstacktrace (); } finally{ try{ //close file. if (bf!=null) { bf.close (); BF = null; } if (pw!=null) { pw.close (); PW = null; } } catch (Exception e) { e.printstacktrace ();}}}}
Import java.io.*;p ublic class textread{public static void Main (string[] args) { BufferedReader bf = null;/* BufferedReader equivalent to a large bucket, in fact, is the memory, here to achieve a large number of read and write, rather than read a byte or character directly written like a hard disk, strengthen the protection of the hard disk. * /try{while (true) {//while (true) {} loop Guarantee program does not end BF = new BufferedReader (new InputStreamReader (system.in) ); /*system.in is the standard input, System.out is the standard output */ /*inputstreamreader language to stream bytes to the conversion of a character stream, which is the process flow * Here quite with 2 pipes are connected between the system.in and the program. the *readline () method is more useful, and it also enables better functionality by processing streams. **/ String line = Bf.readline (); System.out.println (line); } } catch (Exception e) { e.printstacktrace (); } finally{ //Be sure to close the stream after you run out. Better put it in the filally. try{ if (bf!=null) { bf.close (); } } catch (Exception e) { e.printstacktrace ();}}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java io in layman's