星空网 > 软件开发 > Java

读取大文件性能测试

读取大文件的方法这里有三种,

第一种,使用commons-io的FileUtils的类进行读取

第二种,使用Scanner进行读取

第三种,使用cache进行读取

读取文件大小:102M

使用commons-io的FileUtils类进行读取

public static void testReadFile() {    try {      LineIterator lineIterator = FileUtils.lineIterator(new File("D:/test.log"), "UTF-8");      while (lineIterator.hasNext()) {        String line = lineIterator.nextLine();        System.out.println(line);      }    } catch (IOException e) {      e.printStackTrace();    }  }

 

读取时间在8秒左右

使用Scanner进行读取:

public static void testScannerReadFile() {    FileInputStream fileInputStream = null;    Scanner scanner = null;    try {      fileInputStream = new FileInputStream("D:/test.log");      scanner = new Scanner(fileInputStream, "UTF-8");      while (scanner.hasNext()) {        String line = scanner.nextLine();        System.out.println(line);      }    } catch (FileNotFoundException e) {      e.printStackTrace();    } finally {      if (fileInputStream != null) {        try {          fileInputStream.close();        } catch (IOException e) {          e.printStackTrace();        }      }      if (scanner != null) {        scanner.close();      }    }  }

读取时间在10秒左右

使用cache读取

public static void readCache() {    String filename = "D:/test.log";    File file = new File(filename);    BufferedReader reader = null;    try {      reader = new BufferedReader(new FileReader(file), 10 * 1024 * 1024);  //读大文件 设置缓存      String tempString = null;      while ((tempString = reader.readLine()) != null) {        System.out.println(tempString);      }      reader.close();    } catch (IOException e) {      e.printStackTrace();    } finally {      if (reader != null) {        try {          reader.close();        } catch (IOException e) {          e.printStackTrace();        }      }    }  }

读取时间在8秒左右,与commons-io的FileUtils不相上下,我这边暂时没有更大的文件进行比较如果,有更大的文件,欢迎进行测试比较。




原标题:读取大文件性能测试

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

水下智能设备品牌「潜行创新」完成数千万元B+轮融资:https://www.kjdsnews.com/a/1723984.html
沙特网红营销,你需要知道的5件事:https://www.kjdsnews.com/a/1723985.html
E-bike出海品牌「大鱼智行」完成1亿元A轮融资:https://www.kjdsnews.com/a/1723986.html
TikTok和HubSpot联手,助力墨西哥B2B品牌获客:https://www.kjdsnews.com/a/1724926.html
[24-559]Keith律所代理Sungwon Hwang咖啡猫插画起诉!未提出TRO![24-cv-559]:https://www.kjdsnews.com/a/1724927.html
厨房机器人品牌Aniai获1200万美元A轮融资:https://www.kjdsnews.com/a/1724928.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流