隨筆 - 0, 文章 - 75, 評(píng)論 - 0, 引用 - 0
          數(shù)據(jù)加載中……

          IO

          讀寫文件:



          字節(jié)流——圖片、mp3、影音


          字符流——文本


          首先介紹字符流:


          public static void main(String[] args) throws Exception
          {
          Reader rd=null; //讀
          Writer
          wt=null; //寫
          try
          {
          rd=new FileReader("pet.template"); //讀取pet.template文件內(nèi)容
          char[]
          cr = new char[1];
          int
          length=rd.read(cr); //取得內(nèi)容長(zhǎng)度
          StringBuffer str=new
          StringBuffer();
          while(length!=-1){ //長(zhǎng)度為-1則無(wú)數(shù)據(jù)
          str.append(cr);
          length=rd.read(cr);
          }
          wt=new
          FileWriter("f:\\a.txt"); //在f盤創(chuàng)建a.txt
          wt.write(str.toString()); //并寫入數(shù)據(jù)
          System.out.println(str.toString());
          }
          catch (Exception e) {
          }finally{
          try
          {
          if(rd!=null)
          rd.close();
          //一定要關(guān)閉文件流
          if(wt!=null)
          wt.close();
          //..
          } catch
          (Exception e2) {
          }
          }



          字節(jié)流:


          InputStream imput=new
          FileInputStream("Sunset.jpg"); //讀取Sunset.jpg圖片
          FileOutputStream
          output=new FileOutputStream("f:\\a.jpg"); //要輸出的位置
          byte[]
          img=new byte[imput.available()];
          int
          length=imput.read(img); //讀取img長(zhǎng)度
          output.write(img);
          //把圖像寫入f:\a.jpg
          imput.close();
          output.close();

          posted on 2012-04-22 15:32 hantai 閱讀(1257) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 石棉县| 微山县| 东乌| 泗阳县| 海门市| 石狮市| 绿春县| 棋牌| 咸丰县| 阳西县| 九寨沟县| 当涂县| 宾阳县| 西宁市| 易门县| 南充市| 玉林市| 梁山县| 饶河县| 阜宁县| 区。| 商南县| 长顺县| 广州市| 菏泽市| 娱乐| 库尔勒市| 陆川县| 布尔津县| 黄冈市| 武乡县| 马鞍山市| 康马县| 太谷县| 沭阳县| 龙州县| 天峨县| 大竹县| 美姑县| 韩城市| 邯郸县|