完美解决java读取大文件内存溢出的问题

1. 传统方式:在内存中读取文件内容

读取文件行的标准方式是在内存中读取,Guava 和Apache Commons IO都提供了如下所示快速读取文件行的方法:

1

2

Files.readLines(new File(path), Charsets.UTF_8);

FileUtils.readLines(new File(path));

实际上是使用BufferedReader或者其子类LineNumberReader来读取的。

传统方式的问题: 是文件的所有行都被存放在内存中,当文件足够大时很快就会导致程序抛出OutOfMemoryError 异常。

问题思考:我们通常不需要把文件的所有行一次性地放入内存中,相反,我们只需要遍历文件的每一行,然后做相应的处理,处理完之后把它扔掉。所以我们可 以通过行迭代方式来读取,而不是把所有行都放在内存中。

2. 大文件读取处理方式

不重复读取与不耗尽内存的情况下处理大文件:

(1)文件流方式:使用java.util.Scanner类扫描文件的内容,一行一行连续地读取

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

FileInputStream inputStream = null;

Scanner sc = null;

try {

 inputStream = new FileInputStream(path);

 sc = new Scanner(inputStream, UTF-8);

 while (sc.hasNextLine()) {

  String line = sc.nextLine();

  // System.out.println(line);

  }

}catch(IOException e){

  logger.error(e);

}finally {

  if (inputStream != null) {

  inputStream.close();

  }

  if (sc != null) {

    sc.close();

   }

}

该方案将会遍历文件中的所有行,允许对每一行进行处理,而不保持对它的引用。总之没有把它们存放在内存中!

(2)Apache Commons IO流:使用Commons IO库实现,利用该库提供的自定义LineIterator

1

2

3

4

5

6

7

8

9

LineIterator it = FileUtils.lineIterator(theFile, UTF-8);

try {

 while (it.hasNext()) {

 String line = it.nextLine();

 // do something with line

  }

} finally {

 LineIterator.closeQuietly(it);

}

 该方案由于整个文件不是全部存放在内存中,这也就导致相当保守的内存消耗。

 

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java中,处理大文件时可能会遇到内存溢出问题。这是因为Java中的IO操作通常是基于内存缓冲区的,而当处理大文件时,缓冲区可能会超过JVM可用的内存导致内存溢出。下面介绍一些解决内存溢出问题的方法。 1. 使用BufferedInputStream和BufferedOutputStream BufferedInputStream和BufferedOutputStream是Java IO中的两个常用类,它们可以将字节流包装成带有缓冲区的流。这样,在读取和写入大文件时,它们可以减少对内存的使用,从而避免内存溢出问题。 示例代码: ```java try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("largeFile.txt")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("output.txt"))) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = bis.read(buffer)) != -1) { bos.write(buffer, 0, bytesRead); } } catch (IOException e) { e.printStackTrace(); } ``` 2. 使用NIO Java NIO提供了一种非阻塞的IO操作方式,它能够实现高效的数据传输。相比于传统的IO操作,NIO使用的是直接内存缓冲区,这样可以避免在内存中保存大量的数据,从而减少内存占用。 示例代码: ```java try (FileChannel inputChannel = new FileInputStream("largeFile.txt").getChannel(); FileChannel outputChannel = new FileOutputStream("output.txt").getChannel()) { ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024); while (inputChannel.read(buffer) != -1) { buffer.flip(); outputChannel.write(buffer); buffer.clear(); } } catch (IOException e) { e.printStackTrace(); } ``` 3. 分段读取文件 将大文件分成若干个较小的部分,逐一读取并处理每个部分。这样可以避免一次性读取整个文件导致内存溢出问题。 示例代码: ```java try (FileInputStream fis = new FileInputStream("largeFile.txt")) { byte[] buffer = new byte[1024 * 1024]; int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { // 处理每个部分的数据 } } catch (IOException e) { e.printStackTrace(); } ``` 以上三种方法可以帮助我们解决Java读取文件内存溢出问题。根据实际情况选择合适的方法可以提高程序的性能和稳定性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值