首页 > 代码库 > Java IO: InputStream
Java IO: InputStream
Java Io
1 | Java IO Tutorial |
2 | Java IO Overview |
3 | Java IO: Files |
4 | Java IO: Pipes |
5 | Java IO: Networking |
6 | Java IO: Byte & Char Arrays |
7 | Java IO: System.in, System.out, and System.error |
8 | Java IO: Streams |
9 | Java IO: Input Parsing |
10 | Java IO: Readers and Writers |
11 | Java IO: Concurrent IO |
12 | Java IO: Exception Handling |
13 | Java IO: InputStream |
14 | Java IO: OutputStream |
15 | Java IO: FileInputStream |
16 | Java IO: FileOutputStream |
17 | Java IO: RandomAccessFile |
18 | Java IO: File |
19 | Java IO: PipedInputStream |
20 | Java IO: PipedOutputStream |
21 | Java IO: ByteArrayInputStream |
22 | Java IO: ByteArrayOutputStream |
23 | Java IO: FilterInputStream |
24 | Java IO: FilterOutputStream |
25 | Java IO: BufferedInputStream |
26 | Java IO: BufferedOutputStream |
27 | Java IO: PushbackInputStream |
28 | Java IO: SequenceInputStream |
29 | Java IO: DataInputStream |
30 | Java IO: DataOutputStream |
31 | Java IO: PrintStream |
32 | Java IO: ObjectInputStream |
33 | Java IO: ObjectOutputStream |
34 | Java IO: Serializable |
35 | Java IO: Reader |
36 | Java IO: Writer |
37 | Java IO: InputStreamReader |
38 | Java IO: OutputStreamWriter |
39 | Java IO: FileReader |
40 | Java IO: FileWriter |
41 | Java IO: PipedReader |
42 | Java IO: PipedWriter |
43 | Java IO: CharArrayReader |
44 | Java IO: CharArrayWriter |
45 | Java IO: BufferedReader |
46 | Java IO: BufferedWriter |
47 | Java IO: FilterReader |
48 | Java IO: FilterWriter |
49 | Java IO: PushbackReader |
50 | Java IO: LineNumberReader |
51 | Java IO: StreamTokenizer |
52 | Java IO: PrintWriter |
53 | Java IO: StringReader |
54 | Java IO: StringWriter |
Java IO: InputStream
The InputStream
class is the base class (superclass) of all input streams in the Java IO API. InputStream
Subclasses include the FileInputStream
, BufferedInputStream
and the PushbackInputStream
among others. To see a full list of InputStream
subclasses, go to the bottom table of the Java IO Overview page.
InputStreams and Sources
An InputStream
is typically always connected to some data source, like a file, network connection, pipe etc. This is also explained in more detail in the Java IO Overview text.
Java InputStream Example
Java InputStream
‘s are used for reading byte based data, one byte at a time. Here is a Java InputStream
example:
InputStream inputstream = new FileInputStream("c:\\data\\input-text.txt"); int data = http://www.mamicode.com/inputstream.read();>This example creates a new
FileInputStream
instance.FileInputStream
is a subclass ofInputStream
so it is safe to assign an instance ofFileInputStream
to anInputStream
variable (theinputstream
variable).Note: The proper exception handling has been skipped here for the sake of clarity. To learn more about correct exception handling, go to Java IO Exception Handling.
From Java 7 you can use the try-with-resources construct to make sure the
InputStream
is properly closed after use. The link in the previous sentence points to an article that explains how it works in more detail, but here is a simple example:try( InputStream inputstream = new FileInputStream("file.txt") ) { int data = http://www.mamicode.com/inputstream.read();>Once the executing thread exits the
try
block, theinputstream
variable is closed.read()
The
read()
method of anInputStream
returns an int which contains the byte value of the byte read. Here is anInputStream
read()
example:int data = http://www.mamicode.com/inputstream.read();>You can case the returned
int
to achar
like this:char aChar = (char) data;Subclasses of
InputStream
may have alternativeread()
methods. For instance, theDataInputStream
allows you to read Java primitives like int, long, float, double, boolean etc. with its corresponding methodsreadBoolean()
,readDouble()
etc.End of Stream
If the
read()
method returns -1, the end of stream has been reached, meaning there is no more data to read in theInputStream
. That is, -1 as int value, not -1 as byte or short value. There is a difference here!When the end of stream has been reached, you can close the
InputStream
.read(byte[])
The
InputStream
class also contains tworead()
methods which can read data from theInputStream
‘s source into abyte
array. These methods are:
int read(byte[])
int read(byte[], int offset, int length)
Reading an array of bytes at a time is much faster than reading one byte at a time, so when you can, use these read methods instead of the read()
method.
The read(byte[])
method will attempt to read as many bytes into the byte
array given as parameter as the array has space for. The read(byte[])
method returns an int
telling how many bytes were actually read. In case less bytes could be read from the InputStream
than the byte
array has space for, the rest of the byte
array will contain the same data as it did before the read started. Remember to inspect the returned int to see how many bytes were actually read into the byte
array.
The read(byte[], int offset, int length)
method also reads bytes into a byte
array, but starts at offset
bytes into the array, and reads a maximum of length
bytes into the array from that position. Again, theread(byte[], int offset, int length)
method returns an int
telling how many bytes were actually read into the array, so remember to check this value before processing the read bytes.
For both methods, if the end of stream has been reached, the method returns -1 as the number of bytes read.
Here is an example of how it could looke to use the InputStream
‘s read(byte[])
method:
InputStream inputstream = new FileInputStream("c:\\data\\input-text.txt"); byte[] data = http://www.mamicode.com/new byte[1024];>First this example create a
byte
array. Then it creates anint
variable namedbytesRead
to hold the number of bytes read for eachread(byte[])
call, and immediately assignsbytesRead
the value returned from the firstread(byte[])
call.Inside the
while
loop thedoSomethingWithData()
method is called, passing along thedata
byte
array as well as how many bytes were read into the array as parameters. At the end of thewhile
loop data is read into thebyte
array again.It should not take much imagination to figure out how to use the
read(byte[], int offset, int length)
method instead ofread(byte[])
. You pretty much just replace theread(byte[])
calls withread(byte[], int offset, int length)
calls.mark() and reset()
The
InputStream
class has two methods calledmark()
andreset()
which subclasses ofInputStream
may or may not support.If an
InputStream
subclass supports themark()
andreset()
methods, then that subclass should override themarkSupported()
to returntrue
. If themarkSupported()
method returnsfalse
thenmark()
andreset()
are not supported.The
mark()
sets a mark internally in theInputStream
which marks the point in the stream to which data has been read so far. The code using theInputStream
can then continue reading data from it. If the code using theInputStream
wants to go back to the point in the stream where the mark was set, the code callsreset()
on theInputStream
. TheInputStream
then "rewinds" and go back to the mark, and start returning (reading) data from that point again. This will of course result in some data being returned more than once from theInputStream
.The methods
mark()
andreset()
methods are typically used when implementing parsers. Sometimes a parser may need to read ahead in theInputStream
and if the parser doesn‘t find what it expected, it may need to rewind back and try to match the read data against something else.
Java IO: InputStream