BufferedReader读取大文件内存溢出问题 本帖最后由 is_zhoufeng 于 2014-01-11 22:13:22 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主不知道NIO?MappedByteBuffer+RandAccessFile搞定。 肯定不能全读进内存啊。File file = new File(filepath); BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file)); BufferedReader reader = new BufferedReader(new InputStreamReader(fis,"utf-8"),5*1024*1024);// 用5M的缓冲读取文本文件 String line = "";while((line = reader.readLine()) != null){//TODO: write your business} 我这样试过了, 也不行。 还是到300多W行的时候内存溢出。不会吧,这是每次读5M,怎么会溢出?嗯, 是的。 我也不清楚是为什么。还是当读取到300多W行的时候会变得很慢。 http://blog.csdn.net/kongbaidepao/article/details/11876281这个例子挺详细的。 问题解决了。 做法是先使用readLine读取线面的3000多行。 之后就不用readLine读取了, 而是使用字节流。代码如下:package com.zf;import java.io.File;import java.io.FileOutputStream;import java.io.RandomAccessFile;public class RandomAccessFileSplitFile { public static void main(String[] args) throws Exception { String path = "I:/cms.sql" ; File bigFile = new File(path) ; //先将前面3554行读取 RandomAccessFile reader = new RandomAccessFile(bigFile,"r") ; for (int j = 0; j < 3554; j++) { System.out.println(reader.readLine());; } //将后面的内容写入到新文件 (使用字节流读, 而不是使用ReadLine) String outPath = "I:/cms_data.sql" ; File outFile = new File(outPath) ; FileOutputStream fos = new FileOutputStream(outFile , false) ; byte[] buf = new byte[1024] ; int len = 0 ; while((len = reader.read(buf)) != -1){ fos.write(buf, 0, len); } reader.close(); fos.close(); System.out.println("END!!!!!!!!!!!"); }} 感觉是readLine()的Bug,读取行的时候会生成很多String,然后内存没有被GC及时回收造成内存泄漏。 shell script的工作为啥用java做?script不够用的话,用Python,这种工作轮不到用java。 关于多线程的执行顺序(初学) 请问学习JAVA和.Net出来分别可以做什么? 请问:怎样精简 java 虚拟机 跪求return 的使用方法 如何给JLabel绘制一条线? 一个解决问题的好地方!! 谁先来解决问题就给分最多,快点来哦 这两个有区别么?(String[] args) 和(String args[])? 我想做一个菱形的JComponent,请问可以实现吗? 请大家推荐一下java里socket编程的资料。 java jtree 节点图标前还能加文本框吗? 请教一个Swing Java编程的“代码实现与预期不符”的故障原因.
File file = new File(filepath);
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));
BufferedReader reader = new BufferedReader(new InputStreamReader(fis,"utf-8"),5*1024*1024);// 用5M的缓冲读取文本文件
String line = "";
while((line = reader.readLine()) != null){
//TODO: write your business
}
我这样试过了, 也不行。 还是到300多W行的时候内存溢出。不会吧,这是每次读5M,怎么会溢出?嗯, 是的。 我也不清楚是为什么。还是当读取到300多W行的时候会变得很慢。
这个例子挺详细的。
package com.zf;import java.io.File;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;public class RandomAccessFileSplitFile {
public static void main(String[] args) throws Exception {
String path = "I:/cms.sql" ;
File bigFile = new File(path) ;
//先将前面3554行读取
RandomAccessFile reader = new RandomAccessFile(bigFile,"r") ;
for (int j = 0; j < 3554; j++) {
System.out.println(reader.readLine());;
}
//将后面的内容写入到新文件 (使用字节流读, 而不是使用ReadLine)
String outPath = "I:/cms_data.sql" ;
File outFile = new File(outPath) ;
FileOutputStream fos = new FileOutputStream(outFile , false) ;
byte[] buf = new byte[1024] ;
int len = 0 ;
while((len = reader.read(buf)) != -1){
fos.write(buf, 0, len);
}
reader.close();
fos.close();
System.out.println("END!!!!!!!!!!!");
}}
script不够用的话,用Python,这种工作轮不到用java。