java写入大文本文件_java – 尝试从大文本文件读取/写入时的OutOfMemoryError

我正在尝试读/写一个巨大的文本文件.

但是当我尝试这样做时,我得到错误:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

at java.util.Arrays.copyOf(Unknown Source)

at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)

at java.lang.AbstractStringBuilder.append(Unknown Source)

at java.lang.StringBuilder.append(Unknown Source)

at ReadWriteTextFile.getContents(ReadWriteTextFile.java:52)

at ReadWriteTextFile.main(ReadWriteTextFile.java:148)

我的代码如下:

import java.io.*;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

public class ReadWriteTextFile {

/**

* Fetch the entire contents of a text file, and return it in a String.

* This style of implementation does not throw Exceptions to the caller.

*

* @param aFile is a file which already exists and can be read.

*/

static public String getContents(File aFile) {

//...checks on aFile are elided

StringBuilder contents = new StringBuilder();

int maxlines = 1000; //counts max lines t read/write to the file

BufferedReader input = null;

BufferedWriter bw = null;

try {

//use buffering, reading one line at a time

//FileReader always assumes default encoding is OK

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值