<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 閱讀(6070) 評論(1)  編輯  收藏 所屬分類: JAVA SE & EE

    評論

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

    if(n<bytes.length)
    break;

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

    主站蜘蛛池模板: 最近中文字幕免费mv视频7| 免费人人潮人人爽一区二区| 国产在线精品一区免费香蕉| 免费国产精品视频| 黄网站色视频免费观看45分钟| 精品国产免费一区二区| 亚洲中文字幕久久精品无码A | 免费毛片在线视频| 亚洲短视频在线观看| aⅴ在线免费观看| 亚洲国产精品久久丫| 成年在线观看网站免费| 亚洲AV成人噜噜无码网站| 亚洲一级毛片免费观看| 亚洲国产综合精品中文第一| 日韩在线a视频免费播放| 亚洲精华国产精华精华液网站 | 国产乱子伦精品免费女| 国产精品亚洲а∨天堂2021| 亚洲国产婷婷综合在线精品| 九九九精品视频免费| 亚洲国产成人高清在线观看| 亚洲国产精品免费视频| 亚洲精品国产国语| 亚洲第一福利网站在线观看| 中文毛片无遮挡高清免费| 久久久久亚洲AV无码专区首| 成人免费视频网站www| 亚洲国产精品成人午夜在线观看| 免费国产综合视频在线看| a级毛片免费完整视频| 亚洲视频在线观看不卡| 永久在线毛片免费观看| 中文字幕无线码中文字幕免费| 亚洲国产成人片在线观看| 免费中文熟妇在线影片| 人妻免费久久久久久久了| 18gay台湾男同亚洲男同| 日本不卡在线观看免费v| 国产成人免费ā片在线观看老同学| 激情内射亚洲一区二区三区|