java高效讀大文件(csv,text)的幾種處理方式
前言
當(dāng)我們在處理一個2G或者更大的文件數(shù)據(jù)時,往往是很耗系統(tǒng)性能的,處理不當(dāng)可能造成系統(tǒng)崩潰。接下來介紹四種讀取大文件的方式,以及每種方式的資源的調(diào)用情況。
方法1:Guava讀取
由于我是用的windows系統(tǒng),在 第一次測試時用了2G的文件,最后在讀取文件時,讀取了好久,最后報錯堆內(nèi)存溢出(由此可知,這種方式是基于內(nèi)存進(jìn)行一次性讀取整個文件,文件越大,占用的資源越多)。然后 選用了一個624MB的csv文件進(jìn)行測試。
代碼示例如下:
@org.junit.Test
public void testGuavaReadFile() throws IOException {
//本次測試的這個outPut.csv文件的大小是624MB
String filePath = "D:\\outPut.csv";
File file = new File(filePath);
Long startTime = System.currentTimeMillis();
//進(jìn)行文件的讀取,返回結(jié)果:每行數(shù)據(jù)都是一個string字符串
List<String> lines = Files.readLines(file, Charsets.UTF_8);
for (String line : lines) {
// 在這里添加對每行數(shù)據(jù)的處理邏輯
System.out.println("Processing line: " + line);
}
Long endTime = System.currentTimeMillis();
long consume = (endTime - startTime)/1000;
System.out.println("************總共耗時:"+consume+"秒*****************");
}監(jiān)控結(jié)果如下:

從上圖可以看到:
時間消耗:20秒堆內(nèi)存:最高2.5GCPU消耗:最高50%
方式2:Apache Commons IO普通方式
代碼如下:
@org.junit.Test
public void TestCommonsIoReadFile() throws IOException {
//本次測試的這個outPut.csv文件的大小是624MB
String filePath = "D:\\outPut.csv";
File file = new File(filePath);
Long startTime = System.currentTimeMillis();
//Apache Commons IO普通方式讀取文件
List<String> lines = FileUtils.readLines(file, "UTF-8");
for (String line : lines) {
// 在這里添加對每行數(shù)據(jù)的處理邏輯
System.out.println("Processing line: " + line);
}
Long endTime = System.currentTimeMillis();
long consume = (endTime - startTime)/1000;
System.out.println("************CommonsIo方式總共耗時:"+consume+"秒*****************");
}運行結(jié)果:

從上圖可以看出:
時間消耗:17秒CPU消耗:最高50%,平穩(wěn)運行25%左右
方式3:java文件流
代碼如下:
@org.junit.Test
public void TestJavaIoReadFile() throws IOException {
//本次測試的這個outPut.csv文件的大小是624MB
String filePath = "D:\\outPut.csv";
Long startTime = System.currentTimeMillis();
FileInputStream inputStream = null;
Scanner scanner = null;
try {
inputStream = new FileInputStream(filePath);
scanner = new Scanner(inputStream, "UTF-8");
while (scanner.hasNextLine()) {
//逐行讀取文件內(nèi)容
String line = scanner.nextLine();
System.out.println(line);
}
if (scanner.ioException() != null) {
throw scanner.ioException();
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (scanner != null) {
scanner.close();
}
}
Long endTime = System.currentTimeMillis();
long consume = (endTime - startTime)/1000;
System.out.println("************CommonsIo方式總共耗時:"+consume+"秒*****************");
}運行結(jié)果:

從上圖可以看出:
時間消耗:32秒,增加了一倍堆內(nèi)存:最高1G,少了一半CPU消耗:平穩(wěn)運行25%左右
方式4:Apache Commons IO流
代碼如下:
@org.junit.Test
public void TestApacheCommonsIOReanFile() throws IOException {
//本次測試的這個outPut.csv文件的大小是624MB
String filePath = "D:\\outPut.csv";
Long startTime = System.currentTimeMillis();
LineIterator lineIterator = null;
try {
lineIterator = FileUtils.lineIterator(new File(filePath), "UTF-8");
while (lineIterator.hasNext()) {
String line = lineIterator.nextLine();
System.out.println(line);
}
} finally {
LineIterator.closeQuietly(lineIterator);
}
Long endTime = System.currentTimeMillis();
long consume = (endTime - startTime)/1000;
System.out.println("************CommonsIo方式總共耗時:"+consume+"秒*****************");
}運行結(jié)果:

從上圖可以看出:
時間消耗:16秒,最低堆內(nèi)存:最高650M,少了一半
總結(jié) :
從四種方式可以看出,性能最優(yōu)的是Apache Commons IO流 對大文件的處理。
我們可以得出一個結(jié)論,如果我們想要讀取一個大文件,選擇了錯誤的方式,就有可能極大地占用我的內(nèi)存和CPU,當(dāng)文件特別大時,會造成意向不到的問題。
因此為了去解決這樣的問題,有四種常見的讀取大文件的方式。通過分析對比,發(fā)現(xiàn),Apache Commons IO流是最高效的一種方式。
到此這篇關(guān)于java高效讀大文件(csv,text)的幾種處理方式的文章就介紹到這了,更多相關(guān)java高效讀大文件csv,text內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring中12種@Transactional的失效場景(小結(jié))
日常我們進(jìn)行業(yè)務(wù)開發(fā)時,基本上使用的都是聲明式事務(wù),即為使用@Transactional注解的方式,本文主要介紹了spring中12種@Transactional的失效場景,感興趣的小伙伴們可以參考一下2022-01-01
SpringCloud啟動eureka server后,沒報錯卻不能訪問管理頁面(404問題)
這篇文章主要介紹了SpringCloud啟動eureka server后,沒報錯卻不能訪問管理頁面(404問題),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
Spring中Bean的創(chuàng)建流程詳細(xì)解讀
這篇文章主要介紹了Spring中Bean的創(chuàng)建流程詳細(xì)解讀,Spring 中創(chuàng)建 Bean ,是通過調(diào)用 GetBean 方法來觸發(fā)的,所以,我們會從這個方法開始,需要的朋友可以參考下2023-10-10
spring mvc中的@PathVariable獲得請求url中的動態(tài)參數(shù)
本文主要介紹了spring mvc中的@PathVariable獲得請求url中的動態(tài)參數(shù)的代碼。具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02
springboot使用redis實現(xiàn)從配置到實戰(zhàn)
本文主要介紹了springboot使用redis ,采用的是RedisTemplate的形式,還有一種采用spring支持的注解進(jìn)行訪問緩存,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08

