java 读取大数据文件,处理大数据文件性能比较?
来源:互联网 发布:淘宝网天猫电器电油汀 编辑:程序博客网 时间:2024/05/16 17:23
通过使用java提供的io,scanner类,apache提供的api处理大文件数据性能分析比较,代码如下:
- 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
- //造数据,测试下面各个方法读取数据性能
- 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百万
- 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: 使用常规的jdk的io解析输出文件数据
- * @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方法,使用内存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用时间毫秒="+(end-start));
- }
- /**
- * @Title: readTxt2
- * @Description: 使用Scanner扫面文件解析文件数据
- * @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方法,使用内存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用时间毫秒="+(end-start));
- }
- /**
- * @Title: readTxt3
- * @Description: 使用org.apache.commons.io.FileUtils,apache工具类解析文件
- * @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方法,使用内存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用时间毫秒="+(end-start));
- }
- }
运行结果如下:
通过分析比较:
1.apache的api处理时间最短,但是消耗的内存比jdk的io多。
2.scanner类表现的最差,销售内存高,时间久。
3.传统的jdk的io处理时间稍长,内存消耗低。
0 0
- java 读取大数据文件,处理大数据文件性能比较?
- 多线程读取大数据文件
- phpExcel 读取数据,大数据文件处理方案
- 多线程处理大数据文件
- 脚本处理大数据文件
- Java IO流读取大数据文件
- java生成随机大数据文件
- java:大数据文件写入,读取,分割,排序,合并
- Java 高效读取大数据文件—最优方法
- 多线程读取大数据文件(续)
- 读取大数据文件 以oracle为例
- 大数据文件导入MySql处理方式
- 如何处理大数据文件+录入数据
- python结合mongodb处理阿里大数据文件
- 大数据文件上传
- 二进制存储大数据文件
- 大数据文件分隔符
- Hive导入大数据文件
- session、cookie概念作用辨析
- 需要搞清楚的一些列表控件
- 下面的两种方法都能够将Sphere变为Cube 父节点元素
- JSP属性范围
- 2016年的美国虚拟经济策略的对全球的影响
- java 读取大数据文件,处理大数据文件性能比较?
- 子线程中却抛出异常:android.view.ViewRootImpl$CalledFromWrongThreadException
- 苹果应用上架被拒各种原因
- 使用CAShapeLayer与UIBezierPath画出想要的图形
- erlang中各个application参数配置
- cglib动态代理模式
- App被拒的那些坑
- 一些实用的js技巧
- 快速排序算法