星空网 > 软件开发 > Java

Java 读取大文件方法

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

 

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

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

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

SELECT LEVEL||'|'||'张'||LEVEL||'|'||(13900000000+LEVEL) FROM DUAL CONNECT BY LEVEL < 1000000;

 

实现如下:

package com.test.common.util;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.Scanner;import org.apache.commons.io.FileUtils;import org.apache.commons.io.LineIterator;public class HandleTextFile {    // 使用commons-io.jar包的FileUtils的类进行读取  public static void readTxtFileByFileUtils(String fileName) {    File file = new File(fileName);    try {      LineIterator lineIterator = FileUtils.lineIterator(file, "UTF-8");      while (lineIterator.hasNext()) {        String line = lineIterator.nextLine();        System.out.println(line);      }    } catch (IOException e) {      e.printStackTrace();    }  }    // 使用Scanner进行读取  public static void readTxtByScanner(String fileName) {    FileInputStream fileInputStream = null;     Scanner scanner = null;        try {      fileInputStream = new FileInputStream(fileName);      scanner = new Scanner(fileInputStream, "UTF-8");      while (scanner.hasNext()) {        String line = scanner.nextLine();        System.out.println(line);      }    } catch (FileNotFoundException e) {      e.printStackTrace();    } finally {      if (fileInputStream != null) {        try {          fileInputStream.close();        } catch (IOException e) {          e.printStackTrace();        }      }      if (scanner != null) {        scanner.close();      }    }      }  // 使用cache进行读取  public static void readTxtByStringBuffer(String fileName) throws IOException {    File file = new File(fileName);        BufferedReader reader = null;        try {      reader = new BufferedReader(new FileReader(file), 10 * 1024 * 1024);      String stringMsg = null;      while ((stringMsg = reader.readLine()) != null) {        System.out.println(stringMsg);      }      reader.close();    } catch (FileNotFoundException e) {      e.printStackTrace();    }   }    public static void main(String[] args) {    try {      HandleTextFile.readTxtByStringBuffer("D:\\test\\customer_info.txt");    } catch (IOException e) {      e.printStackTrace();    }  }}

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




原标题:Java 读取大文件方法

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Shopee库存管理功能上线,管理库存更 Easy!:https://www.kjdsnews.com/a/543668.html
用时两年,Shopee成巴西下载量最大购物应用程序:https://www.kjdsnews.com/a/543669.html
这款免费的神仙选品工具你还没有用过吗?:https://www.kjdsnews.com/a/543670.html
亚马逊官方选品工具上线!帮你拓展更多品类:https://www.kjdsnews.com/a/543671.html
如何在 15 分钟内创建自己的 Shopify 商店(2021 年 8 月):https://www.kjdsnews.com/a/543672.html
亚马逊产品快速清仓的六个方法:https://www.kjdsnews.com/a/543673.html
旅游景点的打油诗 旅游景点的打油诗怎么写:https://www.vstour.cn/a/366180.html
2024深圳龙岗区文化馆免费音乐培训课程安排+报名方式:https://www.vstour.cn/a/366181.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流