qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請(qǐng)?jiān)L問(wèn) http://qaseven.github.io/

          Java中復(fù)制文件的效率測(cè)試

           項(xiàng)目中用到了圖片的上傳,對(duì)于上傳過(guò)程中,圖片的復(fù)制項(xiàng)目組用了兩種方法,一種是以java的IO流,另外一種是用org.apache.commons.io.FileUtils的工具類,今天我測(cè)試了一下,單純考慮文件的復(fù)制效率,apache的工具類的效率是普通io流讀取的3倍。
            下面是測(cè)試源碼:
          public class ImageTest {
          public static void main(String[] args) throws IOException {
          IOTest();
          }
          public static void fileUtilsTest() throws IOException {
          // 趨近13毫秒后,就保持這個(gè)數(shù)值
          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("耗時(shí)" + 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);
          // 把圖片寫(xiě)入到上面設(shè)置的路徑里
          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("耗時(shí)" + average + "豪秒");
          }
          }

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


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2014年1月>
          2930311234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 安康市| 留坝县| 枣庄市| 吉林市| 义马市| 砀山县| 明星| 承德市| 民和| 灵宝市| 美姑县| 平顺县| 林甸县| 商河县| 吕梁市| 桃园县| 报价| 贡觉县| 南宁市| 高平市| 丰顺县| 潼关县| 永和县| 博罗县| 无锡市| 白城市| 明水县| 托里县| 临颍县| 朝阳县| 吴旗县| 调兵山市| 利川市| 新郑市| 报价| 七台河市| 泸州市| 安图县| 雷州市| 含山县| 武夷山市|