java 流

来源:互联网 发布:数据挖掘工具分类 编辑:程序博客网 时间:2024/05/01 22:49
<div id="article_details" class="details">    <div class="article_title">            <span class="ico ico_type_Repost"></span>    <h1>        <span class="link_title"><a href="/liuxiaogangqq/article/details/25892667">        java总结输入流输出流                    </a></span>    </h1></div>           <div class="article_manage clearfix">        <div class="article_l">            <span class="link_categories">            标签:              <a href="http://www.csdn.net/tag/io%e6%b5%81" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">io流</a><a href="http://www.csdn.net/tag/buffer" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">buffer</a><a href="http://www.csdn.net/tag/%e5%ad%97%e7%ac%a6%e6%b5%81" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">字符流</a><a href="http://www.csdn.net/tag/%e8%ae%a1%e7%ae%97%e6%9c%ba" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">计算机</a><a href="http://www.csdn.net/tag/%e7%bc%96%e7%a8%8b" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">编程</a>            </span>        </div>        <div class="article_r">            <span class="link_postdate">2014-05-15 15:14</span>            <span class="link_view" title="阅读次数">16528人阅读</span>            <span class="link_comments" title="评论次数"> <a href="#comments" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])">评论</a>(10)</span>            <span class="link_collect tracking-ad" data-mod="popu_171"> <a href="javascript:void(0);" onclick="javascript:collectArticle('java总结输入流输出流','25892667');return false;" title="收藏" target="_blank">收藏</a></span>             <span class="link_report"> <a href="#report" onclick="javascript:report(25892667,2);return false;" title="举报">举报</a></span>        </div>    </div>    <div class="embody" style="display:none" id="embody">        <span class="embody_t">本文章已收录于:</span>        <div class="embody_c" id="lib" value="{"err":0,"msg":"ok","data":[]}"></div>    </div>    <style type="text/css">                    .embody{                padding:10px 10px 10px;                margin:0 -20px;                border-bottom:solid 1px #ededed;                            }            .embody_b{                margin:0 ;                padding:10px 0;            }            .embody .embody_t,.embody .embody_c{                display: inline-block;                margin-right:10px;            }            .embody_t{                font-size: 12px;                color:#999;            }            .embody_c{                font-size: 12px;            }            .embody_c img,.embody_c em{                display: inline-block;                vertical-align: middle;                           }             .embody_c img{                               width:30px;                height:30px;            }            .embody_c em{                margin: 0 20px 0 10px;                color:#333;                font-style: normal;            }    </style>    <script type="text/javascript">        $(function () {            try            {                var lib = eval("("+$("#lib").attr("value")+")");                var html = "";                if (lib.err == 0) {                    $.each(lib.data, function (i) {                        var obj = lib.data[i];                        //html += '<img src="' + obj.logo + '"/>' + obj.name + "  ";                        html += ' <a href="' + obj.url + '" target="_blank">';                        html += ' <img src="' + obj.logo + '">';                        html += ' <em><b>' + obj.name + '</b></em>';                        html += ' </a>';                    });                    if (html != "") {                        setTimeout(function () {                            $("#lib").html(html);                                                  $("#embody").show();                        }, 100);                    }                }                  } catch (err)            { }                    });    </script>      <div class="category clearfix">        <div class="category_l">           <img src="http://static.blog.csdn.net/images/category_icon.jpg">            <span>分类:</span>        </div>        <div class="category_r">                    <label onclick="GetCategoryArticles('1160837','liuxiaogangqq','top','25892667');">                        <span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">java类<em>(50)</em></span>                      <img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display:inline;">                      <img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display:none;">                        <div class="subItem">                            <div class="subItem_t"><a href="http://blog.csdn.net/liuxiaogangqq/article/category/1160837" target="_blank">作者同类文章</a><i class="J_close">X</i></div>                            <ul class="subItem_l" id="top_1160837">                                                        </ul>                        </div>                    </label>                            </div>    </div>    <script type="text/javascript" src="http://static.blog.csdn.net/scripts/category.js"></script>             <div style="clear:both"></div><div style="border:solid 1px #ccc; background:#eee; float:left; min-width:200px;padding:4px 10px;"><p style="text-align:right;margin:0;"><span style="float:left;">目录<a href="#" title="系统根据文章中H1到H6标签自动生成文章目录">(?)</a></span><a href="#" onclick="javascript:return openct(this);" title="展开">[+]</a></p><ol style="display:none;margin-left:14px;padding-left:14px;line-height:160%;"><li><a href="#t0">1什么是IO</a></li><li><a href="#t1">2数据流的基本概念</a></li><ol><li><a href="#t2">1 数据流</a></li><li><a href="#t3">2 输入流Input  Stream</a></li><li><a href="#t4"> 3 输出流</a></li><li><a href="#t5">4 数据流分类</a></li></ol><li><a href="#t6">3 标准IO</a></li><ol><li><a href="#t7">1 命令行参数</a></li><li><a href="#t8">2 标准输入输出数据流</a></li></ol><li><a href="#t9">4javaIO层次体系结构</a></li><li><a href="#t10">5 非流式文件类--File类</a></li><li><a href="#t11">6 JavaIO流类库</a></li><ol><li><a href="#t12">1 io流的四个基本类</a></li><li><a href="#t13">1 io流的具体分类</a></li></ol><li><a href="#t14">7 字节流InputStreamOutputStream</a></li><ol><li><a href="#t15"> 1 InputStream抽象类 </a></li><li><a href="#t16"> 2OutputStream抽象类 </a></li><li><a href="#t17">3 文件输入流 FileInputStream类</a></li><li><a href="#t18">4文件输出流FileOutputStream类</a></li><li><a href="#t19">FileInputStream流和FileOutputStream的应用</a></li><li><a href="#t20">缓冲输入输出流 BufferedInputStream BufferedOutputStream</a></li></ol><li><a href="#t21">8 字符流WriterReader</a></li><ol><li><a href="#t22">1 Reader抽象类</a></li><li><a href="#t23">2 Writer抽象类</a></li><li><a href="#t24">3 InputStream与Reader差别 OutputStream与Writer差别</a></li></ol><li><a href="#t25">9 IOException异常类的子类</a></li></ol></div><div style="clear:both"></div><div id="article_content" class="article_content"><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><h2 style="margin:0px; padding:0px"><a name="t0"></a><p class="headline-1 bk-sidecatalog-title" style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:6px; border-bottom-color:rgb(222,223,225); border-bottom-width:1px; border-bottom-style:solid; clear:both"><span style="color:rgb(51,102,153); line-height:36px; font-size:22px">1.什么是IO</span></p></h2></div><p style="color:rgb(51,51,51); font-size:14px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Tahoma; line-height:25px">      <a href="http://lib.csdn.net/base/17" class="replace_word" title="Java EE知识库" target="_blank" style="color:#df3434; font-weight:bold;">Java</a>中I/O操作主要是指使用Java进行输入,输出操作. <span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px">Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列<span style="color:rgb(51,51,51); font-family:Verdana,Arial,Tahoma; font-size:14.399999618530273px; line-height:24.799999237060547px">。</span>Java的I/O流提供了读写数据的标准方法。任何Java中表示数据源的对象都会提供以数据流的方式读写它的数据的方法。  </span></p><p style="color:rgb(51,51,51); font-size:14px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Tahoma; line-height:25px">      <span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px">Java.io是大多数面向数据流的输入/输出类的主要软件包。</span>此外,Java也对块传输提供支持,在核心库 java.nio中采用的便是块IO。</p><p style="color:rgb(51,51,51); font-size:14px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Tahoma; line-height:25px">  流IO的好处是简单易用,缺点是效率较低。块IO效率很高,但编程比较复杂。 <br>    <strong>  Java IO模型  :</strong><br>      Java的IO模型设计非常优秀,它使用Decorator模式,按功能划分Stream,您可以动态装配这些Stream,以便获得您需要的功能。例如,您需要一个具有缓冲的文件输入流,则应当组合使用FileInputStream和BufferedInputStream。 </p><p style="color:rgb(51,51,51); font-size:14px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Tahoma; line-height:25px"></p><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><h2 style="margin:0px; padding:0px"><a name="t1"></a><a target="_blank" name="t1" style="color:rgb(51,102,153)"></a><p class="headline-1 bk-sidecatalog-title" style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:6px; border-bottom-color:rgb(222,223,225); border-bottom-width:1px; border-bottom-style:solid; clear:both"><span style="color:rgb(51,102,153); line-height:36px; font-size:22px">2.数据流的基本概念</span></p></h2></div><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><p style="color:rgb(51,51,51); font-size:14px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Tahoma; line-height:25px"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="text-indent:-28px">       <span style="font-family:'Times New Roman'"> </span><span style="font-family:SimSun">数据流是一串连续不断的数据的集合,就象水管里的水流,在水管的一端一点一点地供水,而在水管的另一端看到的是一股连续不断的水流。数据写入程序可以是一段、一段地向数据流管道中写入数据,这些数据段会按先后顺序形成一个长的数据流。对数据读取程序来说,看不到数据流在写入时的分段情况,每次可以读取其中的任意长度的数据,但只能先读取前面的数据后,再读取后面的数据。不管写入时是将数据分多次写入,还是作为一个整体一次写入,读取时的效果都是完全一样的。</span><span style="font-family:'Comic Sans MS'"> </span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="text-indent:-28px"></span>      “流是磁盘或其它外围设备中存储的数据的源点或终点。”</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:宋体; line-height:28px">    在电脑上的数据有三种存储方式,一种是外存,一种是内存,一种是缓存。比如电脑上的硬盘,磁盘,U盘等都是外存,在电脑上有内存条,缓存是在CPU里面的。外存的存储量最大,其次是内存,最后是缓存,但是外存的数据的读取最慢,其次是内存,缓存最快。这里总结从外存读取数据到内存以及将数据从内存写到外存中。对于内存和外存的理解,我们可以简单的理解为容器,即外存是一个容器,内存又是另外一个容器。那又怎样把放在外存这个容器内的数据读取到内存这个容器以及怎么把内存这个容器里的数据存到外存中呢?</span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:宋体; line-height:28px">     <span style="font-family:Arial,宋体; line-height:24px; background-color:rgb(245,248,253)">在Java类库中,IO部分的内容是很庞大的,因为它涉及的领域很广泛:</span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:宋体; line-height:28px"><span style="font-family:Arial,宋体; line-height:24px; background-color:rgb(245,248,253)"><span style="color:rgb(255,0,0)">         标准输入输出,</span></span></span><span style="font-family:宋体; line-height:28px"><span style="font-family:Arial,宋体; line-height:24px; background-color:rgb(245,248,253)"><span style="color:rgb(255,0,0)">文件的操作,</span></span><span style="color:rgb(255,0,0)"><span style="font-family:Arial"><span style="line-height:24px; background-color:rgb(245,248,253)">网络</span></span><span style="font-family:Arial,宋体; line-height:24px; background-color:rgb(245,248,253)">上的数据流,字符串流,对象流,zip文件流等等,</span></span></span><span style="font-family:宋体; line-height:28px"><span style="color:rgb(255,0,0)"><strong>java中将输入输出抽象称为流</strong></span>,就好像水管,将两个容器连接起来。将数据冲外存中读取到内存中的称为输入流,将数据从内存写入外存中的称为输出流。</span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:宋体; line-height:28px">    <span style="font-family:Arial,宋体; line-height:24px; background-color:rgb(245,248,253)">流是一个很形象的概念,当</span><span style="font-family:Arial"><span style="line-height:24px; background-color:rgb(245,248,253)">程序</span></span><span style="font-family:Arial,宋体; line-height:24px; background-color:rgb(245,248,253)">需要读取数据的时候,就会开启一个通向数据源的流,这个数据源可以是文件,内存,或是网络连接。类似的,当程序需要写入数据的时候,就会开启一个通向目的地的流。</span></span></p><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p>      总结的基本概念如下:</p></div><h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t2"></a><a target="_blank" name="t2" style="color:rgb(51,102,153)"></a><span style="font-family:Arial,宋体"><span style="line-height:24px"><strong>1) 数据流</strong></span>:</span></h3><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体"><span style="line-height:24px">    一组有序,有起点和终点的字节的数据序列。包括输入流和输出流。</span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体"><span style="line-height:24px">  <img src="http://img.my.csdn.net/uploads/201212/14/1355473547_6986.jpg" alt="" style="border:none; max-width:100%"></span></span></p><h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t3"></a><a target="_blank" name="t3" style="color:rgb(51,102,153)"></a><span style="font-family:Arial,宋体"><span style="line-height:24px"><strong>2) 输入流</strong></span>(Input  Stream):</span></h3><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体"><span style="line-height:24px">      程序从输入流读取数据源。数据源包括外界(键盘、文件、网络…),即是将数据源读入到程序的通信通道</span></span></p><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">     <img src="http://my.csdn.net/uploads/201204/01/1333248748_3106.jpg" alt="" style="border:none; max-width:100%"><br></div><h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t4"></a><a target="_blank" name="t4" style="color:rgb(51,102,153)"></a><strong> 3) 输出流</strong>:</h3><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">    程序向输出流写入数据。将程序中的数据输出到外界(显示器、打印机、文件、网络…)的通信通道。</div><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">     <img src="http://my.csdn.net/uploads/201204/01/1333248874_6866.jpg" alt="" style="border:none; max-width:100%; font-family:Verdana,Arial,Tahoma; font-size:14.399999618530273px; line-height:24.799999237060547px">    </p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(255,0,0)">采用数据流的目的就是使得输出输入独立于设备。</span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体; line-height:24px">Input  Stream不关心数据源来自何种设备(键盘,文件,网络)</span><br><span style="font-family:Arial,宋体; line-height:24px">Output  Stream不关心数据的目的是何种设备(键盘,文件,网络)</span></p><h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t5"></a><a target="_blank" name="t5" style="color:rgb(51,102,153)"></a><span style="font-family:Arial,宋体; line-height:24px">4 数据流分类:</span></h3><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体; line-height:24px">流序列中的数据既可以是未经加工的原始二进制数据,也可以是经一定编码处理后符合某种格式规定的特定数据。因此Java中的流分为两种:<br><strong> 1)  字节流:</strong>数据流中最小的数据单元是字节<br><strong> 2)  字符流:</strong>数据流中最小的数据单元是字符, Java中的字符是Unicode编码,一个字符占用两个字节。<br></span></div><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><div style="color:rgb(51,51,51); font-size:14.399999618530273px; font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:24.799999237060547px"><h2 style="margin:0px; padding:0px"><a name="t6"></a><a target="_blank" name="t6" style="color:rgb(51,102,153)"></a><p class="headline-1 bk-sidecatalog-title" style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:6px; border-bottom-color:rgb(222,223,225); border-bottom-width:1px; border-bottom-style:solid; clear:both"><span style="color:rgb(51,102,153); line-height:36px">3. 标准I/O</span></p></h2></div><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(0,0,128)"><span style="font-size:18px"><strong>     </strong></span> J</span>ava程序可通过命令行参数与外界进行简短的信息交换,同时,也规定了与标准输入、输出设备,如键盘、显示器进行信息交换的方式。而通过文件可以与外界进行任意数据形式的信息交换。</p><h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t7"></a><a target="_blank" name="t7" style="color:rgb(51,102,153)"></a><span style="color:rgb(51,51,255)">1. 命令行参数</span></h3><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><div class="dp-highlighter bg_java" style="font-family:Consolas,'Courier New',Courier,mono,serif; border:1px dashed rgb(153,153,153); background-color:rgb(245,245,245); width:700.90625px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important"><ol start="1" class="dp-j" style="margin:0px; padding:5px 0px; border:none; position:relative; list-style-position:initial; color:rgb(92,92,92)"><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">class</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> TestArgs {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">static</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">void</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> main(String[] args) {  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">for</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> (</span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">int</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> i = </span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">; i < args.length; i++) {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            System.out.println(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"args["</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> + i + </span><span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"] is <"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> + args[i] + </span><span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">">"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        }  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    }  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">}  </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-size:18px">运行命令:</span><span style="font-size:18px"><span style="color:rgb(102,102,102)">java Java C VB</span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-size:18px">运行结果:</span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-size:18px"><span style="color:rgb(192,192,192)">args[0] is <Java></span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-size:18px"><span style="color:rgb(192,192,192)">args[1] is <C></span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-size:18px"><span style="color:rgb(192,192,192)">args[2] is <VB></span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial,宋体; line-height:24px"><a name="t8"></a><a target="_blank" name="t8" style="color:rgb(51,102,153)"></a><span style="color:rgb(51,51,255)">2. 标准输入,输出数据流</span></h3><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Simsun; text-indent:28px">java系统自带的标准数据流:java.lang.System:</span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Simsun; text-indent:28px"></span></p><div class="dp-highlighter bg_java" style="font-family:Consolas,'Courier New',Courier,mono,serif; border:1px dashed rgb(153,153,153); background-color:rgb(245,245,245); width:700.90625px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important"><ol start="1" class="dp-j" style="margin:0px; padding:5px 0px; border:none; position:relative; list-style-position:initial; color:rgb(92,92,92)"><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">java.lang.System   </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">final</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">class</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> System  </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">extends</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> Object{   </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">   <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">static</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  PrintStream  err;</span><span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(153,153,153); background-color:inherit">//标准错误流(输出)</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">   <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">static</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  InputStream  in;</span><span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(153,153,153); background-color:inherit">//标准输入(键盘输入流)</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">   <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">static</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  PrintStream  out;</span><span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(153,153,153); background-color:inherit">//标准输出流(显示器输出流)</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">}  </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Simsun; text-indent:28px">注意:<br>(1)System类不能创建对象,只能直接使用它的三个静态成员。<br>(2)每当main方法被执行时,就自动生成上述三个对象。<br><br></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Simsun; text-indent:28px"><span style="font-weight:bold"><span style="color:rgb(51,51,255)">1) 标准输出流 System.out</span></span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Simsun; text-indent:28px"><strong>   System.out向标准输出设备输出数据</strong>,其数据类型为PrintStream。方法:</span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(102,0,51)">      Void print(</span><span style="color:rgb(102,0,51)">参数)</span></div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(102,0,51)">      Void println(</span><span style="color:rgb(102,0,51)">参数)</span></div><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Simsun; text-indent:28px"><span style="font-weight:bold"><span style="color:rgb(51,51,255)">2)标准输入流 System.in</span></span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Simsun; text-indent:28px"><strong>    System.in读取标准输入设备数据</strong>(从标准输入获取数据,一般是键盘),其数 据类型为InputStream。方法:</span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(102,0,51)">        int read()</span><span style="color:rgb(102,0,51)">  </span>//返回ASCII码。若,返回值=-1,说明没有读取到任何字节读取工作结束。</div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(102,0,51)">         int read(byte[] b)</span>//<span style="color:rgb(51,51,204)">读入多个</span>字节到缓冲区b中返回值是读入的字节数</div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">例如:</div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><div class="dp-highlighter bg_java" style="font-family:Consolas,'Courier New',Courier,mono,serif; border:1px dashed rgb(153,153,153); background-color:rgb(245,245,245); font-size:12px; width:700.90625px; overflow:auto; padding-top:1px; margin:18px 0px!important"><ol start="1" class="dp-j" style="margin:0px; padding:5px 0px; border:none; position:relative; list-style-position:initial; color:rgb(92,92,92)"><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">import</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> java.io.*;  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">class</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> StandardInputOutput {  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">static</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">void</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> main(String args[]) {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">int</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> b;  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">try</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            System.out.println(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"please Input:"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">while</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> ((b = System.in.read()) != -</span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">) {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">                System.out.print((<span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">char</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">) b);  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            }  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        } <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">catch</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> (IOException e) {  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            System.out.println(e.toString());  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        }  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    }  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">}  </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div></div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">等待键盘输入,键盘输入什么,就打印出什么:</div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><img src="http://img.my.csdn.net/uploads/201211/28/1354093989_4632.jpg" alt="" style="border:none; max-width:100%"><br></div><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Simsun; text-indent:28px"><strong><span style="color:rgb(51,51,255)">3)标准错误流</span></strong></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Simsun; text-indent:28px"><strong>   System.err输出标准错误</strong>,其数据类型为PrintStream。可查阅API获得详细说明。</span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">    标准输出通过System.out调用println方法输出参数并换行,而print方法输出参数但不换行。println或print方法都通 过重载实现了输出基本数据类型的多个方法,包括输出参数类型为boolean、char、int、long、float和double。同时,也重载实现 了输出参数类型为char[]、String和Object的方法。其中,print(Object)和println(Object)方法在运行时将调 用参数Object的toString方法。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><div class="dp-highlighter bg_java" style="font-family:Consolas,'Courier New',Courier,mono,serif; border:1px dashed rgb(153,153,153); background-color:rgb(245,245,245); width:700.90625px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important"><ol start="1" class="dp-j" style="margin:0px; padding:5px 0px; border:none; position:relative; list-style-position:initial; color:rgb(92,92,92)"><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">import</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> java.io.BufferedReader;  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">import</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> java.io.IOException;  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">import</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> java.io.InputStreamReader;  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">class</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> StandardInputOutput {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">static</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">void</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> main(String args[]) {  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        String s;  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(153,153,153); background-color:inherit">// 创建缓冲区阅读器从键盘逐行读入数据</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        InputStreamReader ir = <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> InputStreamReader(System.in);  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        BufferedReader in = <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> BufferedReader(ir);  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        System.out.println(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"Unix系统: ctrl-d 或 ctrl-c 退出"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">                + <span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"\nWindows系统: ctrl-z 退出"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">try</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            <span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(153,153,153); background-color:inherit">// 读一行数据,并标准输出至显示器</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            s = in.readLine();  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            <span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(153,153,153); background-color:inherit">// readLine()方法运行时若发生I/O错误,将抛出IOException异常</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">while</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> (s != </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">null</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">) {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">                System.out.println(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"Read: "</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> + s);  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">                s = in.readLine();  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            }  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            <span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(153,153,153); background-color:inherit">// 关闭缓冲阅读器</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            in.close();  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        } <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">catch</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> (IOException e) { </span><span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(153,153,153); background-color:inherit">// Catch any IO exceptions.</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            e.printStackTrace();  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        }  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    }  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">}  </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px"></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><h2 style="margin:0px; padding:0px"><a name="t9"></a><a target="_blank" name="t9" style="color:rgb(51,102,153)"></a><p class="headline-1 bk-sidecatalog-title" style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:6px; border-bottom-color:rgb(222,223,225); border-bottom-width:1px; border-bottom-style:solid; clear:both"><span style="color:rgb(51,102,153); line-height:36px; font-size:22px">4.java.IO层次体系结构</span></p></h2></div><p style="color:rgb(51,51,51); font-size:14px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:0px; font-family:Verdana,Arial,Tahoma; line-height:25px"><span style="padding:0px; margin:0px">     <span style="line-height:24.799999237060547px">在整个Java.io包中最重要的就是5个类和一个接口。5个类指的是</span><span style="line-height:24.799999237060547px; padding:0px; margin:0px">File、OutputStream、InputStream、Writer、Reader</span><span style="line-height:24.799999237060547px">;一个接口指的是</span><span style="line-height:24.799999237060547px; padding:0px; margin:0px">Serializable</span><span style="line-height:24.799999237060547px">.掌握了这些IO的核心操作那么对于Java中的IO体系也就有了一个初步的认识了</span></span></p><p style="color:rgb(51,51,51); font-size:14px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:0px; font-family:Verdana,Arial,Tahoma; line-height:25px"><span style="padding:0px; margin:0px"></span></p><p style="color:rgb(51,51,51); font-size:14px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px">      Java I/O主要包括如下几个层次,<span style="font-family:宋体; line-height:26px">包含三个部分:</span></p><p style="color:rgb(51,51,51); font-size:14px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; margin:0cm 0cm 0pt"><span lang="EN-US" style="font-family:宋体">  <strong><span style="color:rgb(51,51,255)"> 1.流式部分</span></strong>――IO的主体部分;</span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; margin:0cm 0cm 0pt"><span lang="EN-US" style="font-family:宋体">   <span style="color:rgb(51,51,255)"><strong>2.非流式部分</strong></span>――主要包含一些辅助流式部分的类,如:File类、RandomAccessFile类和FileDescriptor等类;</span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; margin:0cm 0cm 0pt"><span lang="EN-US" style="font-family:宋体">   <strong><span style="color:rgb(51,51,255)">3.其他类</span></strong>--文件读取部分的与安全相关的类,如:SerializablePermission类,以及与本地操作系统相关的文件系统的类,如:FileSystem类和Win32FileSystem类和WinNTFileSystem类。</span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; margin:0cm 0cm 0pt"><span lang="EN-US" style="font-family:宋体">   主要的类如下:</span></p><p style="color:rgb(51,51,51); font-size:14px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px">     1. File(文件特征与管理):用于文件或者目录的描述信息,例如生成新目录,修改文件名,删除文件,判断文件所在路径等。</p><p style="color:rgb(51,51,51); font-size:14px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px">     2. InputStream(二进制格式操作):抽象类,基于字节的输入操作,是所有输入流的父类。定义了所有输入流都具有的共同特征。</p><p style="color:rgb(51,51,51); font-size:14px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px">     3. OutputStream(二进制格式操作):抽象类。基于字节的输出操作。是所有输出流的父类。定义了所有输出流都具有的共同特征。</p><p style="color:rgb(51,51,51); font-size:14px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px"><strong>     Java中字符是采用Unicode标准,一个字符是16位,即一个字符使用两个字节来表示。为此,JAVA中引入了处理字符的流。</strong></p><p style="color:rgb(51,51,51); font-size:14px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px">     4. Reader(文件格式操作):抽象类,基于字符的输入操作。</p><p style="color:rgb(51,51,51); font-size:14px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px">     5. Writer(文件格式操作):抽象类,基于字符的输出操作。</p><p style="color:rgb(51,51,51); font-size:14px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px">     6. RandomAccessFile(随机文件操作):它的功能丰富,<strong>可以从文件的任意位置进行存取(输入输出)操作</strong>。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><p style="color:rgb(51,51,51); font-size:14px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:0px; font-family:Verdana,Arial,Tahoma; line-height:25px"><span style="padding:0px; margin:0px">     Java中IO流的体系结构</span>如图:</p><p style="color:rgb(51,51,51); font-size:14px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:0px; font-family:Verdana,Arial,Tahoma; line-height:25px"><span style="padding:0px; margin:0px">     <img src="http://img.my.csdn.net/uploads/201212/14/1355467587_4888.jpg" alt="" style="border:none; max-width:100%"></span></p><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体"><span style="line-height:24px"><span style="font-family:Arial"></span></span></span><div><strong><span style="color:rgb(51,51,255)"><span style="font-size:18px"><br></span></span></strong></div><div><span style="font-weight:bold"><span style="color:rgb(51,51,255)"><span style="font-size:18px"></span></span></span><p></p><div><h2 style="margin:0px; padding:0px"><a name="t10"></a><a target="_blank" name="t10" style="color:rgb(51,102,153)"></a><p class="headline-1 bk-sidecatalog-title" style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:6px; border-bottom-color:rgb(222,223,225); border-bottom-width:1px; border-bottom-style:solid; clear:both"><span style="color:rgb(51,102,153); line-height:36px; font-size:22px">5. 非流式文件类--File类</span></p></h2></div></div><div><br></div><div>在Java语言的java.io包中,由File类提供了描述文件和目录的操作与管理方法。但File类不是InputStream、OutputStream或Reader、Writer的子类,因为它不负责数据的输入输出,而专门用来管理磁盘文件与目录。<br></div><div><br></div><div>作用:File类主要用于命名文件、查询文件属性和处理文件目录。<br><div class="dp-highlighter bg_java" style="font-family:Consolas,'Courier New',Courier,mono,serif; border:1px dashed rgb(153,153,153); background-color:rgb(245,245,245); font-size:12px; width:700.90625px; overflow:auto; padding-top:1px; margin:18px 0px!important"><ol start="1" class="dp-j" style="margin:0px; padding:5px 0px; border:none; position:relative; list-style-position:initial; color:rgb(92,92,92)"><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">    </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">class</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">   File   </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">extends</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> Object   </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">implements</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> Serializable,Comparable  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">{}  </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div></div><p></p>File类共提供了三个不同的构造函数,以不同的参数形式灵活地接收文件和目录名信息。构造函数:<br><strong>1)File (String   pathname)   </strong></div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">     例:File  f1=new File("FileTest1.txt"); //创建文件对象f1,f1所指的文件是在当前目录下创建的FileTest1.txt<br><strong>2)File (String  parent  ,  String child)</strong></div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">     例:File f2=new  File(“D:\\dir1","FileTest2.txt") ;//  注意:D:\\dir1目录事先必须存在,否则异常<br><strong>3)File (File    parent  , String child)</strong><br>     例:File  f4=new File("\\dir3");<br>          File  f5=new File(f4,"FileTest5.txt");  //在如果 \\dir3目录不存在使用f4.mkdir()先创建</div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">        一个对应于某磁盘文件或目录的File对象一经创建, 就可以通过调用它的方法来获得文件或目录的属性。    </div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">       1)public boolean exists( ) <span style="white-space:pre"></span>判断文件或目录是否存在<br>       2)public boolean isFile( ) <span style="white-space:pre"></span>判断是文件还是目录 <br>       3)public boolean isDirectory( )<span style="white-space:pre"> </span>判断是文件还是目录<br>       4)public String getName( )<span style="white-space:pre"> </span>返回文件名或目录名<br>       5)public String getPath( )<span style="white-space:pre"> </span>返回文件或目录的路径。<br>       6)public long length( )<span style="white-space:pre"> </span>获取文件的长度 <br>       7)public String[ ] list ( )<span style="white-space:pre"> </span>将目录中所有文件名保存在字符串数组中返回。 </div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">       File类中还定义了一些对文件或目录进行管理、操作的方法,常用的方法有:<br>       1) public boolean renameTo( File newFile );    重命名文件<br>       2) public void delete( );   删除文件<br>       3)  public boolean mkdir( ); 创建目录<br><br></div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">例子:</div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p><span style="font-family:宋体"><span style="line-height:24px"></span></span></p><div class="dp-highlighter bg_java" style="font-family:Consolas,'Courier New',Courier,mono,serif; border:1px dashed rgb(153,153,153); background-color:rgb(245,245,245); font-size:12px; width:700.90625px; overflow:auto; padding-top:1px; margin:18px 0px!important"><ol start="1" class="dp-j" style="margin:0px; padding:5px 0px; border:none; position:relative; list-style-position:initial; color:rgb(92,92,92)"><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">import</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> java.io.File;  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">import</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> java.io.IOException;  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">class</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> TestFile {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">static</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">void</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> main(String args[]) </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">throws</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> IOException {  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        File dir = <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> File(</span><span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"\\root"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        File f1 = <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> File(dir, </span><span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"fileOne.txt"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        File f2 = <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> File(dir, </span><span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"fileTwo.java"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(153,153,153); background-color:inherit">// 文件对象创建后,指定的文件或目录不一定物理上存在</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">if</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> (!dir.exists())  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            dir.mkdir();  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">if</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> (!f1.exists())  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            f1.createNewFile();  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">if</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> (!f2.exists())  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            f2.createNewFile();  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        System.out.println(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"f1's AbsolutePath=  "</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> + f1.getAbsolutePath());  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        System.out.println(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"f1 Canread="</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> + f1.canRead());  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        System.out.println(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"f1's len= "</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> + f1.length());  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        String[] FL;  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">int</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> count = </span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">;  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        FL = dir.list();  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">for</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> (</span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">int</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> i = </span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">; i < FL.length; i++) {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            count++;  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            System.out.println(FL[i] + <span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"is in \\root"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        }  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        System.out.println(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"there are"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> + count + </span><span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"file in //root"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    }  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">}  </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><p><span style="font-family:宋体"><span style="line-height:24px"></span></span></p><p>说明:File类的方法:<br>(1) exists()测试磁盘中指定的文件或目录是否存在<br>(2) mkdir()创建文件对象指定的目录(单层目录)<br>(3) createNewFile()创建文件对象指定的文件<br></p><p>(4) list()返回目录中所有文件名字符串</p><div style="line-height:24px"><strong><span style="color:rgb(51,51,255)"><span style="font-size:18px"></span></span></strong><h2 style="margin:0px; padding:0px"><a name="t11"></a><a target="_blank" name="t11" style="color:rgb(51,102,153)"></a><p class="headline-1 bk-sidecatalog-title" style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:6px; border-bottom-color:rgb(222,223,225); border-bottom-width:1px; border-bottom-style:solid; clear:both"><span style="color:rgb(51,102,153); line-height:36px; font-size:22px">6. Java.IO流类库</span></p></h2></div></div><h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t12"></a><a target="_blank" name="t12" style="color:rgb(51,102,153)"></a><span style="font-family:Arial,宋体"><span style="font-size:14.399999618530273px; line-height:24px">1. io流的<span style="font-size:14.399999618530273px">四个基本类</span></span></span></h3><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体"><span style="line-height:24px">      java.io包中包含了流式I/O所需要的所有类。在java.io包中有四个基本类:InputStream、OutputStream及Reader、Writer类,它们分别处理字节流和字符流:</span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体"><span style="line-height:24px"></span></span></p><p align="center" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><strong><span style="font-size:18px; color:rgb(255,0,255)">基本数据流的I/O</span></strong></p><div align="center" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><table id="table1" border="1" cellpadding="0" cellspacing="0" width="529"><tbody><tr><td valign="top"><p>输入/输出</p></td><td valign="top"><p>字节流</p></td><td valign="top"><p>字符流</p></td></tr><tr><td valign="top"><p>输入流</p></td><td valign="top"><p>Inputstream</p></td><td valign="top"><p>Reader</p></td></tr><tr><td valign="top"><p>输出流</p></td><td valign="top"><p>OutputStream</p></td><td valign="top"><p>Writer</p></td></tr></tbody></table></div><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体"><span style="line-height:24px"><strong><span style="color:rgb(255,0,0)">     </span></strong>Java中其他多种多样变化的流均是由它们派生出来的:</span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体"><span style="line-height:24px"><img src="http://my.csdn.net/uploads/201204/01/1333250374_3564.gif" alt="" style="border:none; max-width:100%"></span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体"><span style="line-height:24px"><img src="http://my.csdn.net/uploads/201204/01/1333250382_9126.gif" alt="" style="border:none; max-width:100%"></span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体"><span style="line-height:24px"><img src="http://my.csdn.net/uploads/201204/01/1333250341_6152.gif" alt="" style="border:none; max-width:100%"></span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体"><span style="line-height:24px"><img src="http://my.csdn.net/uploads/201204/01/1333250133_8675.gif" alt="" style="border:none; max-width:100%"></span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体"><span style="line-height:24px">      JDK1.4版本开始引入了新I/O类库,它位于java.nio包中,新I/O类库利用通道和缓冲区等来提高I/O操作的效率。<br></span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体"><span style="line-height:24px">      在java.io包中, java.io.InputStream 表示字节输入流, java.io.OutputStream表示字节输出流,处于java.io包最顶层。</span></span><span style="font-family:Arial,宋体; line-height:24px">这两个类均为抽象类,也就是说它们不能被实例化,必须生成子类之后才能实现一定的功能。</span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体; line-height:24px"></span></p><h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t13"></a><a target="_blank" name="t13" style="color:rgb(51,102,153)"></a><span style="font-family:Arial,宋体"><span style="line-height:24px">1. io流的具体分类</span></span></h3><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体; line-height:24px"><strong>一、按I/O类型来总体分类:</strong></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体; line-height:24px">    <strong> 1. Memory</strong> 1)从/向内存数组读写数据: CharArrayReader、 CharArrayWriter、ByteArrayInputStream、ByteArrayOutputStream<span style="white-space:pre"></span><br>                   2)从/向内存字符串读写数据 StringReader、StringWriter、StringBufferInputStream<span style="white-space:pre"></span><br>    <strong> 2.Pipe管道  </strong>实现管道的输入和输出(<span style="font-size:12px">进程间通信</span>): PipedReader、PipedWriter、PipedInputStream、PipedOutputStream<span style="white-space:pre"></span><br>     <strong>3.File 文件流</strong>。对文件进行读、写操作 :FileReader、FileWriter、FileInputStream、FileOutputStream<span style="white-space:pre"></span><br>     <strong>4. ObjectSerialization 对象输入、输出</strong> :ObjectInputStream、ObjectOutputStream<span style="white-space:pre"></span><br>     <strong>5.DataConversion数据流</strong> 按基本数据类型读、写(处理的数据是Java的基本类型(如布尔型,字节,整数和浮点数)):DataInputStream、DataOutputStream<span style="white-space:pre"></span><br>    <strong> 6.Printing</strong> <span style="white-space:pre"></span>包含方便的打印方法 :PrintWriter、PrintStream<span style="white-space:pre"></span><br>     <strong>7.Buffering缓冲 </strong> 在读入或写出时,对数据进行缓存,以减少I/O的次数:BufferedReader、BufferedWriter、BufferedInputStream、BufferedOutputStream<span style="white-space:pre"></span><br>     <strong>8.Filtering </strong><span style="white-space:pre"></span><strong>滤流</strong>,在数据进行读或写时进行过滤:FilterReader、FilterWriter、FilterInputStream、FilterOutputStream<span style="white-space:pre"></span>过<br>     <strong>9.Concatenation合并输入</strong> 把多个输入流连接成一个输入流 :SequenceInputStream <span style="white-space:pre"></span><br>    <strong>10.Counting计数</strong>  在读入数据时对行记数 :LineNumberReader、LineNumberInputStream<span style="white-space:pre"></span><br>    <strong>11.Peeking Ahead</strong> 通过缓存机制,进行预读 :PushbackReader、PushbackInputStream<span style="white-space:pre"></span><br>    <strong>12.Converting between Bytes and Characters</strong> 按照一定的编码/解码标准将字节流转换为字符流,或进行反向转换(<span style="font-size:12px">Stream到Reader,Writer的转换类</span>):<span style="white-space:pre"></span>InputStreamReader、OutputStreamWriter<span style="white-space:pre"></span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体; line-height:24px"><span style="font-size:12px"><strong>二、按数据来源(去向)分类:</strong> <br>1、File(文件): FileInputStream, FileOutputStream, FileReader, FileWriter <br>2、byte[]:ByteArrayInputStream, ByteArrayOutputStream <br>3、Char[]: CharArrayReader, CharArrayWriter <br>4、String: StringBufferInputStream, StringReader, StringWriter <br>5、网络数据流:InputStream, OutputStream, Reader, Writer </span><br></span></p><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"><span style="font-size:12px"></span></p><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px"><h2 style="margin:0px; padding:0px; line-height:26px"><a name="t14"></a><a target="_blank" name="t14" style="color:rgb(51,102,153)"></a><p class="headline-1 bk-sidecatalog-title" style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:6px; border-bottom-color:rgb(222,223,225); border-bottom-width:1px; border-bottom-style:solid; clear:both"><span style="color:rgb(51,102,153)"><span style="line-height:36px; font-size:22px">7. 字节流InputStream/OutputStream</span></span></p></h2></div><h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t15"></a><a target="_blank" name="t15" style="color:rgb(51,102,153)"></a><span style="font-size:12px"><span style="font-family:Verdana,Arial,Tahoma; font-size:14px; line-height:24.799999237060547px"><strong><span style="color:rgb(51,51,255)"> 1. InputStream<span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:18.399999618530273px; line-height:24.799999237060547px"><strong>抽象类 </strong></span></span></strong></span></span></h3><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"><span style="font-size:12px"><span style="padding:0px; margin:0px; font-family:Verdana,Arial,Tahoma; font-size:14px; line-height:25px">      InputStream 为字节输入流,它本身为一个抽象类,必须依靠其子类实现各种功能,此抽象类是表示字节输入流的所有类的超类。</span><span style="padding:0px; margin:0px; font-family:Verdana,Arial,Tahoma; font-size:14px; line-height:25px"> 继承自InputStream  的流都是向程序中输入数据的,且数据单位为字节(8bit);</span><br></span></p><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"><span style="font-size:12px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px">InputStream是输入字节数据用的类,所以InputStream类提供了3种重载的read方法.Inputstream类中的常用方法: </span><br style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px">  (1) public abstract int read( ):读取一个byte的数据,返回值是高位补0的int类型值。<span style="color:rgb(255,0,0)"><span style="font-family:Verdana,Arial,Tahoma; font-size:14.399999618530273px; line-height:24.799999237060547px">若返</span><span style="font-family:Verdana,Arial,Tahoma; font-size:14.399999618530273px; line-height:24.799999237060547px">回值=-1说明没有读取到任何字节读取工作结束。</span></span></span><br style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px">  (2) public int read(byte b[ ]):读取b.length个字节的数据放到b数组中。返回值是读取的字节数。该方法实际上是调用下一个方法实现的 </span><br style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px">  (3) public int read(byte b[ ], int off, int len):从输入流中最多读取len个字节的数据,存放到偏移量为off的b数组中。 </span><br style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px">  (4) public int available( ):返回输入流中可以读取的字节数。<span style="color:rgb(255,102,0)">注意:若输入阻塞,当前线程将被挂起,如果InputStream对象调用这个方法的话,它只会返回0,这个方法必须由继承InputStream类的子类对象调用才有用,</span> </span><br style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px">  (5) public long skip(long n):忽略输入流中的n个字节,返回值是实际忽略的字节数, 跳过一些字节来读取 </span><br style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px">  (6) public int close( ) :我们在使用完后,必须对我们打开的流进行关闭. </span><br></span></p><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"><span style="font-size:12px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px">         主要的子类:</span></span></p><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"><span style="font-size:12px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px">        <img src="http://my.csdn.net/uploads/201204/01/1333250374_3564.gif" alt="" style="border:none; max-width:100%; font-family:Arial,宋体; line-height:24px"></span></span></p><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"><span style="font-size:12px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px">         1) FileInputStream把一个文件作为InputStream,实现对文件的读取操作     <br>    2) ByteArrayInputStream:把内存中的一个缓冲区作为InputStream使用 <br>    3) StringBufferInputStream:把一个String对象作为InputStream <br>    4) PipedInputStream:实现了pipe的概念,主要在线程中使用 <br>    5) SequenceInputStream:把多个InputStream合并为一个InputStream </span></span></p><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"></p><h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t16"></a><a target="_blank" name="t16" style="color:rgb(51,102,153)"></a><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px"><strong><span style="color:rgb(51,51,255)"> 2.OutputStream</span></strong></span><span style="color:rgb(51,51,255); font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px">抽象类 </span></h3><span style="color:rgb(51,51,51); font-family:Arial; line-height:26px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px">  OutputStream提供了3个write方法来做数据的输出,这个是和InputStream是相对应的。 <br>  1. public void write(byte b[ ]):将参数b中的字节写到输出流。 <br>  2. public void write(byte b[ ], int off, int len) :将参数b的从偏移量off开始的len个字节写到输出流。 <br>  3. public abstract void write(int b) :先将int转换为byte类型,把低字节写入到输出流中。 <br>  4. public void flush( ) : 将数据缓冲区中数据全部输出,并清空缓冲区。 <br>  5. public void close( ) : 关闭输出流并释放与流相关的系统资源。 <br></span></span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></span><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">       主要的子类:</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">        <img src="http://my.csdn.net/uploads/201204/01/1333250382_9126.gif" alt="" style="border:none; max-width:100%; font-family:Arial,宋体; line-height:24px"></p><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"><span style="font-size:12px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px">      1) ByteArrayOutputStream:把信息存入内存中的一个缓冲区中 <br>      2) FileOutputStream:把信息存入文件中 <br>      3) PipedOutputStream:实现了pipe的概念,主要在线程中使用 <br>      4) SequenceOutputStream:把多个OutStream合并为一个OutStream </span></span></p><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"><span style="font-size:12px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px"><span style="font-size:14.399999618530273px; line-height:24.799999237060547px"><span style="color:rgb(255,153,0)">流结束的判断:方法read()的返回值为-1时;readLine()的返回值为null时。</span></span></span></span></p><h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t17"></a><a target="_blank" name="t17" style="color:rgb(51,102,153)"></a><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px"><span style="color:rgb(51,51,255)">3. 文件输入流: FileInputStream类</span></span></h3><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"><span style="font-size:12px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px"><span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; font-size:14.399999618530273px; line-height:17.600000381469727px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px">      FileInputStream可以使用read()方法一次读入一个字节,并以int类型返回,或者是使用read()方法时读入至一个byte数组,byte数组的元素有多少个,就读入多少个字节。在将整个文件读取完成或写入完毕的过程中,这么一个byte数组通常被当作缓冲区,因为这么一个byte数组通常扮演承接数据的中间角色。</span></span></span></span></p><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"><span style="font-size:12px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px"><span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; font-size:14.399999618530273px; line-height:17.600000381469727px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px"><span style="font-family:宋体"><span style="line-height:24px"></span></span></span></span></span></span></p><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></div><img src="http://img.my.csdn.net/uploads/201211/29/1354189614_8795.jpg" alt="" style="border:none; max-width:100%; color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:宋体"><span style="line-height:24px"></span></span></p><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></div><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">作用:以文件作为数据输入源的数据流。或者说是打开文件,从文件读数据到内存的类。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">使用方法(1)    </span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">   File  fin=new File("d:/abc.txt"); </span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">  FileInputStream in=new FileInputStream( fin);</span><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:宋体"><span style="line-height:24px">使用方法(2)<br>   FileInputStream  in=new  FileInputStream(“d: /abc.txt”);</span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="line-height:24px">程序举例:<br>将InputFromFile.java的程序的内容显示在显示器上<br></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="line-height:24px"><img src="http://img.my.csdn.net/uploads/201211/29/1354189800_1523.jpg" alt="" style="border:none; max-width:100%"><br></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体"><span style="line-height:24px"></span></span></p><div class="dp-highlighter bg_java" style="font-family:Consolas,'Courier New',Courier,mono,serif; border:1px dashed rgb(153,153,153); background-color:rgb(245,245,245); width:700.90625px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important"><ol start="1" class="dp-j" style="margin:0px; padding:5px 0px; border:none; position:relative; list-style-position:initial; color:rgb(92,92,92)"><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">import</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> java.io.IOException;  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">import</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> java.io.FileInputStream;  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">;  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">class</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> TestFile {  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">static</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">void</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> main(String args[]) </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">throws</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> IOException {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">try</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">{      </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">               FileInputStream rf=<span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">   FileInputStream(</span><span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"InputFromFile.java"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">               <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">int</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> n=</span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">512</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">;   </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">byte</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  buffer[]=</span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">byte</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">[n];     </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">               <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">while</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">((rf.read(buffer,</span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">,n)!=-</span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)&&(n></span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)){  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">                   System.out.println(<span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> String(buffer) );  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">                }  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">                System.out.println();  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">                rf.close();  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        } <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">catch</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">(IOException  IOe){        </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">              System.out.println(IOe.toString());  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        }  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    }  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">}  </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"></p><h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t18"></a><a target="_blank" name="t18" style="color:rgb(51,102,153)"></a><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px"><span style="color:rgb(51,51,255)">4.<span style="font-size:18.399999618530273px; line-height:24.799999237060547px"><strong>文件输出流:FileOutputStream类</strong></span></span></span></h3><span style="color:rgb(51,51,51); font-size:14px; font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px">   <span style="font-size:14.399999618530273px; line-height:24.799999237060547px">作用:用来处理以文件作为数据输出目的数据流;或者说是从内存区读数据入文件</span></span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></span><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px">      FileOutputStream类用来处理以文件作为数据输出目的数据流;一个表示文件名的字符串,也可以是File或FileDescriptor对象。 <br>  创建一个文件流对象有两种方法: <br>  方式1: <br>  <span style="font-size:14.399999618530273px; line-height:24.799999237060547px"> File   f=new  File (“d:/myjava/write.txt ");</span><br style="font-size:14.399999618530273px; line-height:24.799999237060547px"><span style="font-size:14.399999618530273px; line-height:24.799999237060547px">        FileOutputStream  out= new FileOutputStream (f);</span><br>  方式2: <br>  <span style="font-size:14.399999618530273px; line-height:24.799999237060547px">FileOutputStream out=new FileOutputStream(“d:/myjava/write.txt "); </span><br>  方式3:构造函数将 FileDescriptor()对象作为其参数。 <br>  FileDescriptor() fd=new FileDescriptor(); <br>  FileOutputStream f2=new FileOutputStream(fd); <br>  方式4:构造函数将文件名作为其第一参数,将布尔值作为第二参数。 <br>  FileOutputStream f=new FileOutputStream("d:/abc.txt",true); <br>  注意: (1)文件中写数据时,若文件已经存在,则覆盖存在的文件;(2)的读/写操作结束时,应调用close方法关闭流。 </span></p><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px"></span></p><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><div><span style="color:red">程序举例:</span><span style="color:rgb(51,51,204)">使用键盘输入一段文章,将文章保存在文件</span>write.txt中</div><img src="http://img.my.csdn.net/uploads/201211/29/1354190012_9885.jpg" alt="" style="border:none; max-width:100%"><br></div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><div class="dp-highlighter bg_java" style="font-family:Consolas,'Courier New',Courier,mono,serif; border:1px dashed rgb(153,153,153); background-color:rgb(245,245,245); font-size:12px; width:700.90625px; overflow:auto; padding-top:1px; margin:18px 0px!important"><ol start="1" class="dp-j" style="margin:0px; padding:5px 0px; border:none; position:relative; list-style-position:initial; color:rgb(92,92,92)"><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">import</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> java.io.IOException;  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">import</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> java.io.FileOutputStream;  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">class</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> TestFile {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">static</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">void</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> main(String args[]) </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">throws</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> IOException {  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">try</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            System.out.println(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"please Input from      Keyboard"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">int</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> count, n = </span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">512</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">;  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">byte</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> buffer[] = </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">byte</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">[n];  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            count = System.in.read(buffer);  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            FileOutputStream wf = <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> FileOutputStream(</span><span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"d:/myjava/write.txt"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            wf.write(buffer, <span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, count);  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            wf.close(); <span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(153,153,153); background-color:inherit">// 当流写操作结束时,调用close方法关闭流。</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            System.out.println(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"Save to the write.txt"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        } <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">catch</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> (IOException IOe) {  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            System.out.println(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"File Write Error!"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        }  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    }  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">}  </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><h3 style="margin:0px; padding:0px"><a name="t19"></a><a target="_blank" name="t19" style="color:rgb(51,102,153)"></a><span style="color:rgb(51,51,255)">5. FileInputStream流和FileOutputStream的应用</span></h3></div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">利用程序将文件file1.txt 拷贝到file2.txt中。<br><div class="dp-highlighter bg_java" style="font-family:Consolas,'Courier New',Courier,mono,serif; border:1px dashed rgb(153,153,153); background-color:rgb(245,245,245); font-size:12px; width:700.90625px; overflow:auto; padding-top:1px; margin:18px 0px!important"><ol start="1" class="dp-j" style="margin:0px; padding:5px 0px; border:none; position:relative; list-style-position:initial; color:rgb(92,92,92)"><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">import</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> java.io.File;  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">import</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> java.io.IOException;  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">import</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> java.io.FileOutputStream;  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">import</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> java.io.FileInputStream;  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">class</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> TestFile {  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">static</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">void</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> main(String args[]) </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">throws</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> IOException {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">try</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> {  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            File inFile = <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> File(</span><span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"copy.java"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            File outFile = <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> File(</span><span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"copy2.java"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            FileInputStream finS = <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> FileInputStream(inFile);  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            FileOutputStream foutS = <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> FileOutputStream(outFile);  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">int</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> c;  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">while</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> ((c = finS.read()) != -</span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">) {  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">                foutS.write(c);  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            }  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            finS.close();  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            foutS.close();  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        } <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">catch</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> (IOException e) {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            System.err.println(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"FileStreamsTest: "</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> + e);  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        }  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    }  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">}  </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div></div><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><h3 style="margin:0px; padding:0px"><a name="t20"></a><a target="_blank" name="t20" style="color:rgb(51,102,153)"></a><span style="font-size:14px; color:rgb(51,51,255)">6. 缓冲输入输出流 </span><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:24.799999237060547px"><span style="font-size:14px; color:rgb(51,51,255)">BufferedInputStream/ <span style="line-height:24.799999237060547px">BufferedOutputStream</span></span></span></h3><span style="font-size:14.399999618530273px">        <img src="http://img.my.csdn.net/uploads/201212/14/1355473479_8987.jpg" alt="" style="border:none; max-width:100%"></span></div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">      </div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="font-family:Arial,宋体; line-height:24px">       计算机访问外部设备非常耗时。访问外存的频率越高,造成CPU闲置的概率就越大。为了减少访问外存的次数,应该在一次对外设的访问中,读写更多的数据。为此,除了程序和流节点间交换数据必需的读写机制外,还应该增加缓冲机制。缓冲流就是每一个数据流分配一个缓冲区,一个缓冲区就是一个临时存储数据的内存。这样可以减少访问硬盘的次数,提高传输效率。</p><p style="font-family:Arial,宋体; line-height:24px"></p><p><span style="font-family:Arial,宋体"><span style="line-height:24px">BufferedInputStream:当向缓冲流写入数据时候,数据先写到缓冲区,待缓冲区写满后,系统一次性将数据发送给输出设备。</span></span></p><p><span style="font-family:Arial,宋体"><span style="line-height:24px">BufferedOutputStream :当从向缓冲流读取数据时候,系统先从缓冲区读出数据,待缓冲区为空时,系统再从输入设备读取数据到缓冲区。</span></span></p></div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,255)"><strong>1)将文件读入内存:</strong></span></div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p>将<span style="color:rgb(204,0,204)">BufferedInputStream</span>与<span style="color:rgb(51,51,204)">FileInputStream</span>相接</p><p>  FileInputStream <span style="color:rgb(51,51,204)">in</span>=new  <span style="color:rgb(51,51,204)">FileInputStream</span>( “file1.txt ” );</p><p>  BufferedInputStream <span style="color:rgb(204,0,204)">bin</span>=new  <span style="color:rgb(204,0,204)">BufferedInputStream</span>( <span style="color:rgb(51,51,204)">in</span>); </p><p><span style="font-family:Arial,宋体; line-height:24px"><strong><span style="color:rgb(51,51,255)">2)将内存写入文件:</span></strong></span><br></p><p>将<span style="color:rgb(204,0,204)">BufferedOutputStream</span>与<span style="color:rgb(204,0,204)"> </span><span style="color:rgb(51,51,204)">FileOutputStream</span>相接</p><p>FileOutputStream<span style="color:rgb(51,51,204)">out</span>=new <span style="color:rgb(51,51,204)">FileOutputStream</span>(“file1.txt”);</p><p>BufferedOutputStream  <span style="color:rgb(255,51,153)">bin</span>=new <span style="color:rgb(255,51,153)">BufferedInputStream</span>(<span style="color:rgb(51,51,204)">out</span>);</p><br><strong><span style="color:rgb(51,51,255)">3)键盘输入流读到内存</span></strong></div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,204)">将</span><span style="color:rgb(51,51,204)">BufferedReader</span><span style="color:rgb(51,51,204)">与</span><span style="color:rgb(51,51,204)">标准的数据流</span><span style="color:rgb(51,51,204)">相接</span> </div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"> InputStreamReader <span style="color:rgb(51,51,204)">sin</span>=new InputStreamReader (<span style="color:red">System.in</span>) ;</div><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">BufferedReader bin=new             BufferedReader(<span style="color:rgb(51,51,204)">sin</span>);</div><div class="dp-highlighter bg_java" style="font-family:Consolas,'Courier New',Courier,mono,serif; border:1px dashed rgb(153,153,153); background-color:rgb(245,245,245); width:700.90625px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important"><ol start="1" class="dp-j" style="margin:0px; padding:5px 0px; border:none; position:relative; list-style-position:initial; color:rgb(92,92,92)"><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">import</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> java.io.*;  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">class</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> ReadWriteToFile {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">static</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">void</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> main(String args[]) </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">throws</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> IOException {  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        InputStreamReader sin = <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> InputStreamReader(System.in);  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        BufferedReader bin = <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> BufferedReader(sin);  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        FileWriter out = <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> FileWriter(</span><span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"myfile.txt"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        BufferedWriter bout = <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> BufferedWriter(out);  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        String s;  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">while</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> ((s = bin.readLine()).length() > </span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">) {  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            bout.write(s, <span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, s.length());  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        }  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    }  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">}  </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">程序说明:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">从键盘读入字符,并写入到文件中</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">BufferedReader类的方法:</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">String readLine()</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">作用:读一行字符串,以回车符为结束。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">BufferedWriter类的方法:</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">bout.write(String s,offset,len)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">作用:从缓冲区将字符串s从offset开始,len长度的字符串写到某处。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px"></span></p><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体"><span style="line-height:24px"><span style="font-family:Arial"></span></span></span><h2 style="margin:0px; padding:0px"><a name="t21"></a><a target="_blank" name="t21" style="color:rgb(51,102,153)"></a><p class="headline-1 bk-sidecatalog-title" style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:6px; border-bottom-color:rgb(222,223,225); border-bottom-width:1px; border-bottom-style:solid; clear:both"><span style="color:rgb(51,102,153); line-height:36px; font-size:22px">8. 字符流Writer/Reader</span></p></h2></div><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Verdana,Arial,Tahoma; line-height:25px"><span style="color:rgb(51,51,255)"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif">       <span style="font-size:14.399999618530273px; line-height:24.799999237060547px"><strong> </strong></span><span style="font-size:14.399999618530273px; line-height:24.799999237060547px">Java中字符是采用Unicode标准,一个字符是16位,即一个字符使用两个字节来表示。为此,JAVA中引入了处理字符的流。</span></span></span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Verdana,Arial,Tahoma; line-height:25px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif"><span style="font-size:14.399999618530273px; line-height:24.799999237060547px"></span></span></span></p><h3 style="margin:0px; padding:0px; font-family:Arial; line-height:26px; color:rgb(51,51,255)"><a name="t22"></a><a target="_blank" name="t22" style="color:rgb(51,102,153)"></a><span style="font-family:Verdana,Arial,Tahoma; line-height:25px">1. Reader抽象类</span></h3><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><p style="font-family:Arial; font-size:14px; line-height:26px; color:rgb(51,51,255)"><span style="color:rgb(51,51,51); font-family:Verdana,Arial,Tahoma; line-height:25px">    用于读取字符流的抽象类。子类必须实现的方法只有 read(char[], int, int) 和 close()。但是,多数子类将重写此处定义的一些方法,以提供更高的效率和/或其他功能。</span></p><span style="font-family:Arial; font-size:14px; line-height:26px; color:rgb(51,51,255)">       <img src="http://my.csdn.net/uploads/201204/01/1333250341_6152.gif" alt="" style="border:none; max-width:100%; font-family:Arial,宋体; line-height:24px"></span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></span><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px">        <strong><span style="color:rgb(51,51,255)">1) FileReader :</span></strong>与FileInputStream对应  <br>           主要用来读取字符文件,使用缺省的字符编码,有三种构造函数: <br>      (1)将文件名作为字符串 :FileReader f=new FileReader(“c:/temp.txt”); <br>      (2)构造函数将File对象作为其参数。 <br>              File f=new file(“c:/temp.txt”); <br>              FileReader f1=new FileReader(f); <br>     (3)  构造函数将FileDescriptor对象作为参数 <br>            FileDescriptor() fd=new FileDescriptor() <br>            FileReader f2=new FileReader(fd); <br>               (1) 用指定字符数组作为参数:CharArrayReader(char[]) <br>               (2) 将字符数组作为输入流:CharArrayReader(char[], int, int) <br>          读取字符串,构造函数如下: public StringReader(String s); <br>      <span style="color:rgb(51,51,255)"><strong>  2) CharArrayReader:</strong></span>与ByteArrayInputStream对应  <br>  <span style="color:rgb(51,51,255)"><strong>3) StringReader : </strong></span>与StringBufferInputStream对应 <br>  <span style="color:rgb(51,51,255)"><strong>4) InputStreamReader </strong></span><br>        从输入流读取字节,在将它们转换成字符:Public inputstreamReader(inputstream is); <br>  <span style="color:rgb(51,51,255)"><strong>5) FilterReader: </strong></span>允许过滤字符流 <br>        protected filterReader(Reader r); <br>  <span style="color:rgb(51,51,255)"><strong>6) BufferReader :</strong></span>接受Reader对象作为参数,并对其添加字符缓冲器,使用readline()方法可以读取一行。 <br>     Public BufferReader(Reader r); </p><p style="font-size:14px; color:rgb(51,51,255); font-family:Arial,宋体; line-height:24px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px">      主要方法:<br style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px"></span></p><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px"><span style="color:rgb(51,51,255)">     </span> <span style="font-size:14.399999618530273px"><strong><span style="font-family:Verdana,Helvetica,Arial; line-height:18px"><span style="font-weight:normal">(1)  public int read() throws IOException; //读取一个字符,返回值为读取的字符 </span></span></strong></span><span style="font-size:14.399999618530273px"><br></span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px"><span style="font-size:14.399999618530273px"><span style="font-family:Verdana,Helvetica,Arial; line-height:18px">  (2)  public int read(char cbuf[]) throws IOException; /*读取一系列字符到数组cbuf[]中,返回值为实际读取的字符的数量*/ <br></span></span></span><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px"><span style="font-size:14.399999618530273px"><span style="font-family:Verdana,Helvetica,Arial; line-height:18px">  (3)  public abstract int read(char cbuf[],int off,int len) throws IOException; <br></span></span></span><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px"><span style="font-size:14.399999618530273px"><span style="font-family:Verdana,Helvetica,Arial; line-height:18px">  /*读取len个字符,从数组cbuf[]的下标off处开始存放,返回值为实际读取的字符数量,该方法必须由子类实现*/ </span></span></span></p><h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t23"></a><a target="_blank" name="t23" style="color:rgb(51,102,153)"></a><span style="font-family:Verdana,Arial,Tahoma; line-height:25px"><span style="color:rgb(51,51,255)">2. Writer抽象类</span></span></h3><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Verdana,Arial,Tahoma; line-height:25px">     写入字符流的抽象类。子类必须实现的方法仅有 write(char[], int, int)、flush() 和 close()。但是,多数子类将重写此处定义的一些方法,以提供更高的效率和/或其他功能。 其子类如下:</span><span style="text-indent:-28px"></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Verdana,Arial,Tahoma; line-height:25px">    <img src="http://my.csdn.net/uploads/201204/01/1333250133_8675.gif" alt="" style="border:none; max-width:100%; font-family:Arial,宋体; line-height:24px"></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Verdana,Arial,Tahoma; line-height:25px">      <span style="color:rgb(51,51,255)">1) FileWrite:</span></span><span style="font-family:Verdana,Arial,Tahoma; line-height:25px"> <span style="font-size:14.399999618530273px; line-height:24.799999237060547px">与FileOutputStream对应  </span><br>  将字符类型数据写入文件,使用缺省字符编码和缓冲器大小。 <br>  Public FileWrite(file f); <br>  </span><span style="font-family:Verdana,Arial,Tahoma; line-height:25px"><span style="color:rgb(51,51,255)">2)  chararrayWrite:</span><span style="font-size:14.399999618530273px; line-height:24.799999237060547px">与ByteArrayOutputStream对应 ,</span>将字符缓冲器用作输出。 <br>      Public CharArrayWrite(); <br>  <span style="color:rgb(51,51,255)">3) PrintWrite:</span>生成格式化输出 <br>      public PrintWriter(outputstream os); <br>  <span style="color:rgb(51,51,255)">4) filterWriter</span>:用于写入过滤字符流 <br>      protected FilterWriter(Writer w); <br>  <span style="font-size:14.399999618530273px; line-height:24.799999237060547px"><span style="color:rgb(51,51,255)">5) PipedWriter:</span></span><span style="font-size:14.399999618530273px; line-height:24.799999237060547px">与PipedOutputStream对应  </span> </span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Verdana,Arial,Tahoma; line-height:25px">      <span style="font-size:14.399999618530273px; line-height:24.799999237060547px"><span style="color:rgb(51,51,255)">6) StringWriter:</span></span><span style="font-size:14.399999618530273px; line-height:24.799999237060547px">无与之对应的以字节为导向的stream  </span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Verdana,Arial,Tahoma; line-height:25px">      主要方法:</span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Verdana,Arial,Tahoma; line-height:25px"><span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px">  <span style="font-size:14.399999618530273px; line-height:17.600000381469727px">(1)  </span>public void write(int c) throws IOException; </span><span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px">//将整型值c的低16位写入输出流 </span><br style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px"><span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px">  (<span style="font-size:14.399999618530273px; line-height:17.600000381469727px">2)  </span>public void write(char cbuf[]) throws IOException; </span><span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px">//将字符数组cbuf[]写入输出流 </span><br style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px"><span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px">  <span style="font-size:14.399999618530273px; line-height:17.600000381469727px">(</span><span style="font-size:14.399999618530273px; line-height:17.600000381469727px">3)  </span>public abstract void write(char cbuf[],int off,int len) throws IOException; </span><span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px">//将字符数组cbuf[]中的从索引为off的位置处开始的len个字符写入输出流 </span><br style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px"><span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px">  <span style="font-size:14.399999618530273px; line-height:17.600000381469727px">(</span><span style="font-size:14.399999618530273px; line-height:17.600000381469727px">4)  </span>public void write(String str) throws IOException; </span><span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px">//将字符串str中的字符写入输出流 </span><br style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px"><span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px">  <span style="font-size:14.399999618530273px; line-height:17.600000381469727px">(</span><span style="font-size:14.399999618530273px; line-height:17.600000381469727px">5)  </span>public void write(String str,int off,int len) throws IOException; </span><span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px">//将字符串str 中从索引off开始处的len个字符写入输出流 </span><br style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px"><span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px">  <span style="font-size:14.399999618530273px; line-height:17.600000381469727px">(</span><span style="font-size:14.399999618530273px; line-height:17.600000381469727px">6)  </span>flush( ) //</span><span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px">刷空输出流,并输出所有被缓存的字节。 </span><br style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px"><span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px">  <span style="font-size:14.399999618530273px; line-height:17.600000381469727px">(</span><span style="font-size:14.399999618530273px; line-height:17.600000381469727px">7)<span style="font-size:14.399999618530273px; line-height:17.600000381469727px">close() </span>  </span> 关闭流 </span><span style="color:rgb(94,94,94); font-family:Verdana,Helvetica,Arial; line-height:18px">public abstract void close() throws IOException</span></span></p><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px"></span></p><p style="color:rgb(51,51,51); font-size:14px; font-family:Arial,宋体; line-height:24px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25px"></span></p><h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t24"></a><a target="_blank" name="t24" style="color:rgb(51,102,153)"></a><span style="font-family:Arial,宋体"><span style="line-height:24px"><strong><span style="font-size:18px; color:rgb(51,51,255)">3 .InputStream与Reader差别 OutputStream与Writer差别</span></strong></span></span></h3><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体"><span style="line-height:24px">InputStream和OutputStream类处理的是字节流,数据流中的最小单位是字节(8个bit)<br>Reader与Writer处理的是字符流,在处理字符流时涉及了字符编码的转换问题<br></span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体"><span style="line-height:24px"></span></span></p><div class="dp-highlighter bg_java" style="font-family:Consolas,'Courier New',Courier,mono,serif; border:1px dashed rgb(153,153,153); background-color:rgb(245,245,245); width:700.90625px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important"><ol start="1" class="dp-j" style="margin:0px; padding:5px 0px; border:none; position:relative; list-style-position:initial; color:rgb(92,92,92)"><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">import</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> java.io.*;  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">class</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> EncodeTest {  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">private</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">static</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">void</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> readBuff(</span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">byte</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> [] buff) </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">throws</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> IOException {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">       ByteArrayInputStream in =<span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">new</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> ByteArrayInputStream(buff);  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">int</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> data;  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">while</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">((data=in.read())!=-</span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)   System.out.print(data+</span><span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"  "</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        System.out.println();     in.close();     }  </span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">   <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">public</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">static</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">void</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> main(String args[]) </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">throws</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> IOException {  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">       System.out.println(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"内存中采用unicode字符编码:"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> );  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">       <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">char</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">   c=</span><span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">'好'</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">;  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">       <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">int</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> lowBit=c&</span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">0xFF</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">;     </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,0,255); background-color:inherit; font-weight:bold">int</span><span style="margin:0px; padding:0px; border:none; background-color:inherit"> highBit=(c&</span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">0xFF00</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)>></span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">8</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">;  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">       System.out.println(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">""</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">+lowBit+</span><span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"   "</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">+highBit);  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">       String s=<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"好"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">;  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">       System.out.println(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"本地操作系统默认字符编码:"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">       readBuff(s.getBytes());  </span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">       System.out.println(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"采用GBK字符编码:"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);  </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">       readBuff(s.getBytes(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"GBK"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">));  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">       System.out.println(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"采用UTF-8字符编码:"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">);        </span></span></li><li style="border:none; list-style:decimal-leading-zero outside; line-height:13px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">       readBuff(s.getBytes(<span class="string" style="margin:0px; padding:0px; border:none; color:rgb(0,153,0); background-color:inherit">"UTF-8"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">));      }  </span></span></li><li class="alt" style="margin:0px!important; padding:0px 3px 0px 10px!important; border:none; list-style:decimal-leading-zero outside; color:inherit; line-height:13px"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">}  </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><img src="http://img.my.csdn.net/uploads/201211/29/1354191297_8218.jpg" alt="" style="border:none; max-width:100%; color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Reader类能够将输入流中采用其他编码类型的字符转换为Unicode字符,然后在内存中为其分配内存</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Writer类能够将内存中的Unicode字符转换为其他编码类型的字符,再写到输出流中。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><div style="color:rgb(51,51,51); font-size:14.399999618530273px; font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:24.799999237060547px"><h2 style="margin:0px; padding:0px"><a name="t25"></a><a target="_blank" name="t25" style="color:rgb(51,102,153)"></a><p class="headline-1 bk-sidecatalog-title" style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:6px; border-bottom-color:rgb(222,223,225); border-bottom-width:1px; border-bottom-style:solid; clear:both"><span style="color:rgb(51,102,153); line-height:36px">9. IOException异常类的子类</span></p></h2></div><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial,宋体; line-height:24px">1.public class  EOFException :<br>   非正常到达文件尾或输入流尾时,抛出这种类型的异常。<br>2.public class FileNotFoundException:<br>   当文件找不到时,抛出的异常。<br>3.public class InterruptedIOException:<br>   当I/O操作被中断时,抛出这种类型的异常。<br></span></p><div><span style="font-family:Arial,宋体; line-height:24px"><br></span></div>   </div><!-- Baidu Button BEGIN --><div class="bdsharebuttonbox tracking-ad bdshare-button-style0-16" style="float: right;" data-mod="popu_172" data-bd-bind="1480835278044"><a href="#" class="bds_more" data-cmd="more" style="background-position:0 0 !important; background-image: url(http://bdimg.share.baidu.com/static/api/img/share/icons_0_16.png?v=d754dcc0.png) !important" target="_blank"></a><a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间" style="background-position:0 -52px !important" target="_blank"></a><a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博" style="background-position:0 -104px !important" target="_blank"></a><a href="#" class="bds_tqq" data-cmd="tqq" title="分享到腾讯微博" style="background-position:0 -260px !important" target="_blank"></a><a href="#" class="bds_renren" data-cmd="renren" title="分享到人人网" style="background-position:0 -208px !important" target="_blank"></a><a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信" style="background-position:0 -1612px !important" target="_blank"></a></div><script>window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdMini": "1", "bdMiniList": false, "bdPic": "", "bdStyle": "0", "bdSize": "16" }, "share": {} }; with (document) 0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=' + ~(-new Date() / 36e5)];</script><!-- Baidu Button END -->   <link rel="stylesheet" href="http://static.blog.csdn.net/css/blog_detail.css">    <!--172.16.140.12--><!-- Baidu Button BEGIN --><script type="text/javascript" id="bdshare_js" data="type=tools&uid=1536434" src="http://bdimg.share.baidu.com/static/js/bds_s_v2.js?cdnversion=411344"></script><script type="text/javascript">    document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000)</script><!-- Baidu Button END -->         <div id="digg" articleid="25892667">            <dl id="btnDigg" class="digg digg_enable" onclick="btndigga();">                                <dt>顶</dt>                <dd>8</dd>            </dl>                                     <dl id="btnBury" class="digg digg_enable" onclick="btnburya();">                                <dt>踩</dt>                <dd>1</dd>                           </dl>                    </div>     <div class="tracking-ad" data-mod="popu_222"><a href="javascript:void(0);" target="_blank"> </a>   </div>    <div class="tracking-ad" data-mod="popu_223"> <a href="javascript:void(0);" target="_blank"> </a></div>    <script type="text/javascript">                function btndigga() {                    $(".tracking-ad[data-mod='popu_222'] a").click();                }                function btnburya() {                    $(".tracking-ad[data-mod='popu_223'] a").click();                }            </script>   <ul class="article_next_prev">                <li class="prev_article"><span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_shangyipian']);location.href='/liuxiaogangqq/article/details/21940213';">上一篇</span><a href="/liuxiaogangqq/article/details/21940213" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_shangyipian'])">jquery easyui pagination分页带条件查询不能返回首页的问题</a></li>                <li class="next_article"><span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_xiayipian']);location.href='/liuxiaogangqq/article/details/38657333';">下一篇</span><a href="/liuxiaogangqq/article/details/38657333" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_xiayipian'])">Error: cannot call methods on draggable prior to initialization; attempted to call</a></li>    </ul>    <div style="clear:both; height:10px;"></div>        <div class="similar_article" style="">                <h4>我的同类文章</h4>                <div class="similar_c" style="margin:20px 0px 0px 0px">                    <div class="similar_c_t">                                <label class="similar_cur">                                    <span style="cursor:pointer" onclick="GetCategoryArticles('1160837','liuxiaogangqq','foot','25892667');">java类<em>(50)</em></span>                                </label>                    </div>                                       <div class="similar_wrap tracking-ad" data-mod="popu_141" style="max-height:195px;">                        <a href="http://blog.csdn.net" style="display:none" target="_blank">http://blog.csdn.net</a>                        <ul class="similar_list fl"><li><em>•</em><a href="http://blog.csdn.net/liuxiaogangqq/article/details/51699511" id="foot_aritcle_51699511undefined9483076118886749" target="_blank" title="win10 驱动程序无法使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:Could not generate DH keypair">win10 驱动程序无法使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:Could not generate DH keypair</a><span>2016-06-17</span><label><i>阅读</i><b>4581</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/liuxiaogangqq/article/details/49999445" id="foot_aritcle_49999445undefined10589326417933176" target="_blank" title="java swt多线程问题的解决现场(一点一点耐心看)">java swt多线程问题的解决现场(一点一点耐心看)</a><span>2015-11-23</span><label><i>阅读</i><b>567</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/liuxiaogangqq/article/details/49614437" id="foot_aritcle_49614437undefined18098418492986545" target="_blank" title="启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”">启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”</a><span>2015-11-03</span><label><i>阅读</i><b>228</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/liuxiaogangqq/article/details/48138921" id="foot_aritcle_48138921undefined3669729181649666" target="_blank" title="关于JSONArray明明添加add JSONObject却为空">关于JSONArray明明添加add JSONObject却为空</a><span>2015-08-31</span><label><i>阅读</i><b>1236</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/liuxiaogangqq/article/details/42507441" id="foot_aritcle_42507441undefined16775864313862066" target="_blank" title="java截取文件上传或下载的文件名及后缀">java截取文件上传或下载的文件名及后缀</a><span>2015-01-07</span><label><i>阅读</i><b>1570</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/liuxiaogangqq/article/details/41866021" id="foot_aritcle_41866021undefined5026633839238881" target="_blank" title="Java内存分配的原理详解">Java内存分配的原理详解</a><span>2014-12-11</span><label><i>阅读</i><b>511</b></label></li> </ul>                        <ul class="similar_list fr"><li><em>•</em><a href="http://blog.csdn.net/liuxiaogangqq/article/details/51691109" id="foot_aritcle_51691109undefined1219852773680512" target="_blank" title="Unable to read entire header; 336 bytes read; expected 512 bytes">Unable to read entire header; 336 bytes read; expected 512 bytes</a><span>2016-06-16</span><label><i>阅读</i><b>305</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/liuxiaogangqq/article/details/49664913" id="foot_aritcle_49664913undefined9021021657583108" target="_blank" title="java增删改查JDBC封装类">java增删改查JDBC封装类</a><span>2015-11-05</span><label><i>阅读</i><b>824</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/liuxiaogangqq/article/details/48465905" id="foot_aritcle_48465905undefined4700989347316211" target="_blank" title="正确理解ThreadLocal">正确理解ThreadLocal</a><span>2015-09-15</span><label><i>阅读</i><b>207</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/liuxiaogangqq/article/details/48025681" id="foot_aritcle_48025681undefined036619319888830715" target="_blank" title="关于jquery easy treegrid 分级跳级显示的问题">关于jquery easy treegrid 分级跳级显示的问题</a><span>2015-08-27</span><label><i>阅读</i><b>541</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/liuxiaogangqq/article/details/42297001" id="foot_aritcle_42297001undefined26400180236545423" target="_blank" title="easyui datagrid自适应浏览器宽度">easyui datagrid自适应浏览器宽度</a><span>2014-12-31</span><label><i>阅读</i><b>3813</b></label></li> </ul>                    <a href="http://blog.csdn.net/liuxiaogangqq/article/category/1160837" class="MoreArticle">更多文章</a></div>                </div>            </div>        <script type="text/javascript">        $(function () {            GetCategoryArticles('1160837', 'liuxiaogangqq','foot','25892667');        });    </script>      </div>

0 0
原创粉丝点击