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

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Java讀取大文件方法

2019-11-14 15:29:45
字體:
供稿:網(wǎng)友

需求:實(shí)際開發(fā)中讀取文本文件的需求還是很多,如讀取兩個(gè)系統(tǒng)之間FTP發(fā)送文件,讀取后保存到數(shù)據(jù)庫中或日志文件的數(shù)據(jù)庫中保存等。

 

為了測試首先利用數(shù)據(jù)庫SQL生成大數(shù)據(jù)文件。

規(guī)則是 編號|姓名|手機(jī)號,如 10|張10|13900000010

利用下面語句可以生成1,000,000條數(shù)據(jù)。生成的數(shù)據(jù)保存到 D://test//customer_info.txt 文件里面。

SELECT LEVEL||'|'||''||LEVEL||'|'||(13900000000+LEVEL)  FROM DUAL CONNECT BY LEVEL < 1000000;

利用java程序讀取剛生成的文件。 

實(shí)現(xiàn)如下:

package com.test.common.util;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.Scanner;import org.apache.commons.io.FileUtils;import org.apache.commons.io.LineIterator;public class HandleTextFile {        // 使用commons-io.jar包的FileUtils的類進(jìn)行讀取    public static void readTxtFileByFileUtils(String fileName) {        File file = new File(fileName);        try {            LineIterator lineIterator = FileUtils.lineIterator(file, "UTF-8");            while (lineIterator.hasNext()) {                String line = lineIterator.nextLine();                System.out.PRintln(line);            }        } catch (IOException e) {            e.printStackTrace();        }    }        // 使用Scanner進(jìn)行讀取    public static void readTxtByScanner(String fileName) {        FileInputStream fileInputStream = null;         Scanner scanner = null;                try {            fileInputStream = new FileInputStream(fileName);            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();            }        }            }    // 使用cache進(jìn)行讀取    public static void readTxtByStringBuffer(String fileName) throws IOException {        File file = new File(fileName);                BufferedReader reader = null;                try {            reader = new BufferedReader(new FileReader(file), 10 * 1024 * 1024);            String stringMsg = null;            while ((stringMsg = reader.readLine()) != null) {                System.out.println(stringMsg);            }            reader.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        }     }        public static void main(String[] args) {        try {            HandleTextFile.readTxtByStringBuffer("D://test//customer_info.txt");        } catch (IOException e) {            e.printStackTrace();        }    }}

 參考文件:讀取大文件性能測試


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 河源市| 高陵县| 鄂托克旗| 宜川县| 北流市| 高台县| 石家庄市| 教育| 天等县| 莱西市| 米泉市| 宜城市| 婺源县| 乌鲁木齐县| 三亚市| 册亨县| 晋中市| 平陆县| 土默特左旗| 固原市| 杭锦旗| 宁波市| 扬中市| 宕昌县| 莒南县| 阜阳市| 陇南市| 苍山县| 横山县| 万安县| 蒲江县| 晋城| 红原县| 阳高县| 苏州市| 清流县| 瓦房店市| 邯郸市| 鲁甸县| 永泰县| 广西|