java读取大文件并处理

╰+攻爆jí腚メ 2023-10-18 14:39 215阅读 0赞

原地址:https://www.jb51.net/article/120760.htm

  1. FileInputStream inputStream = null;
  2. Scanner sc = null;
  3. try {
  4. inputStream = new FileInputStream(path);
  5. sc = new Scanner(inputStream, UTF-8);
  6. while (sc.hasNextLine()) {
  7. String line = sc.nextLine();
  8. // System.out.println(line);
  9. }
  10. }catch(IOException e){
  11. logger.error(e);
  12. }finally {
  13. if (inputStream != null) {
  14. inputStream.close();
  15. }
  16. if (sc != null) {
  17. sc.close();
  18. }
  19. }
  20. LineIterator it = FileUtils.lineIterator(theFile, UTF-8);
  21. try {
  22. while (it.hasNext()) {
  23. String line = it.nextLine();
  24. // do something with line
  25. }
  26. } finally {
  27. LineIterator.closeQuietly(it);
  28. }

发表评论

表情:
评论列表 (有 0 条评论,215人围观)

还没有评论,来说两句吧...

相关阅读

    相关 读取文件显示 PHP

    在PHP中,我们经常需要处理大型文件,例如日志文件或其他类型的数据文件。为了有效地读取和显示这些大文件的内容,我们可以采用分段读取的方法,以避免内存占用过大的问题。在本文中,我