The input stream, the output stream is exactly how to distinguish, in writing code often encounter problems, or often confusing to find two of the relationship between streams. Through today's study to make a note, but also to provide everyone to learn, at the same time ask me questions.
The book says
Input stream: Data can only be read from and cannot be written to.
Output stream: Data can only be written to it and cannot be read from.
So I have a question, what the hell is this stream?
The book on the definition of flow is: Java in the different input/output source (keyboard, file, network connection, etc.) abstract of the bid is "flow." So, the stream is the keyboard, file, network connection and so on is only abstracted.
Then take FileInputStream and FileOutputStream as examples to distinguish what is a stream. What is an input stream. What is an output stream.
public static void Test1 () throws exception{
fileinputstream fileinputstream = new FileInputStream ("F:\\demo\\1.txt ");
Create output stream
fileoutputstream fos = new FileOutputStream ("F:\\demo\\2.txt");
The definition character is used to receive the data read in the file
byte[] bbuf = new byte[100];
Define a hasread to determine the number of read data
int hasread = 0;
String line = "";
while ((Hasread = Fileinputstream.read (bbuf)) > 0) {
System.out.println (new String (bbuf, 0, Hasread));
Writes the data read in the stream to the FOS stream to
fos.write (BBUF);
}
Turn off the input stream
fileinputstream.close ();
Fos.close ();
}
The function of this code is simply to read the data from the input stream and write it to the output stream.
so this input stream is 1.txt. The output stream is 2.txt
. The input stream (1.txt) reads the data from the previous input stream and the output stream definition. The output stream (2.txt) writes data to it.
in this case, the abstract flow is easy to understand.
Hope to have a better view of the following write their own profile for everyone to learn.
This article is a little ambiguous, I wrote an article, we look at it, you can clearly understand what is the input and output stream.
About FileInputStream and fileoutputstream input and output flow issues two