title: Sorting a huge file in Java
body: <p>I have a file, which consists of a one row:</p>
  
  <pre><code> 1 , 1 2 , 1 3 6 , 4 ,...
  </code></pre>
  
  <p>In this representation, spaces separate the integers and commas. 
  This  string is so huge that I can't read it with RandomAccessFile.readLine() (almost 4 Gb needed). So that I created a buffer, which can contain 10 integers. My task is to sort all integers in the string. </p>
  
  <p>Could you, please, help? </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>@Oscar Reyes</p>
  
  <p>I need to write some sequences of integers to a file and then to read from it. Actually I don't know, how to do it. I'm a newbie. So I decided to use chars to write integers, delimiters between integers are ",", and delimeters between sequences are "\n\r" which. So that I created a monster that reads it:</p>
  
  <pre><code>public BinaryRow getFilledBuffer(String filePath, long offset) throws IOException{
      mainFile = new RandomAccessFile(filePath, "r");
  
      if (mainFile.length() == 0){
          return new BinaryRow();
      }
  
      StringBuilder str = new StringBuilder();
  
      mainFile.seek(mainFile.length()-4); //that is "\n" symbol
      char chN = mainFile.readChar();
  
      mainFile.seek(offset);
      int i = 0;
      char nextChar = mainFile.readChar();
      while (i &lt; 11 &amp;&amp; nextChar != chN){
          str.append(nextChar);
          if (nextChar == ','){
              i++;
              if (i == 10){
                  break;
              }
          }
          nextChar = mainFile.readChar();
      }
  
      if (nextChar == chN){
          position = -1;
      }else{
          position = mainFile.getFilePointer();
      }
  
      BinaryRow br = new BinaryRow();
  
      StringBuilder temp = new StringBuilder();
  
      for (int j = 0; j &lt; str.length(); j++){
          if ((str.charAt(j) != ',')){
              temp.append(str.charAt(j));
              if (j == str.length() - 1){
                  br.add(Integer.parseInt(temp.toString()));
              }   
          }else{
              br.add(Integer.parseInt(temp.toString()));
              temp.delete(0, temp.length());
          }
      }
  
  
      mainFile.close();
      return br;
  
  }
  </code></pre>
  
  <p>If you could advise how to do it, please do it =)</p>