⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javaio.doc12.html

📁 java语言规范
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<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.doc11.html">Prev</a> | <a href="javaio.doc13.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
 
<a name="29287"></a>
<center><h1>22.14  The Class  <code>java.io.StreamTokenizer</code></h1></center>
<a name="29288"></a>
A <code>StreamTokenizer</code> takes an input stream and parses it into "tokens," allowing 
the tokens to be read one at a time. The parsing process is controlled by a table 
and a number of flags that can be set to various states, allowing recognition of 
identifiers, numbers, quoted strings, and comments in a standard style.
<p><pre><a name="29289"></a>public class <code><b>StreamTokenizer</b></code> {
<a name="29290"></a>	public static final int <code><b>TT_EOF</b></code> = -1;
<a name="29291"></a>	public static final int <code><b>TT_EOL</b></code> = '\n';
<a name="29292"></a>	public static final int <code><b>TT_NUMBER</b></code> = -2;
<a name="29293"></a>	public static final int <code><b>TT_WORD</b></code> = -3;
<a name="29294"></a>	public int <code><b>ttype</b></code>;
<a name="29295"></a>	public String <code><b>sval</b></code>;
<a name="29296"></a>	public double <code><b>nval</b></code>;
<a name="29297"></a>	public <code><b>StreamTokenizer</b></code>(InputStream in);
<a name="29298"></a>	public void <code><b>resetSyntax</b></code>();
<a name="29299"></a>	public void <code><b>wordChars</b></code>(int low, int hi);
<a name="29300"></a>	public void <code><b>whitespaceChars</b></code>(int low, int hi);
<a name="29301"></a>	public void <code><b>ordinaryChars</b></code>(int low, int hi);
<a name="29302"></a>	public void <code><b>ordinaryChar</b></code>(int ch);
<a name="29303"></a>	public void <code><b>commentChar</b></code>(int ch);
<a name="29304"></a>	public void <code><b>quoteChar</b></code>(int ch);
<a name="29305"></a>	public void <code><b>parseNumbers</b></code>();
<a name="29306"></a>	public void <code><b>eolIsSignificant</b></code>(boolean flag);
<a name="29307"></a>	public void <code><b>slashStarComments</b></code>(boolean flag);
<a name="29308"></a>	public void <code><b>slashSlashComments</b></code>(boolean flag);
<a name="29309"></a>	public void <code><b>lowerCaseMode</b></code>(boolean flag);
<a name="29310"></a>	public int <code><b>nextToken</b></code>() throws IOException;
<a name="29311"></a>	public void <code><b>pushBack</b></code>();
<a name="29312"></a>	public int <code><b>lineno</b></code>();
<a name="29313"></a>	public String <code><b>toString</b></code>();
<a name="29314"></a>}
</pre><a name="29315"></a>
Each byte read from the input stream is regarded as a character in the range <code>'\u0000'</code> through <code>'\u00FF'</code>. The character value is used to look up five possible attributes of the character: whitespace, alphabetic, numeric, string quote, and comment character (a character may have more than one of these attributes, or none at all). In addition, there are three flags controlling whether line terminators are to be recognized as tokens, whether Java-style end-of-line comments that start with <code>//</code> should be recognized and skipped, and whether Java-style "traditional" comments delimited by <code>/*</code> and <code>*/</code> should be recognized and skipped. One more flag controls whether all the characters of identifiers are converted to lowercase.<p>
<a name="31827"></a>
Here is a simple example of the use of a <code>StreamTokenizer</code>. The following code merely reads all the tokens in the standard input stream and prints an identification of each one. Changes in the line number are also noted.<p>
<pre><a name="31833"></a>import java.io.StreamTokenizer;
<a name="31834"></a>import java.io.IOException;
</pre><pre><a name="31836"></a>
class Tok {
<a name="31837"></a>	public static void main(String[] args) {
<a name="31838"></a>		StreamTokenizer st = new StreamTokenizer(System.in);
<a name="31839"></a>		st.ordinaryChar('/');
<a name="31840"></a>		int lineNum = -1;
<a name="31841"></a>		try {
<a name="31842"></a>			for (int tokenType = st.nextToken();
<a name="31843"></a>					tokenType != StreamTokenizer.TT_EOF;
<a name="31844"></a>					tokenType = st.nextToken()) {
<a name="31845"></a>				int newLineNum = st.lineno();
<a name="31846"></a>				if (newLineNum != lineNum) {
<a name="31847"></a>					System.out.println("[line " + newLineNum
<a name="31871"></a>											+ "]");
<a name="31848"></a>					lineNum = newLineNum;
<a name="31849"></a>				}
<a name="31850"></a>				switch(tokenType) {
<a name="31851"></a>				case StreamTokenizer.TT_NUMBER:
<a name="31852"></a>					System.out.println("the number " + st.nval);
<a name="31853"></a>					break;
<a name="31854"></a>				case StreamTokenizer.TT_WORD:
<a name="31855"></a>					System.out.println("identifier " + st.sval);
<a name="31856"></a>					break;
<a name="31857"></a>				default:
<a name="31858"></a>					System.out.println("  operator "
<a name="31870"></a>											+ (char)tokenType);
<a name="31859"></a>				}
<a name="31860"></a>			}
<a name="31861"></a>		} catch (IOException e) {
<a name="31868"></a>			System.out.println("I/O failure");
<a name="31869"></a>		}
<a name="31862"></a>	}
<a name="31863"></a>}
</pre><a name="31828"></a>
If the input stream contains this data:
<p><pre><a name="31878"></a>
10 LET A = 4.5
<a name="31879"></a>20 LET B = A*A
<a name="31880"></a>30 PRINT A, B
</pre><a name="31874"></a>
then the resulting output is:
<p><pre><a name="31885"></a>
[line 1]
<a name="31886"></a>the number 10.0
<a name="31887"></a>identifier LET
<a name="31888"></a>identifier A
<a name="31889"></a>  operator =
<a name="31890"></a>the number 4.5
<a name="31891"></a>[line 2]
<a name="31892"></a>the number 20.0
<a name="31893"></a>identifier LET
<a name="31894"></a>identifier B
<a name="31895"></a>  operator =
<a name="31896"></a>identifier A
<a name="31897"></a>  operator *
<a name="31898"></a>identifier A
<a name="31899"></a>[line 3]
<a name="31900"></a>the number 30.0
<a name="31901"></a>identifier PRINT
<a name="31902"></a>identifier A
<a name="31903"></a>  operator ,
<a name="31904"></a>identifier B
</pre><a name="29316"></a>
<p><font size=+1><strong>22.14.1   </strong> <code>public static final int <code><b>TT_EOF</b></code> = -1;</code></font>
<p>
<a name="29317"></a>
A constant that indicates end of file was reached.
<p><a name="29318"></a>
<p><font size=+1><strong>22.14.2   </strong> <code>public static final int <code><b>TT_EOL</b></code> = '\n';</code></font>
<p>
<a name="29319"></a>
A constant that indicates that a line terminator was recognized.
<p><a name="29320"></a>
<p><font size=+1><strong>22.14.3   </strong> <code>public static final int <code><b>TT_NUMBER</b></code> = -2;</code></font>
<p>
<a name="29321"></a>
A constant that indicates that a number was recognized.
<p><a name="29322"></a>
<p><font size=+1><strong>22.14.4   </strong> <code>public static final int <code><b>TT_WORD</b></code> = -3;</code></font>
<p>
<a name="29323"></a>
A constant that indicates that a word (identifier) was recognized.
<p><a name="29324"></a>
<p><font size=+1><strong>22.14.5   </strong> <code>public int <code><b>ttype</b></code>;</code></font>
<p>
<a name="29325"></a>
The type of the token that was last recognized by this <code>StreamTokenizer</code>. This 
will be <code>TT_EOF</code>, <code>TT_EOL</code>, <code>TT_NUMBER</code>, <code>TT_WORD</code>, or a nonnegative byte value that 
was the first byte of the token (for example, if the token is a string token, then 
<code>ttype</code> has the quote character that started the string).
<p><a name="29326"></a>
<p><font size=+1><strong>22.14.6   </strong> <code>public String <code><b>sval</b></code>;</code></font>
<p>
<a name="29327"></a>
If the value of <code>ttype</code> is <code>TT_WORD</code> or a string quote character, then the value of 
<code>sval</code> is a <code>String</code> that contains the characters of the identifier or of the string 
(without the delimiting string quotes). For all other types of tokens recognized, 
the value of <code>sval</code> is <code>null</code>.
<p><a name="29328"></a>
<p><font size=+1><strong>22.14.7   </strong> <code>public double <code><b>nval</b></code>;</code></font>
<p>
<a name="29329"></a>
If the value of <code>ttype</code> is <code>TT_NUMBER</code>, then the value of <code>nval</code> is the numerical value 
of the number.
<p><a name="29330"></a>
<p><font size=+1><strong>22.14.8   </strong> <code>public <code><b>StreamTokenizer</b></code>(InputStream in)</code></font>
<p>
<a name="29331"></a>
This constructor initializes a newly created <code>StreamTokenizer</code> by saving its argument,
the input stream <code>in</code>, for later use. The <code>StreamTokenizer</code> is also initialized 
to the following default state:
<p><ul><a name="29332"></a>
<li>All byte values <code>'A'</code> through <code>'Z'</code>, <code>'a'</code> through <code>'z'</code>, and <code>0xA0</code> through <code>0xFF</code> are considered to be alphabetic.
<a name="29333"></a>
<li>All byte values <code>0x00</code> through <code>0x20</code> are considered to be whitespace.
<a name="29334"></a>
<li><code>'/'</code> is a comment character.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -