www.lyjust.netJava.io.StreamTokenizer.resetSyntax()方法实例

CoffeeScript ww9992019com 发表于 5 天前

www.lyjust.net开户请拨18288069766密室大逃脱佛山南海区ziULTw<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="3231">
                <input type="hidden" name="catid" id="catid" value="0">

                <p>

<strong>java.io.StreamTokenizer.resetSyntax() </strong>让所有的字符都是方法重置此标记生成器的语法表为“ordinary”。请参阅上一个字符为普通详细信息,此时要用ordinaryChar方法。</p>

<h2>

声明</h2>

<p>

以下是java.io.StreamTokenizer.resetSyntax()方法的声明</p>

<pre class="prettyprint prettyprinted">

<span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> resetSyntax</span><span class="pun">()</span></pre>

<h2>

参数</h2>

<ul>

<li>

    <p>

        NA</p>

</li>

</ul>

<h2>

返回值</h2>

<p>

这个方法没有返回值</p>

<h2>

异常</h2>

<ul>

<li>

    <p>

        NA</p>

</li>

</ul>

<h2>

例子</h2>

<p>

下面的示例演示java.io.StreamTokenizer.resetSyntax()方法的用法。</p>

<pre class="prettyprint prettyprinted">

<span class="kwd">package</span><span class="pln"> com</span><span class="pun">.</span><span class="pln">yiibai</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">io</span><span class="pun">.*;</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">StreamTokenizerDemo</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> main</span><span class="pun">(</span><span class="typ">String</span><span class="pun">[]</span><span class="pln"> args</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

  </span><span class="typ">String</span><span class="pln"> text </span><span class="pun">=</span><span class="pln"> </span><span class="str">"Hello. This is a text

that will be split "</span><span class="pln">

          </span><span class="pun">+</span><span class="pln"> </span><span class="str">"into tokens. 1+1=2"</span><span class="pun">;</span><span class="pln">

  </span><span class="kwd">try</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

     </span><span class="com">// create a new file with an ObjectOutputStream</span><span class="pln">

     </span><span class="typ">FileOutputStream</span><span class="pln"> </span><span class="kwd">out</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">FileOutputStream</span><span class="pun">(</span><span class="str">"test.txt"</span><span class="pun">);</span><span class="pln">

     </span><span class="typ">ObjectOutputStream</span><span class="pln"> oout </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">ObjectOutputStream</span><span class="pun">(</span><span class="kwd">out</span><span class="pun">);</span><span class="pln">



     </span><span class="com">// write something in the file</span><span class="pln">

     oout</span><span class="pun">.</span><span class="pln">writeUTF</span><span class="pun">(</span><span class="pln">text</span><span class="pun">);</span><span class="pln">

     oout</span><span class="pun">.</span><span class="pln">flush</span><span class="pun">();</span><span class="pln">



     </span><span class="com">// create an ObjectInputStream for the file we created before</span><span class="pln">

     </span><span class="typ">ObjectInputStream</span><span class="pln"> ois </span><span class="pun">=</span><span class="pln">

             </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">ObjectInputStream</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">FileInputStream</span><span class="pun">(</span><span class="str">"test.txt"</span><span class="pun">));</span><span class="pln">



     </span><span class="com">// create a new tokenizer</span><span class="pln">

     </span><span class="typ">Reader</span><span class="pln"> r </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">BufferedReader</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">InputStreamReader</span><span class="pun">(</span><span class="pln">ois</span><span class="pun">));</span><span class="pln">

     </span><span class="typ">StreamTokenizer</span><span class="pln"> st </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">StreamTokenizer</span><span class="pun">(</span><span class="pln">r</span><span class="pun">);</span><span class="pln">



     </span><span class="com">// print the stream tokens</span><span class="pln">

     </span><span class="kwd">boolean</span><span class="pln"> eof </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">

     </span><span class="kwd">do</span><span class="pln"> </span><span class="pun">{</span><span class="pln">



        </span><span class="kwd">int</span><span class="pln"> token </span><span class="pun">=</span><span class="pln"> st</span><span class="pun">.</span><span class="pln">nextToken</span><span class="pun">();</span><span class="pln">



        </span><span class="kwd">switch</span><span class="pln"> </span><span class="pun">(</span><span class="pln">token</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

           </span><span class="kwd">case</span><span class="pln"> </span><span class="typ">StreamTokenizer</span><span class="pun">.</span><span class="pln">TT_EOF</span><span class="pun">:</span><span class="pln">

              </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"End of File encountered."</span><span class="pun">);</span><span class="pln">

              eof </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln">

              </span><span class="kwd">break</span><span class="pun">;</span><span class="pln">

           </span><span class="kwd">case</span><span class="pln"> </span><span class="typ">StreamTokenizer</span><span class="pun">.</span><span class="pln">TT_EOL</span><span class="pun">:</span><span class="pln">

              </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"End of Line encountered."</span><span class="pun">);</span><span class="pln">

              </span><span class="kwd">break</span><span class="pun">;</span><span class="pln">

           </span><span class="kwd">case</span><span class="pln"> </span><span class="typ">StreamTokenizer</span><span class="pun">.</span><span class="pln">TT_WORD</span><span class="pun">:</span><span class="pln">

              </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Word: "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> st</span><span class="pun">.</span><span class="pln">sval</span><span class="pun">);</span><span class="pln">

              </span><span class="kwd">break</span><span class="pun">;</span><span class="pln">

           </span><span class="kwd">case</span><span class="pln"> </span><span class="typ">StreamTokenizer</span><span class="pun">.</span><span class="pln">TT_NUMBER</span><span class="pun">:</span><span class="pln">

              </span><span class="com">// when a number is met, reset syntax</span><span class="pln">

              st</span><span class="pun">.</span><span class="pln">resetSyntax</span><span class="pun">();</span><span class="pln">

              </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Number: "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> st</span><span class="pun">.</span><span class="pln">nval</span><span class="pun">);</span><span class="pln">

              </span><span class="kwd">break</span><span class="pun">;</span><span class="pln">

           </span><span class="kwd">default</span><span class="pun">:</span><span class="pln">



              </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">((</span><span class="kwd">char</span><span class="pun">)</span><span class="pln"> token </span><span class="pun">+</span><span class="pln"> </span><span class="str">" encountered."</span><span class="pun">);</span><span class="pln">

              </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">token </span><span class="pun">==</span><span class="pln"> </span><span class="str">'!'</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

                 eof </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln">

              </span><span class="pun">}</span><span class="pln">

        </span><span class="pun">}</span><span class="pln">



     </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">while</span><span class="pln"> </span><span class="pun">(!</span><span class="pln">eof</span><span class="pun">);</span><span class="pln">





  </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">catch</span><span class="pln"> </span><span class="pun">(</span><span class="typ">Exception</span><span class="pln"> ex</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

     ex</span><span class="pun">.</span><span class="pln">printStackTrace</span><span class="pun">();</span><span class="pln">

  </span><span class="pun">}</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span></pre>

