java.io.CharArrayReader.read(char[]b,intoff,intlen)方法实例

算法 ww9992019com 发表于 1 周前

<div style="width:100%;float:left;" class="article-content">

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

                <p>

<strong>java.io.CharArrayReader.read(char[] b, int off, int len) </strong>方法读取字符到指定数组的一部分。</p>

<h2>

声明</h2>

<p>

以下是java.io.CharArrayReader.read(char[] b, int off, int len) 方法的声明:</p>

<pre class="prettyprint prettyprinted">

<span class="kwd">public</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> read</span><span class="pun">(</span><span class="kwd">char</span><span class="pun">[]</span><span class="pln"> b</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> off</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> len</span><span class="pun">)</span></pre>

<h2>

参数</h2>

<ul>

<li>

    <p>

        b -- 目标数组。</p>

</li>

<li>

    <p>

        off -- 偏移量开始存储字符。</p>

</li>

<li>

    <p>

        len -- 要读取的字符数。</p>

</li>

</ul>

<h2>

返回值</h2>

<p>

实际读取字符的数目,-1为流的末尾。</p>

<h2>

异常</h2>

<ul>

<li>

    <p>

        IOException -- 如果发生任何I/ O错误。</p>

</li>

</ul>

<h2>

例子</h2>

<p>

下面的例子显示java.io.CharArrayReader.read(char[] b, int off, int len) 方法。</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="typ">CharArrayReader</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="typ">IOException</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">CharArrayReaderDemo</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">CharArrayReader</span><span class="pln"> car </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">;</span><span class="pln">

  </span><span class="kwd">char</span><span class="pun">[]</span><span class="pln"> ch </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="str">'H'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'E'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'L'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'L'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'O'</span><span class="pun">};</span><span class="pln">



  </span><span class="kwd">char</span><span class="pun">[]</span><span class="pln"> d </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="kwd">char</span><span class="pun">[</span><span class="lit">5</span><span class="pun">];</span><span class="pln">

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



     </span><span class="com">// create new character array reader</span><span class="pln">

     car </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">CharArrayReader</span><span class="pun">(</span><span class="pln">ch</span><span class="pun">);</span><span class="pln">



     </span><span class="com">// read character to the destination buffer</span><span class="pln">

     car</span><span class="pun">.</span><span class="pln">read</span><span class="pun">(</span><span class="pln">d</span><span class="pun">,</span><span class="pln"> </span><span class="lit">3</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span class="pun">);</span><span class="pln">



     </span><span class="com">// for every character in the buffer</span><span class="pln">

     </span><span class="kwd">for</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">char</span><span class="pln"> c </span><span class="pun">:</span><span class="pln"> d</span><span class="pun">)</span><span class="pln">

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

        </span><span class="kwd">int</span><span class="pln"> i </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">int</span><span class="pun">)</span><span class="pln">c</span><span class="pun">;</span><span class="pln">

        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">i</span><span class="pun">==</span><span class="lit">0</span><span class="pun">)</span><span class="pln">

        </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">"0"</span><span class="pun">);</span><span class="pln">

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

        </span><span class="kwd">else</span><span class="pln">

        </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="pln">c</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="kwd">catch</span><span class="pun">(</span><span class="typ">IOException</span><span class="pln"> e</span><span class="pun">){</span><span class="pln">



     </span><span class="com">// if I/O error occurs</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="kwd">print</span><span class="pun">(</span><span class="str">"Stream is already closed"</span><span class="pun">);</span><span class="pln">

  </span><span class="pun">}</span><span class="kwd">finally</span><span class="pun">{</span><span class="pln">



     </span><span class="com">// releases any system resources associated with the stream</span><span class="pln">

     </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">car</span><span class="pun">!=</span><span class="kwd">null</span><span class="pun">)</span><span class="pln">

        car</span><span class="pun">.</span><span class="pln">close</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></pre>

<p>

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

<pre class="prettyprint prettyprinted">

<span class="lit">0</span><span class="pln">

</span><span class="lit">0</span><span class="pln">

</span><span class="lit">0</span><span class="pln">

H

E</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.CharArrayReader.read()方法实例"><i class="layui-icon"></i>
                            Java.io.CharArrayReader.read()方法实例</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:Java.io.CharArrayReader.ready()方法实例"><i class="layui-icon"></i>
                            Java.io.CharArrayReader.ready()方法实例</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>
暂无回复,说出你的观点吧
登录后即可参与回复