您好,登錄后才能下訂單哦!
通過(guò)使用java提供的io,scanner類(lèi),apache提供的api處理大文件數(shù)據(jù)性能分析比較,代碼如下:
package test;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.util.Random;
import java.util.Scanner;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.junit.Test;
public class TestFile {
//@Test
//造數(shù)據(jù),測(cè)試下面各個(gè)方法讀取數(shù)據(jù)性能
public void makeFile() throws IOException
{
File file = new File("D:\\phone.txt");
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
//2百萬(wàn)
for(int i=0; i < 2000000; i++)
{
bw.write(bulidPhone());
bw.newLine();
}
bw.close();
os.close();
}
//生成字符串
private String bulidPhone()
{
Long lo = new Random().nextLong();
return String.valueOf(lo);
}
/**
* @Title: readTxt1
* @Description: 使用常規(guī)的jdk的io解析輸出文件數(shù)據(jù)
* @throws IOException
*/
@Test
public void readTxt1() throws IOException
{
long start = System.currentTimeMillis();
File file = new File("D:\\phone.txt");
Reader in = new FileReader(file);
BufferedReader br = new BufferedReader(in);
while(br.ready())
{
//System.out.println(br.readLine());
br.readLine();
}
in.close();
br.close();
long end = System.currentTimeMillis();
System.out.println("readTxt1方法,使用內(nèi)存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用時(shí)間毫秒="+(end-start));
}
/**
* @Title: readTxt2
* @Description: 使用Scanner掃面文件解析文件數(shù)據(jù)
* @throws IOException
*/
@Test
public void readTxt2() throws IOException
{
long start = System.currentTimeMillis();
File file = new File("D:\\phone.txt");
InputStream is = new FileInputStream(file);
Scanner scan = new Scanner(is,"UTF-8");
while(scan.hasNextLine())
{
//System.out.println(scan.nextLine());
scan.nextLine();
//scan.next();
}
is.close();
scan.close();
long end = System.currentTimeMillis();
System.out.println("readTxt2方法,使用內(nèi)存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用時(shí)間毫秒="+(end-start));
}
/**
* @Title: readTxt3
* @Description: 使用org.apache.commons.io.FileUtils,apache工具類(lèi)解析文件
* @throws IOException
*/
@Test
public void readTxt3() throws IOException
{
long start = System.currentTimeMillis();
File file = new File("D:\\phone.txt");
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
while(it.hasNext())
{
it.next();
}
it.close();
long end = System.currentTimeMillis();
System.out.println("readTxt3方法,使用內(nèi)存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用時(shí)間毫秒="+(end-start));
}
}
運(yùn)行結(jié)果如下:
通過(guò)分析比較: 獲取【下載地址】
1.apache的api處理時(shí)間最短,但是消耗的內(nèi)存比jdk的io多。
2.scanner類(lèi)表現(xiàn)的最差,銷(xiāo)售內(nèi)存高,時(shí)間久。
3.傳統(tǒng)的jdk的io處理時(shí)間稍長(zhǎng),內(nèi)存消耗低。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。