天天看點

Java IO: InputStreamJava IO: InputStream

Java Io 

1

<a target="_blank" href="http://tutorials.jenkov.com/java-io/index.html">Java IO Tutorial</a>

2

<a target="_blank" href="http://tutorials.jenkov.com/java-io/overview.html">Java IO Overview</a>

3

<a target="_blank" href="http://tutorials.jenkov.com/java-io/files.html">Java IO: Files</a>

4

<a target="_blank" href="http://tutorials.jenkov.com/java-io/pipes.html">Java IO: Pipes</a>

5

<a target="_blank" href="http://tutorials.jenkov.com/java-io/networking.html">Java IO: Networking</a>

6

<a target="_blank" href="http://tutorials.jenkov.com/java-io/arrays.html">Java IO: Byte &amp; Char Arrays</a>

7

<a target="_blank" href="http://tutorials.jenkov.com/java-io/system-in-out-error.html">Java IO: System.in, System.out, and System.error</a>

8

<a target="_blank" href="http://tutorials.jenkov.com/java-io/streams.html">Java IO: Streams</a>

9

<a target="_blank" href="http://tutorials.jenkov.com/java-io/input-parsing.html">Java IO: Input Parsing</a>

10

<a target="_blank" href="http://tutorials.jenkov.com/java-io/readers-writers.html">Java IO: Readers and Writers</a>

11

<a target="_blank" href="http://tutorials.jenkov.com/java-io/concurrent-io.html">Java IO: Concurrent IO</a>

12

<a target="_blank" href="http://tutorials.jenkov.com/java-io/io-exception-handling.html">Java IO: Exception Handling</a>

13

<a target="_blank" href="http://tutorials.jenkov.com/java-io/inputstream.html">Java IO: InputStream</a>

14

<a target="_blank" href="http://tutorials.jenkov.com/java-io/outputstream.html">Java IO: OutputStream</a>

15

<a target="_blank" href="http://tutorials.jenkov.com/java-io/fileinputstream.html">Java IO: FileInputStream</a>

16

<a target="_blank" href="http://tutorials.jenkov.com/java-io/fileoutputstream.html">Java IO: FileOutputStream</a>

17

<a target="_blank" href="http://tutorials.jenkov.com/java-io/randomaccessfile.html">Java IO: RandomAccessFile</a>

18

<a target="_blank" href="http://tutorials.jenkov.com/java-io/file.html">Java IO: File</a>

19

<a target="_blank" href="http://tutorials.jenkov.com/java-io/pipedinputstream.html">Java IO: PipedInputStream</a>

20

<a target="_blank" href="http://tutorials.jenkov.com/java-io/pipedoutputstream.html">Java IO: PipedOutputStream</a>

21

<a target="_blank" href="http://tutorials.jenkov.com/java-io/bytearrayinputstream.html">Java IO: ByteArrayInputStream</a>

22

<a target="_blank" href="http://tutorials.jenkov.com/java-io/bytearrayoutputstream.html">Java IO: ByteArrayOutputStream</a>

23

<a target="_blank" href="http://tutorials.jenkov.com/java-io/filterinputstream.html">Java IO: FilterInputStream</a>

24

<a target="_blank" href="http://tutorials.jenkov.com/java-io/filteroutputstream.html">Java IO: FilterOutputStream</a>

25

<a target="_blank" href="http://tutorials.jenkov.com/java-io/bufferedinputstream.html">Java IO: BufferedInputStream</a>

26

<a target="_blank" href="http://tutorials.jenkov.com/java-io/bufferedoutputstream.html">Java IO: BufferedOutputStream</a>

27

<a target="_blank" href="http://tutorials.jenkov.com/java-io/pushbackinputstream.html">Java IO: PushbackInputStream</a>

28

<a target="_blank" href="http://tutorials.jenkov.com/java-io/sequenceinputstream.html">Java IO: SequenceInputStream</a>

29

<a target="_blank" href="http://tutorials.jenkov.com/java-io/datainputstream.html">Java IO: DataInputStream</a>

30

<a target="_blank" href="http://tutorials.jenkov.com/java-io/dataoutputstream.html">Java IO: DataOutputStream</a>

31

<a target="_blank" href="http://tutorials.jenkov.com/java-io/printstream.html">Java IO: PrintStream</a>

32

<a target="_blank" href="http://tutorials.jenkov.com/java-io/objectinputstream.html">Java IO: ObjectInputStream</a>

33

<a target="_blank" href="http://tutorials.jenkov.com/java-io/objectoutputstream.html">Java IO: ObjectOutputStream</a>

34

<a target="_blank" href="http://tutorials.jenkov.com/java-io/serializable.html">Java IO: Serializable</a>

35

<a target="_blank" href="http://tutorials.jenkov.com/java-io/reader.html">Java IO: Reader</a>

36

<a target="_blank" href="http://tutorials.jenkov.com/java-io/writer.html">Java IO: Writer</a>

37

