📄 javaio.doc10.html
字号:
<html>
<head>
<title>The Java Language Specification The Package java.io </title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<a href="index.html">Contents</a> | <a href="javaio.doc9.html">Prev</a> | <a href="javaio.doc11.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="32925"></a>
<center><h1>22.12 The Class <code>java.io.LineNumberInputStream</code></h1></center>
<a name="32926"></a>
A <code>LineNumberInputStream</code> adds functionality to another input stream, namely
the ability to count lines. When the <code>LineNumberInputStream</code> is created, the line
number counter is set to zero. As bytes from the stream are read or skipped, the
counter is incremented whenever a line terminator (<code>\n</code>, <code>\r</code>, or <code>\r\n</code>) is encountered.
Such line terminators are also converted to a single <code>'\n'</code> character. The
method <code>getLineNumber</code> returns the current value of the counter, and the method
<code>setLineNumber</code> sets the counter to a given integer value. If the contained input
stream supports the <code>mark</code> operation, then so does the <code>LineNumberInputStream</code>;
the <code>mark</code> operation remembers the line number counter and the <code>reset</code> operation
sets the counter to the value remembered by the <code>mark</code> operation.
<p><pre><a name="29102"></a>public class <code><b>LineNumberInputStream</b></code> extends FilterInputStream {
<a name="29103"></a> public <code><b>LineNumberInputStream</b></code>(InputStream in);
<a name="29104"></a> public int <code><b>read</b></code>() throws IOException;
<a name="29105"></a> public int <code><b>read</b></code>(byte[] b)
<a name="32368"></a> throws IOException, NullPointerException;
<a name="29106"></a> public int <code><b>read</b></code>(byte[] b, int off, int len)<br>
throws IOException, NullPointerException,
<a name="32369"></a> IndexOutOfBoundsException;
<a name="29107"></a> public long <code><b>skip</b></code>(long n) throws IOException;
<a name="29108"></a> public int <code><b>available</b></code>() throws IOException;
<a name="29109"></a> public void <code><b>mark</b></code>(int readlimit);
<a name="29110"></a> public void <code><b>reset</b></code>() throws IOException;
<a name="29111"></a> public int <code><b>getLineNumber</b></code>();
<a name="29112"></a> public void <code><b>setLineNumber</b></code>(int lineNumber);
<a name="29113"></a>}
</pre><a name="29114"></a>
<p><font size=+1><strong>22.12.1 </strong> <code>public <code><b>LineNumberInputStream</b></code>(InputStream in)</code></font>
<p>
<a name="29115"></a>
This constructor initializes a newly created <code>LineNumberInputStream</code> by saving
its argument, the input stream <code>in</code>, for later use.
<p><a name="29119"></a>
<p><font size=+1><strong>22.12.2 </strong> <code>public int <code><b>read</b></code>() throws IOException</code></font>
<p>
<a name="29123"></a>
See the general contract of the <code>read</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28142">(§22.3.1)</a>.
<p><a name="29124"></a>
As bytes are read from the contained input stream, line terminators are recognized and counted. For each line terminator recognized in the contained input stream, a single character <code>'\n'</code> is returned.<p>
<a name="29128"></a>
Overrides the <code>read</code> method of <code>FilterInputStream</code> <a href="javaio.doc7.html#28556">(§22.9.3)</a>.<p>
<a name="29129"></a>
<p><font size=+1><strong>22.12.3 </strong> <code>public int <code><b>read</b></code>(byte[] b)<br>throws IOException, NullPointerException</code></font>
<p>
<a name="29133"></a>
See the general contract of the <code>read</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28150">(§22.3.2)</a>.
<p><a name="29134"></a>
As bytes are read from the contained input stream, line terminators are recognized and counted. For each line terminator recognized in the contained input stream, a single character <code>'\n'</code> is returned.<p>
<a name="29138"></a>
Overrides the <code>read</code> method of <code>FilterInputStream</code> <a href="javaio.doc7.html#28563">(§22.9.4)</a>.<p>
<a name="29139"></a>
<p><font size=+1><strong>22.12.4 </strong> <code>public int <code><b>read</b></code>(byte[] b, int off, int len)<br>throws IOException, NullPointerException,      IndexOutOfBoundsException</code></font>
<p>
<a name="29143"></a>
See the general contract of the <code>read</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28164">(§22.3.3)</a>.
<p><a name="29144"></a>
As bytes are read from the contained input stream, line terminators are recognized and counted. For each line terminator recognized in the contained input stream, a single character <code>'\n'</code> is returned.<p>
<a name="29148"></a>
Overrides the <code>read</code> method of <code>FilterInputStream</code> <a href="javaio.doc18.html#24613">(§22.9.5)</a>.<p>
<a name="29149"></a>
<p><font size=+1><strong>22.12.5 </strong> <code>public long <code><b>skip</b></code>(long n) throws IOException</code></font>
<p>
<a name="29153"></a>
See the general contract of the <code>skip</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28178">(§22.3.4)</a>.
<p><a name="29154"></a>
As bytes are read from the contained input stream, line terminators are recognized and counted. Each line terminator recognized in the contained input stream is considered to be a single byte skipped, even if it is the sequence <code>\r\n</code>.<p>
<a name="29158"></a>
Overrides the <code>skip</code> method of <code>FilterInputStream</code> <a href="javaio.doc7.html#28577">(§22.9.6)</a>.<p>
<a name="29159"></a>
<p><font size=+1><strong>22.12.6 </strong> <code>public int <code><b>available</b></code>() throws IOException</code></font>
<p>
<a name="29163"></a>
See the general contract of the <code>available</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28182">(§22.3.5)</a>.
<p><a name="29164"></a>
Note that if the contained input stream is able to supply <i>k</i> input characters without blocking, the <code>LineNumberInputStream</code> can guarantee only to provide <img src="javaio.doc.anc.gif"> characters without blocking, because the <i>k</i> characters from the contained input stream might consist of <img src="javaio.doc.anc1.gif"> <code>\r\n</code> pairs, which will be converted to just <img src="javaio.doc.anc2.gif"> <code>'\n'</code> characters.<p>
<a name="29177"></a>
Overrides the <code>available</code> method of <code>FilterInputStream</code> <a href="javaio.doc7.html#28584">(§22.9.7)</a>.<p>
<a name="29178"></a>
<p><font size=+1><strong>22.12.7 </strong> <code>public void <code><b>mark</b></code>(int readlimit)</code></font>
<p>
<a name="29182"></a>
See the general contract of the <code>mark</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28192">(§22.3.7)</a>.
<p><a name="29183"></a>
Marking a point in the input stream remembers the current line number as it would be returned by <code>getLineNumber</code> <a href="javaio.doc10.html#29202">(§22.12.9)</a>.<p>
<a name="29190"></a>
Overrides the <code>mark</code> method of <code>FilterInputStream</code> <a href="javaio.doc7.html#28598">(§22.9.9)</a>.<p>
<a name="29191"></a>
<p><font size=+1><strong>22.12.8 </strong> <code>public void <code><b>reset</b></code>() throws IOException</code></font>
<p>
<a name="29195"></a>
See the general contract of the <code>reset</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28197">(§22.3.8)</a>.
<p><a name="29196"></a>
Resetting the input stream to a previous point also resets the line number to the value it had at the marked point.<p>
<a name="29200"></a>
Overrides the <code>reset</code> method of <code>FilterInputStream</code> <a href="javaio.doc12.html#31849">(§22.9.10)</a>.<p>
<a name="29202"></a>
<p><font size=+1><strong>22.12.9 </strong> <code>public int <code><b>getLineNumber</b></code>()</code></font>
<p>
<a name="29203"></a>
The current line number is returned. This quantity depends on <i>k</i>, the number of
line terminators encountered since the most recent occurrence of one of the following
three kinds of events:
<p><ul><a name="29204"></a>
<li>If a call to the <code>setLineNumber</code> method was most recent, let <i>n</i> be the argument that was given to <code>setLineNumber</code>; then the current line number is <img src="javaio.doc.anc3.gif">.
<a name="29208"></a>
<li>If a call to the <code>reset</code> method was most recent, let <i>m</i> be the line number that had been remembered by <code>mark</code>; then the current line number is <img src="javaio.doc.anc4.gif">.
<a name="29212"></a>
<li>If creation of the <code>LineNumberInputStream</code> was most recent (that is, neither of the other kinds of event have occurred), then the current line number is <i>k</i>.
</ul><a name="32641"></a>
These rules imply that the current line number is <code>0</code> as the characters of the first
line are read, and becomes <code>1</code> after the line terminator for the first line has been
read.
<p><a name="29213"></a>
<p><font size=+1><strong>22.12.10 </strong> <code>public void <code><b>setLineNumber</b></code>(int lineNumber)</code></font>
<p>
<a name="29214"></a>
The current line number is set equal to the argument.
<p>
<hr>
<!-- This inserts footnotes--><p>
<a href="index.html">Contents</a> | <a href="javaio.doc9.html">Prev</a> | <a href="javaio.doc11.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<p>
<font size=-1>Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)<br>
<i><a href="jcopyright.doc.html">Copyright © 1996 Sun Microsystems, Inc.</a>
All rights reserved</i>
<br>
Please send any comments or corrections to <a href="mailto:doug.kramer@sun.com">doug.kramer@sun.com</a>
</font>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -