<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

    [原創]JAVA讀取文件或是數據流的源代碼--涵蓋了多種形式

        --sunfruit

        java讀取文件或是文件流的代碼,涵蓋了讀取jar文件中的文件流,網絡文件流等,有些讀取方式為了防止編碼轉換帶來的問題,采取了動態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() {
      }
      /**
       * 讀取源文件內容
       * @param filename String 文件路徑
       * @throws IOException
       * @return byte[] 文件內容
       */
      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;

      }

      /**
       * 將數據寫入文件
       * @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;
      }
     
      /**
       * 讀取網絡流,為了防止中文的問題,在讀取過程中沒有進行編碼轉換,而且采取了動態的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 閱讀(6076) 評論(1)  編輯  收藏 所屬分類: JAVA SE & EE

    評論

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

    if(n<bytes.length)
    break;

    這句是不是會造成讀取的數據不全?  回復  更多評論   

    主站蜘蛛池模板: 97视频免费观看2区| 精品国产香蕉伊思人在线在线亚洲一区二区| 国产亚洲精品va在线| CAOPORM国产精品视频免费| 亚洲不卡AV影片在线播放| 特级毛片免费观看视频| 亚洲精品国产成人影院| 中文字幕免费播放| 久久精品国产96精品亚洲| 一级毛片**不卡免费播| 亚洲最大成人网色| 免费视频爱爱太爽了| 亚洲kkk4444在线观看| 日韩一区二区三区免费体验| 精品亚洲福利一区二区| 亚洲精品无码AV中文字幕电影网站| 一级毛片aa高清免费观看| 精品亚洲永久免费精品| 日韩插啊免费视频在线观看| 亚洲免费网站在线观看| 免费a级毛片18以上观看精品| 无码日韩人妻AV一区免费l| 国外亚洲成AV人片在线观看| 无码免费一区二区三区免费播放| 亚洲视频在线不卡| 国产大片91精品免费看3| 最近的2019免费中文字幕| 91亚洲国产成人精品下载| 大学生美女毛片免费视频| 一级A毛片免费观看久久精品 | 日本免费一区尤物| 一级毛片人与动免费观看| 亚洲高清日韩精品第一区| 好吊妞788免费视频播放| A级毛片成人网站免费看| 久久亚洲AV无码精品色午夜| 色播在线永久免费视频| 中文在线观看免费网站| 国产精品亚洲一区二区麻豆| 中文字幕亚洲第一| 性xxxx视频播放免费|