<p>

让我们编译和运行上面的程序,这将产生以下结果:</p>

<pre class="prettyprint prettyprinted">

<span class="typ">Word</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Hello</span><span class="pun">.</span><span class="pln">

</span><span class="typ">Word</span><span class="pun">:</span><span class="pln"> </span><span class="typ">This</span><span class="pln">

</span><span class="typ">Word</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">is</span><span class="pln">

</span><span class="typ">Word</span><span class="pun">:</span><span class="pln"> a

</span><span class="typ">Word</span><span class="pun">:</span><span class="pln"> text

</span><span class="typ">Word</span><span class="pun">:</span><span class="pln"> that

</span><span class="typ">Word</span><span class="pun">:</span><span class="pln"> will

</span><span class="typ">Word</span><span class="pun">:</span><span class="pln"> be

</span><span class="typ">Word</span><span class="pun">:</span><span class="pln"> split

</span><span class="typ">Word</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">into</span><span class="pln">

</span><span class="typ">Word</span><span class="pun">:</span><span class="pln"> tokens</span><span class="pun">.</span><span class="pln">

</span><span class="typ">Number</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1.0</span><span class="pln">

</span><span class="pun">+</span><span class="pln"> encountered</span><span class="pun">.</span><span class="pln">

</span><span class="lit">1</span><span class="pln"> encountered</span><span class="pun">.</span><span class="pln">

</span><span class="pun">=</span><span class="pln"> encountered</span><span class="pun">.</span><span class="pln">

</span><span class="lit">2</span><span class="pln"> encountered</span><span class="pun">.</span><span class="pln">

</span><span class="typ">End</span><span class="pln"> of </span><span class="typ">File</span><span class="pln"> encountered</span><span class="pun">.</span></pre>

                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="Java.io.StreamTokenizer.quoteChar()方法实例"><i class="layui-icon"></i>
                            Java.io.StreamTokenizer.quoteChar()方法实例</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:Java.io.StreamTokenizer.slashSlashComments()方法实例"><i class="layui-icon"></i>
                            Java.io.StreamTokenizer.slashSlashComments()方法实例</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复