Java 读取大文件方法

开发 后端
需求:实际开发中读取文本文件的需求还是很多,如读取两个系统之间FTP发送文件,读取后保存到数据库中或日志文件的数据库中保存等。

需求:实际开发中读取文本文件的需求还是很多,如读取两个系统之间FTP发送文件,读取后保存到数据库中或日志文件的数据库中保存等。

为了测试首先利用数据库SQL生成大数据文件。

规则是 编号|姓名|手机号,如 10|张10|13900000010

利用下面语句可以生成10,000,000条数据。

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

实现如下:

  1. package com.test.common.util; 
  2.  
  3. import java.io.BufferedReader; 
  4. import java.io.File; 
  5. import java.io.FileInputStream; 
  6. import java.io.FileNotFoundException; 
  7. import java.io.FileReader; 
  8. import java.io.IOException; 
  9. import java.util.Scanner; 
  10.  
  11. import org.apache.commons.io.FileUtils; 
  12. import org.apache.commons.io.LineIterator; 
  13.  
  14. public class HandleTextFile { 
  15.      
  16.     // 使用commons-io.jar包的FileUtils的类进行读取 
  17.     public static void readTxtFileByFileUtils(String fileName) { 
  18.         File file = new File(fileName); 
  19.         try { 
  20.             LineIterator lineIterator = FileUtils.lineIterator(file, "UTF-8"); 
  21.             while (lineIterator.hasNext()) { 
  22.                 String line = lineIterator.nextLine(); 
  23.                 System.out.println(line); 
  24.             } 
  25.         } catch (IOException e) { 
  26.             e.printStackTrace(); 
  27.         } 
  28.     } 
  29.      
  30.     // 使用Scanner进行读取 
  31.     public static void readTxtByScanner(String fileName) { 
  32.         FileInputStream fileInputStream = null;  
  33.         Scanner scanner = null
  34.          
  35.         try { 
  36.             fileInputStream = new FileInputStream(fileName); 
  37.             scanner = new Scanner(fileInputStream, "UTF-8"); 
  38.             while (scanner.hasNext()) { 
  39.                 String line = scanner.nextLine(); 
  40.                 System.out.println(line); 
  41.             } 
  42.         } catch (FileNotFoundException e) { 
  43.             e.printStackTrace(); 
  44.         } finally { 
  45.             if (fileInputStream != null) { 
  46.                 try { 
  47.                     fileInputStream.close(); 
  48.                 } catch (IOException e) { 
  49.                     e.printStackTrace(); 
  50.                 } 
  51.             } 
  52.             if (scanner != null) { 
  53.                 scanner.close(); 
  54.             } 
  55.         } 
  56.          
  57.     } 
  58.  
  59.     // 使用cache进行读取 
  60.     public static void readTxtByStringBuffer(String fileName) throws IOException { 
  61.         File file = new File(fileName); 
  62.          
  63.         BufferedReader reader = null
  64.          
  65.         try { 
  66.             reader = new BufferedReader(new FileReader(file), 10 * 1024 * 1024); 
  67.             String stringMsg = null
  68.             while ((stringMsg = reader.readLine()) != null) { 
  69.                 System.out.println(stringMsg); 
  70.             } 
  71.             reader.close(); 
  72.         } catch (FileNotFoundException e) { 
  73.             e.printStackTrace(); 
  74.         }  
  75.     } 
  76.      
  77.     public static void main(String[] args) { 
  78.         try { 
  79.             HandleTextFile.readTxtByStringBuffer("D:\\test\\customer_info.txt"); 
  80.         } catch (IOException e) { 
  81.             e.printStackTrace(); 
  82.         } 
  83.     } 

 参考文件:读取大文件性能测试

 

责任编辑:王雪燕 来源: 博客园
相关推荐

2020-06-15 08:03:17

大文件OOM内存

2009-07-20 16:09:39

2012-06-20 14:16:36

Java内存映射

2019-11-14 05:00:00

Linux文件磁盘空间

2021-01-15 12:02:25

java 大文件工具

2009-08-12 17:27:11

C#读取文件

2021-05-10 07:33:10

Java开源工具

2014-04-28 09:29:36

2013-05-29 09:59:20

Java-RMI远程调用

2015-08-07 15:35:42

ios短点下载源码

2015-03-13 13:50:47

Java读取文件夹大小Java读取文件Java读取

2009-11-16 11:41:19

PHP上传大文件

2022-06-13 14:06:33

大文件上传前端

2022-07-25 11:33:48

Python大文件

2020-08-14 11:01:32

数据Pandas文件

2016-12-13 23:08:48

Linux命令

2009-06-11 17:39:55

xmljava

2022-09-26 00:21:03

Spring文件项目

2010-08-02 16:58:08

Flex配置文件

2017-12-05 13:25:40

PHP开发服务器内存
点赞
收藏

51CTO技术栈公众号