且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java 读取大文件方法

更新时间:2022-09-27 19:18:07

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

Java 读取大文件方法

为了测试首先利用数据库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