国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

讀取大文件性能測試

2019-11-14 15:34:31
字體:
來源:轉載
供稿:網友

讀取大文件的方法這里有三種,

第一種,使用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進行讀?。?/h2>
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不相上下,我這邊暫時沒有更大的文件進行比較如果,有更大的文件,歡迎進行測試比較。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 合肥市| 平湖市| 恩平市| 嘉兴市| 安岳县| 饶平县| 文山县| 莱芜市| 灵丘县| 株洲县| 和硕县| 东平县| 洛隆县| 丽江市| 根河市| 东台市| 阳新县| 南皮县| 伊宁市| 广宁县| 盈江县| 田阳县| 延寿县| 嘉义县| 崇仁县| 乌鲁木齐县| 桃源县| 东丽区| 蓬溪县| 环江| 台南县| 车致| 石家庄市| 莆田市| 隆德县| 揭阳市| 阿克苏市| 神农架林区| 原阳县| 弥渡县| 红河县|