Original link Author: Jakob Jenkov Translator: Li Jing ([email protected])
The InputStream class is the base class for all input streams in the Java IO API. InputStream subclasses include Fileinputstream,bufferedinputstream,pushbackinputstream and so on. Refer to the table at the bottom of this section of Java IO Overview to browse the complete list of InputStream subclasses.
Java InputStream Example
InputStream is used to read byte-based data, one byte at a time, which is an example of InputStream:
01 |
InputStream inputstream = newFileInputStream("c:\\data\\input-text.txt"); |
03 |
intdata = inputstream.read(); |
07 |
//do something with data... |
09 |
doSomethingWithData(data); |
11 |
data = inputstream.read(); |
This example creates a FileInputStream instance. FileInputStream is a subclass of InputStream, so you can assign FileInputStream instances to InputStream variables.
Note: For clarity, the code ignores some of the necessary exception handling. For more information about exception handling, refer to Java IO exception handling.
Starting with Java7, you can use the "try-with-resource" structure to make sure that InputStream is closed after the end of use, and links to an article about how "Try-with-resource" works, here is a simple example:
01 |
try( InputStream inputstream = newFileInputStream("file.txt") ) { |
03 |
intdata = inputstream.read(); |
07 |
System.out.print((char) data); |
09 |
data = inputstream.read(); |
The InputStream variable is closed when the execution thread exits the TRY statement block.
Read ()
The Read () method returns a byte content read from within the InputStream stream (translator note: 0~255), as shown in the following example:
1 |
intdata = inputstream.read(); |
You can convert the returned int type to char type:
1 |
charaChar = (char) data; |
The subclass of InputStream may contain alternative methods for the Read () method. For example, DataInputStream allows you to read Java basic type variables int,long,float,double and Boolean using methods such as Readboolean (), readdouble (), and so on.
Stream End
If the Read () method returns 1, it means that the program has read to the end of the stream, and there is no more data available to read in the stream. 1 is an int type, not a byte or char type, which is not the same.
When the end of the stream is reached, you can close the stream.
read (byte[])
The InputStream contains 2 read () Methods for reading data from the InputStream and storing the data in a buffered array, respectively:
- int read (byte[])
- int read (byte, int offset, int length)
It is much faster to read one byte array at a time than to read one byte at a time, so use these two methods instead of the read () method whenever possible.
The read (byte[]) method attempts to read the number of bytes that are as large as the size of a given byte array, and the return value describes the number of bytes that have been read. If the InputStream data is not sufficient to fill the byte array, the remainder of the array will contain the data before the read. Remember to check how much data is actually written to the byte array.
The read (byte, int offset, int length) method also reads the data into a byte array, unlike the method, which starts at the offset position of the array and writes a maximum of length bytes to the array. Similarly, read (byte, int offset, int length) method returns an int variable that tells you how many bytes have been written to the byte array, so remember to check the last call to read (byte, int offset, int) before reading the data The return value of length).
Both methods return 1 when reading to the end of the stream.
This is an example of read (byte[]) using InputStream:
01 |
InputStream inputstream = newFileInputStream("c:\\data\\input-text.txt"); |
03 |
byte[] data = newbyte[1024]; |
05 |
intbytesRead = inputstream.read(data); |
07 |
while(bytesRead != -1) { |
09 |
doSomethingWithData(data, bytesRead); |
11 |
bytesRead = inputstream.read(data); |
In the code, you first create a byte array. Then declare an int variable called Bytesread that stores each call to the read (byte[]) return value, and assign the return value given to it for the first call to read (byte[]).
Inside the while loop, pass the byte array and the number of bytes read as parameters to the Dosomethingwithdata method and then execute the call. At the end of the loop, the data is written again to the byte array.
You don't need to imagine a scene with Read (Byte, int offset, int length) instead of Read (byte[]), which can be used almost anywhere using read (byte, int offset, int length) read (byte[] )。
input streams and data sources
An input stream is often associated with a data source, such as files, network connections, pipelines, etc., and more details have been introduced in the Java IO Overview article.
original articles, reproduced please specify: reproduced from the Concurrent programming network –ifeve.com This article link address: Java io:inputstream
Java Io:inputstream