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

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

          導航

          <2006年2月>
          2930311234
          567891011
          12131415161718
          19202122232425
          2627281234
          567891011

          統計

          公告

          個人寫的作品會盡量附上代碼,大家使用發現問題就指出,交流第一嘛  QQ:316228067

          常用鏈接

          留言簿(13)

          隨筆分類(121)

          隨筆檔案(105)

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

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

          評論

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

          if(n<bytes.length)
          break;

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

          主站蜘蛛池模板: 梁河县| 富顺县| 南雄市| 澳门| 南华县| 屏东市| 锦屏县| 涞水县| 东至县| 平果县| 沁源县| 天长市| 长泰县| 泰和县| 孝昌县| 江西省| 黄梅县| 长汀县| 海兴县| 齐齐哈尔市| 庐江县| 桓台县| 安徽省| 栾城县| 临高县| 华蓥市| 邵阳市| 海城市| 灵武市| 武汉市| 镇江市| 蒙山县| 龙泉市| 雅安市| 定襄县| 鄂尔多斯市| 萍乡市| 崇阳县| 丽江市| 鸡西市| 绥棱县|