<a target="_blank" href="http://tutorials.jenkov.com/java-io/inputstreamreader.html">Java IO: InputStreamReader</a>

38

<a target="_blank" href="http://tutorials.jenkov.com/java-io/outputstreamwriter.html">Java IO: OutputStreamWriter</a>

39

<a target="_blank" href="http://tutorials.jenkov.com/java-io/filereader.html">Java IO: FileReader</a>

40

<a target="_blank" href="http://tutorials.jenkov.com/java-io/filewriter.html">Java IO: FileWriter</a>

41

<a target="_blank" href="http://tutorials.jenkov.com/java-io/pipedreader.html">Java IO: PipedReader</a>

42

<a target="_blank" href="http://tutorials.jenkov.com/java-io/pipedwriter.html">Java IO: PipedWriter</a>

43

<a target="_blank" href="http://tutorials.jenkov.com/java-io/chararrayreader.html">Java IO: CharArrayReader</a>

44

<a target="_blank" href="http://tutorials.jenkov.com/java-io/chararraywriter.html">Java IO: CharArrayWriter</a>

45

<a target="_blank" href="http://tutorials.jenkov.com/java-io/bufferedreader.html">Java IO: BufferedReader</a>

46

<a target="_blank" href="http://tutorials.jenkov.com/java-io/bufferedwriter.html">Java IO: BufferedWriter</a>

47

<a target="_blank" href="http://tutorials.jenkov.com/java-io/filterreader.html">Java IO: FilterReader</a>

48

<a target="_blank" href="http://tutorials.jenkov.com/java-io/filterwriter.html">Java IO: FilterWriter</a>

49

<a target="_blank" href="http://tutorials.jenkov.com/java-io/pushbackreader.html">Java IO: PushbackReader</a>

50

<a target="_blank" href="http://tutorials.jenkov.com/java-io/linenumberreader.html">Java IO: LineNumberReader</a>

51

<a target="_blank" href="http://tutorials.jenkov.com/java-io/streamtokenizer.html">Java IO: StreamTokenizer</a>

52

<a target="_blank" href="http://tutorials.jenkov.com/java-io/printwriter.html">Java IO: PrintWriter</a>

53

<a target="_blank" href="http://tutorials.jenkov.com/java-io/stringreader.html">Java IO: StringReader</a>

54

<a target="_blank" href="http://tutorials.jenkov.com/java-io:-stringwriter.html">Java IO: StringWriter</a>

Java IO: InputStreamJava IO: InputStream

Rate article:

&lt;iframe frameborder="0" hspace="0" marginheight="0" marginwidth="0" scrolling="no" tabindex="0" vspace="0" width="100%" id="I0_1416446177219" name="I0_1416446177219" src="https://apis.google.com/se/0/_/+1/fastbutton?usegapi=1&amp;amp;origin=http%3A%2F%2Ftutorials.jenkov.com&amp;amp;url=http%3A%2F%2Ftutorials.jenkov.com%2Fjava-io%2Finputstream.html&amp;amp;gsrc=3p&amp;amp;ic=1&amp;amp;jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.zh_CN.0KI2lcOUxJ0.O%2Fm%3D__features__%2Fam%3DAQ%2Frt%3Dj%2Fd%3D1%2Ft%3Dzcms%2Frs%3DAGLTcCPnLWTRWXjQ3yHtGTFSsUVyRcOV5g#_methods=onPlusOne%2C_ready%2C_close%2C_open%2C_resizeMe%2C_renderstart%2Concircled%2Cdrefresh%2Cerefresh&amp;amp;id=I0_1416446177219&amp;amp;parent=http%3A%2F%2Ftutorials.jenkov.com&amp;amp;pfname=&amp;amp;rpctoken=27601238" data-gapiattached="true" style="position: absolute; top: -10000px; width: 450px; margin: 0px; border-style: none;"&gt;&lt;/iframe&gt;

Share article:

&lt;iframe frameborder="0" hspace="0" marginheight="0" marginwidth="0" scrolling="no" tabindex="0" vspace="0" width="100%" id="I1_1416446177224" name="I1_1416446177224" src="https://apis.google.com/se/0/_/+1/sharebutton?plusShare=true&amp;amp;usegapi=1&amp;amp;action=share&amp;amp;height=24&amp;amp;annotation=none&amp;amp;origin=http%3A%2F%2Ftutorials.jenkov.com&amp;amp;url=http%3A%2F%2Ftutorials.jenkov.com%2Fjava-io%2Finputstream.html&amp;amp;gsrc=3p&amp;amp;ic=1&amp;amp;jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.zh_CN.0KI2lcOUxJ0.O%2Fm%3D__features__%2Fam%3DAQ%2Frt%3Dj%2Fd%3D1%2Ft%3Dzcms%2Frs%3DAGLTcCPnLWTRWXjQ3yHtGTFSsUVyRcOV5g#_methods=onPlusOne%2C_ready%2C_close%2C_open%2C_resizeMe%2C_renderstart%2Concircled%2Cdrefresh%2Cerefresh%2Conload&amp;amp;id=I1_1416446177224&amp;amp;parent=http%3A%2F%2Ftutorials.jenkov.com&amp;amp;pfname=&amp;amp;rpctoken=22162960" data-gapiattached="true" style="position: absolute; top: -10000px; width: 450px; margin: 0px; border-style: none;"&gt;&lt;/iframe&gt;

