<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;

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

    主站蜘蛛池模板: 特级无码毛片免费视频 | 免费国产精品视频| 亚洲精品美女久久久久| 久久永久免费人妻精品| 亚洲高清专区日韩精品| 在线观看免费无码专区| 亚洲国产精品福利片在线观看| 你好老叔电影观看免费| 国产精品亚洲一区二区三区在线| 久久国产乱子伦精品免费午夜| 中文字幕无码精品亚洲资源网| 很黄很污的网站免费| 精品国产亚洲一区二区三区| 免费无码又爽又刺激高潮软件| 亚洲色婷婷综合久久| 亚洲视频在线免费观看| 亚洲欧洲日产国产最新| 24小时日本在线www免费的| 亚洲国产美女精品久久久| 亚洲 小说区 图片区 都市| ssswww日本免费网站片| 亚洲精品国产精品乱码不卡√| 久久久久国产精品免费看| 亚洲成人黄色在线| 日韩在线天堂免费观看 | 亚洲日韩精品一区二区三区无码 | 亚洲一区二区三区日本久久九| 97久久免费视频| 亚洲色偷精品一区二区三区| 国产精品美女久久久免费 | 在线a亚洲老鸭窝天堂av高清| 日韩高清在线免费观看| 成人无码视频97免费| 亚洲国产日韩在线成人蜜芽| 免费看香港一级毛片| 99免费在线视频| 四虎必出精品亚洲高清| 成人午夜免费福利视频| 三级片免费观看久久| 亚洲精品中文字幕乱码| 免费一级毛片在线播放不收费|