Java 读取大文件方法

简介:

需求:实际开发中读取文本文件的需求还是很多,如读取两个系统之间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.     } 



作者:弘林

来源:51CTO

相关文章
|
28天前
|
Java
有关Java发送邮件信息(支持附件、html文件模板发送)
有关Java发送邮件信息(支持附件、html文件模板发送)
26 1
|
1月前
|
Java
java中替换文件内容
java中替换文件内容
14 1
|
13天前
|
Java
Java中ReentrantLock中tryLock()方法加锁分析
Java中ReentrantLock中tryLock()方法加锁分析
12 0
|
1月前
|
Java
java中日期处理的一些工具方法
java中日期处理的一些工具方法
17 1
|
2天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
24 3
|
4天前
|
存储 Java
Java动态转发代理IP的实现方法
Java动态转发代理IP的实现方法
20 11
|
5天前
|
Java
Java接口中可以定义哪些方法?
【4月更文挑战第13天】
7 0
Java接口中可以定义哪些方法?
|
11天前
|
Java Shell
Java 21颠覆传统:未命名类与实例Main方法的编码变革
Java 21颠覆传统:未命名类与实例Main方法的编码变革
13 0
|
13天前
|
Java
Java中关于ConditionObject的signal()方法的分析
Java中关于ConditionObject的signal()方法的分析
21 4
|
13天前
|
安全 Java
append在Java中是哪个类下的方法
append在Java中是哪个类下的方法
21 9