java 大文件 内存溢出_java读取超大文件方法 利用缓冲区分段读取 防止内存溢出异常...

[java]代码库import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.RandomAccessFile;

import java.nio.ByteBuffer;

import java.nio.MappedByteBuffer;

import java.nio.channels.FileChannel;

public class ReadBig {

public static String fff = "C:\\mq\\read\\from.xml";

public static void main1(String[] args) throws Exception {

final int BUFFER_SIZE = 0x300000;// 缓冲区大小为3M

File f = new File(fff);

/**

*

* map(FileChannel.MapMode mode,long position, long size)

*

* mode - 根据是按只读、读取/写入或专用(写入时拷贝)来映射文件,分别为 FileChannel.MapMode 类中所定义的

* READ_ONLY、READ_WRITE 或 PRIVATE 之一

*

* position - 文件中的位置,映射区域从此位置开始;必须为非负数

*

* size - 要映射的区域大小;必须为非负数且不大于 Integer.MAX_VALUE

*

* 所以若想读取文件后半部分内容,如例子所写;若想读取文本后1/8内容,需要这样写map(FileChannel.MapMode.READ_ONLY,

* f.length()*7/8,f.length()/8)

*

* 想读取文件所有内容,需要这样写map(FileChannel.MapMode.READ_ONLY, 0,f.length())

*

*/

MappedByteBuffer inputBuffer = new RandomAccessFile(f, "r")

.getChannel().map(FileChannel.MapMode.READ_ONLY,

f.length() / 2, f.length() / 2);

byte[] dst = new byte[BUFFER_SIZE];// 每次读出3M的内容

long start = System.currentTimeMillis();

for (int offset = 0; offset < inputBuffer.capacity(); offset += BUFFER_SIZE) {

if (inputBuffer.capacity() - offset >= BUFFER_SIZE) {

for (int i = 0; i < BUFFER_SIZE; i++)

dst[i] = inputBuffer.get(offset + i);

} else {

for (int i = 0; i < inputBuffer.capacity() - offset; i++)

dst[i] = inputBuffer.get(offset + i);

}

int length = (inputBuffer.capacity() % BUFFER_SIZE == 0) ? BUFFER_SIZE

: inputBuffer.capacity() % BUFFER_SIZE;

System.out.println(new String(dst, 0, length));// new

// String(dst,0,length)这样可以取出缓存保存的字符串,可以对其进行操作

}

long end = System.currentTimeMillis();

System.out.println("读取文件文件一半内容花费:" + (end - start) + "毫秒");

}

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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读取文件内存溢出的问题。根据实际情况选择合适的方法可以提高程序的性能和稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值