In the Java API, an object that can read a sequence of bytes from it is called an input stream, and an object that can write a sequence of bytes to it is called an output stream. The origin and destination of these byte sequences can be files, and usually files, but can also be network connections, or even blocks of memory. Abstract classes InputStream and OutputStream form the basis of the input/output (I/O) class hierarchy.
The InputStream class has an abstract method:
abstractint read()
This method reads in a byte and returns the read-in byte, or 1 when the end of the input source is encountered. When designing a specific input stream class, you must override this method to provide the applicable functionality.
Similarly, the OutputStream class defines the following abstract methods:
abstractvoid write(int b)
It can write a byte to an output location.
The read and write methods are blocked at execution until the bytes are actually read in or written out. This means that if the stream cannot be accessed immediately (usually because the network connection is busy), then the current thread will be blocked. This makes it possible for other threads to perform useful work while the two methods wait for the specified rheology to be available.
The available method allows us to check the number of bytes currently readable, and when the read and write of the stream is completed, it should be closed by calling the Close method, which frees up very limited operating system resources. If an application opens too many streams without shutting down, the system resources will be exhausted. Closing an output stream also flushes the buffers used for that output stream: All characters that are temporarily placed in the buffer so that they are passed in the form of a larger package are sent out when the output stream is closed. In particular, if you do not close the file, the last packet that writes out the bytes may never be passed. Of course, we can also flush the output artificially by flushing the method.
FileInputStream and FileOutputStream can provide input and output streams attached to a disk file, and you only need to provide their constructors with the file name or the full pathname of the file. For example:
new FileInputStream("test.dat");
This line of code can access files named "Test.dat" in the user directory.
Like abstract classes InputStream and OutputStream, these classes only support read and write at the byte level. In other words, we can only read bytes and byte arrays from the Fin object.
Just as FileInput Stream does not have any method of reading the numeric type, DataInputStream does not have any method of fetching data from the file. Java uses a clever mechanism to separate these two responsibilities. Some streams, such as fileinputstream and the input stream returned by the OpenStream method of the URL class, can get bytes from files and other more external locations, while other streams (such as DataInputStream and PrintWriter) can assemble bytes into more useful data types. We have to combine the two. For example, to read a number from a file, you first need to create a fileinputstream and then pass it to the DataInputStream constructor:
new FileInputStream("test.dat"new DataInputStream(fin);int i = din.readInt();
We can also add multiple functions by nesting filters. For example, the stream is not cached by the buffer by default, that is, each call to read requests the operating system to redistribute one byte. In contrast, it is more efficient to request a block of data and place it in a buffer. If we want to use the buffering mechanism and the data entry method for the file, then we need to use this rather scary constructor sequence:
new DataInputStream( new BufferedInputStream( new FileInputStream("test.dat")));
In the Stream class library of other programming languages, details such as buffering mechanisms and previews are handled automatically. So, in comparison, Java has a bit of a problem, and it has to combine multiple stream filters. However, the ability to mix and match filter classes to build truly useful flow sequences will provide great flexibility.
Java input/output (I/O) Learning notes-stream