<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    sunfruit[請訪問http://www.fruitres.cn]

    --我相信JAVA能走得更遠 QQ:316228067

    [原創(chuàng)]JAVA讀取文件或是數(shù)據(jù)流的源代碼--涵蓋了多種形式

        --sunfruit

        java讀取文件或是文件流的代碼,涵蓋了讀取jar文件中的文件流,網(wǎng)絡(luò)文件流等,有些讀取方式為了防止編碼轉(zhuǎn)換帶來的問題,采取了動態(tài)byte[]的方式讀取,源碼如下

    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.BufferedOutputStream;
    import java.io.IOException;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;

    public class Util {

      public Util() {
      }
      /**
       * 讀取源文件內(nèi)容
       * @param filename String 文件路徑
       * @throws IOException
       * @return byte[] 文件內(nèi)容
       */
      public static byte[] readFile(String filename) throws IOException {

        File file =new File(filename);
        if(filename==null || filename.equals(""))
        {
          throw new NullPointerException("無效的文件路徑");
        }
        long len = file.length();
        byte[] bytes = new byte[(int)len];

        BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(file));
        int r = bufferedInputStream.read( bytes );
        if (r != len)
          throw new IOException("讀取文件不正確");
        bufferedInputStream.close();

        return bytes;

      }

      /**
       * 將數(shù)據(jù)寫入文件
       * @param data byte[]
       * @throws IOException
       */
      public static void writeFile(byte[] data,String filename) throws IOException {
        File file =new File(filename);
        file.getParentFile().mkdirs();
        BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(file));
        bufferedOutputStream.write(data);
        bufferedOutputStream.close();

      }

      /**
       * 從jar文件里讀取class
       * @param filename String
       * @throws IOException
       * @return byte[]
       */
      public byte[] readFileJar(String filename) throws IOException {
        BufferedInputStream bufferedInputStream=new BufferedInputStream(getClass().getResource(filename).openStream());
        int len=bufferedInputStream.available();
        byte[] bytes=new byte[len];
        int r=bufferedInputStream.read(bytes);
        if(len!=r)
        {
          bytes=null;
          throw new IOException("讀取文件不正確");
        }
        bufferedInputStream.close();
        return bytes;
      }
     
      /**
       * 讀取網(wǎng)絡(luò)流,為了防止中文的問題,在讀取過程中沒有進行編碼轉(zhuǎn)換,而且采取了動態(tài)的byte[]的方式獲得所有的byte返回
       * @param bufferedInputStream BufferedInputStream
       * @throws IOException
       * @return byte[]
       */
      public byte[] readUrlStream(BufferedInputStream bufferedInputStream) throws IOException {
        byte[] bytes = new byte[100];
        byte[] bytecount=null;
        int n=0;
        int ilength=0;
        while((n=bufferedInputStream.read(bytes))>=0)
        {
          if(bytecount!=null)
            ilength=bytecount.length;
          byte[] tempbyte=new byte[ilength+n];
          if(bytecount!=null)
          {
            System.arraycopy(bytecount,0,tempbyte,0,ilength);
          }

          System.arraycopy(bytes,0,tempbyte,ilength,n);
          bytecount=tempbyte;

          if(n<bytes.length)
            break;
        }
        return bytecount;
      }

    }

    posted on 2006-02-19 18:04 sunfruit 閱讀(6069) 評論(1)  編輯  收藏 所屬分類: JAVA SE & EE

    評論

    # re: [原創(chuàng)]JAVA讀取文件或是數(shù)據(jù)流的源代碼--涵蓋了多種形式 2008-06-17 23:02 guozl

    if(n<bytes.length)
    break;

    這句是不是會造成讀取的數(shù)據(jù)不全?  回復(fù)  更多評論   

    主站蜘蛛池模板: 免费毛片在线播放| 亚洲熟妇无码乱子AV电影| 视频一区二区三区免费观看| 亚洲成a人一区二区三区| 久久99热精品免费观看牛牛| 2020久久精品亚洲热综合一本 | 日韩亚洲国产高清免费视频| 亚洲国产美女精品久久久| 国产成人亚洲精品狼色在线| 国产人成免费视频网站| 天堂亚洲免费视频| 亚洲国产成人资源在线软件| 亚洲Aⅴ无码一区二区二三区软件| 亚洲免费在线视频| 国产亚洲综合精品一区二区三区| 亚洲激情中文字幕| 免费在线观看的黄色网址| 最近免费字幕中文大全视频| 三年片在线观看免费观看大全中国 | 浮力影院第一页小视频国产在线观看免费 | 青青视频免费在线| 亚洲综合色一区二区三区小说| 四虎免费永久在线播放| 久久精品一本到99热免费| 一级做a爱片特黄在线观看免费看| 亚洲一区精品视频在线| 亚洲精品无码永久在线观看你懂的| 在线播放高清国语自产拍免费| 中文字幕乱码一区二区免费| 国产精品亚洲а∨天堂2021| 亚洲人成毛片线播放| 亚洲AV日韩精品久久久久久| 亚洲国产成人精品女人久久久| 两个人的视频高清在线观看免费| 精品国产麻豆免费人成网站| 一级女人18片毛片免费视频| 亚洲精品国产精品| 国产成人亚洲精品| 亚洲永久中文字幕在线| 亚洲精品无码不卡| 亚洲国产精品无码专区|