本帖最后由 is_zhoufeng 于 2014-01-11 22:13:22 编辑

解决方案 »

  1.   

    楼主不知道NIO?MappedByteBuffer+RandAccessFile搞定。
      

  2.   

    肯定不能全读进内存啊。
    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
    }
      

  3.   


    我这样试过了, 也不行。 还是到300多W行的时候内存溢出。不会吧,这是每次读5M,怎么会溢出?嗯, 是的。 我也不清楚是为什么。还是当读取到300多W行的时候会变得很慢。 
      

  4.   

    http://blog.csdn.net/kongbaidepao/article/details/11876281
    这个例子挺详细的。
      

  5.   

    问题解决了。 做法是先使用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!!!!!!!!!!!");

    }}
      

  6.   

    感觉是readLine()的Bug,读取行的时候会生成很多String,然后内存没有被GC及时回收造成内存泄漏。
      

  7.   

    shell script的工作为啥用java做?
    script不够用的话,用Python,这种工作轮不到用java。