<a target="_blank" href="https://twitter.com/share">Tweet</a>

Table of Contents

<a target="_blank" href="http://tutorials.jenkov.com/java-io/inputstream.html#inputstreams-and-sources">InputStreams and Sources</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-io/inputstream.html#java-inputstream-example">Java InputStream Example</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-io/inputstream.html#read">read()</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-io/inputstream.html#read-byte-array">read(byte[])</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-io/inputstream.html#mark-and-reset">mark() and reset()</a>

<a target="_blank"></a>

Java <code>InputStream</code>'s are used for reading byte based data, one byte at a time. Here is a Java <code>InputStream</code>example:

This example creates a new <code>FileInputStream</code> instance. <code>FileInputStream</code> is a subclass of <code>InputStream</code> so it is safe to assign an instance of <code>FileInputStream</code> to an <code>InputStream</code> variable (the <code>inputstream</code> variable).

Once the executing thread exits the <code>try</code> block, the <code>inputstream</code> variable is closed.

The <code>read()</code> method of an <code>InputStream</code> returns an int which contains the byte value of the byte read. Here is an<code>InputStream</code> <code>read()</code> example:

You can case the returned <code>int</code> to a <code>char</code> like this:

Subclasses of <code>InputStream</code> may have alternative <code>read()</code> methods. For instance, the <code>DataInputStream</code> allows you to read Java primitives like int, long, float, double, boolean etc. with its corresponding methods <code>readBoolean()</code>,<code>readDouble()</code> etc.

If the <code>read()</code> method returns -1, the end of stream has been reached, meaning there is no more data to read in the<code>InputStream</code>. 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 <code>InputStream</code>.

The <code>InputStream</code> class also contains two <code>read()</code> methods which can read data from the <code>InputStream</code>'s source into a <code>byte</code> array. These methods are:

<code>int read(byte[])</code>

<code>int read(byte[], int offset, int length)</code>

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 <code>read()</code> method.

The <code>read(byte[])</code> method will attempt to read as many bytes into the <code>byte</code> array given as parameter as the array has space for. The <code>read(byte[])</code> method returns an <code>int</code> telling how many bytes were actually read. In case less bytes could be read from the <code>InputStream</code> than the <code>byte</code> array has space for, the rest of the <code>byte</code> 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 <code>byte</code> array.

The <code>read(byte[], int offset, int length)</code> method also reads bytes into a <code>byte</code> array, but starts at <code>offset</code>bytes into the array, and reads a maximum of <code>length</code> bytes into the array from that position. Again, the<code>read(byte[], int offset, int length)</code> method returns an <code>int</code> 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 <code>InputStream</code>'s <code>read(byte[])</code> method:

First this example create a <code>byte</code> array. Then it creates an <code>int</code> variable named <code>bytesRead</code> to hold the number of bytes read for each <code>read(byte[])</code> call, and immediately assigns <code>bytesRead</code> the value returned from the first<code>read(byte[])</code> call.

Inside the <code>while</code> loop the <code>doSomethingWithData()</code> method is called, passing along the <code>data</code> <code>byte</code> array as well as how many bytes were read into the array as parameters. At the end of the <code>while</code> loop data is read into the <code>byte</code>array again.

It should not take much imagination to figure out how to use the <code>read(byte[], int offset, int length)</code>method instead of <code>read(byte[])</code>. You pretty much just replace the <code>read(byte[])</code> calls with <code>read(byte[], int offset, int length)</code> calls.

The <code>InputStream</code> class has two methods called <code>mark()</code> and <code>reset()</code> which subclasses of <code>InputStream</code> may or may not support.

If an <code>InputStream</code> subclass supports the <code>mark()</code> and <code>reset()</code> methods, then that subclass should override the<code>markSupported()</code> to return <code>true</code>. If the <code>markSupported()</code> method returns <code>false</code> then <code>mark()</code> and <code>reset()</code> are not supported.

The <code>mark()</code> sets a mark internally in the <code>InputStream</code> which marks the point in the stream to which data has been read so far. The code using the <code>InputStream</code> can then continue reading data from it. If the code using the<code>InputStream</code> wants to go back to the point in the stream where the mark was set, the code calls <code>reset()</code> on the<code>InputStream</code>. The <code>InputStream</code> 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 the <code>InputStream</code>.

The methods <code>mark()</code> and <code>reset()</code> methods are typically used when implementing parsers. Sometimes a parser may need to read ahead in the <code>InputStream</code> 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.

<a target="_blank" href="http://tutorials.jenkov.com/java-io/outputstream.html">Next:   Java IO: OutputStream</a>