qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Java中復制文件的效率測試

           項目中用到了圖片的上傳,對于上傳過程中,圖片的復制項目組用了兩種方法,一種是以java的IO流,另外一種是用org.apache.commons.io.FileUtils的工具類,今天我測試了一下,單純考慮文件的復制效率,apache的工具類的效率是普通io流讀取的3倍。
            下面是測試源碼:
          public class ImageTest {
          public static void main(String[] args) throws IOException {
          IOTest();
          }
          public static void fileUtilsTest() throws IOException {
          // 趨近13毫秒后,就保持這個數值
          File srcFile = new File("D:/1.apk");
          File destFile = new File("E:/2.apk");
          long sum = 0;
          for (int i = 0; i < 10; i++) {
          long startTime = System.currentTimeMillis();
          FileUtils.copyFile(srcFile, destFile);
          long endTime = System.currentTimeMillis();
          sum += (endTime - startTime);
          }
          long average = sum / 10;
          System.out.println("耗時" + average + "豪秒");
          }
          public static void IOTest() throws IOException {
          // 50毫秒
          File srcFile = new File("D:/1.apk");
          File destFile = new File("E:/2.apk");
          long sum = 0;
          for (int i = 0; i < 10; i++) {
          long startTime = System.currentTimeMillis();
          InputStream is = new FileInputStream(srcFile);
          // 把圖片寫入到上面設置的路徑里
          OutputStream os = new FileOutputStream(destFile);
          byte[] buffer = new byte[400];
          int length = 0;
          while ((length = is.read(buffer)) > 0) {
          os.write(buffer, 0, length);
          }
          is.close();
          os.close();
          long endTime = System.currentTimeMillis();
          sum += (endTime - startTime);
          }
          long average = sum / 10;
          System.out.println("耗時" + average + "豪秒");
          }
          }

          posted on 2014-01-24 16:06 順其自然EVO 閱讀(189) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2014年1月>
          2930311234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 弥渡县| 中江县| 桐乡市| 连州市| 枞阳县| 宁陵县| 炉霍县| 固阳县| 扎囊县| 日土县| 西安市| 阿合奇县| 兴安盟| 鲜城| 温州市| 年辖:市辖区| 周宁县| 高州市| 岱山县| 普兰店市| 分宜县| 饶阳县| 镇康县| 东平县| 蛟河市| 工布江达县| 雅安市| 神农架林区| 玉环县| 内江市| 新巴尔虎右旗| 娄烦县| 九江县| 平果县| 三江| 鄂尔多斯市| 太和县| 洪江市| 黄冈市| 安仁县| 广元市|