public static List<File> sortInBatch(File file,Comparator<String> cmp,int maxtmpfiles) throws IOException {
  List<File> files=new ArrayList<File>();
  BufferedReader fbr=new BufferedReader(new FileReader(file));
  long blocksize=estimateBestSizeOfBlocks(file,maxtmpfiles);
  try {
    List<String> tmplist=new ArrayList<String>();
    String line="";
    try {
      while (line != null) {
        long currentblocksize=0;
        while ((currentblocksize < blocksize) && ((line=fbr.readLine()) != null)) {
          tmplist.add(line);
          currentblocksize+=line.length() * 2;
        }
        files.add(sortAndSave(tmplist,cmp));
        tmplist.clear();
      }
    }
 catch (    EOFException oef) {
      if (tmplist.size() > 0) {
        files.add(sortAndSave(tmplist,cmp));
        tmplist.clear();
      }
    }
  }
  finally {
    fbr.close();
  }
  return files;
}

public static List < File > sortInBatch ( File file , Comparator < String > cmp , int maxtmpfiles ) throws IOException { List < File > files = new ArrayList < File > ( ) ; BufferedReader fbr = new BufferedReader ( new FileReader ( file ) ) ; long blocksize = estimateBestSizeOfBlocks ( file , maxtmpfiles ) ; try { List < String > tmplist = new ArrayList < String > ( ) ; String line = "" ; try { while ( line != null ) { long currentblocksize = 0 ; while ( ( currentblocksize < blocksize ) && ( ( line = fbr . readLine ( ) ) != null ) ) { tmplist . add ( line ) ; currentblocksize += line . length ( ) * 2 ; } files . add ( sortAndSave ( tmplist , cmp ) ) ; tmplist . clear ( ) ; } } catch ( EOFException oef ) { if ( tmplist . size ( ) > 0 ) { files . add ( sortAndSave ( tmplist , cmp ) ) ; tmplist . clear ( ) ; } } } finally { fbr . close ( ) ; } return files ; }
this will simply load the file by blocks of x rows , then sort them in - memory , and write the result to temporary files that have to be merged later . you can specify a bound on the number of temporary files that will be created .
