title: Read large file error "outofmemoryerror"(java)
body: <p>sorry for my english. I want to read a large file, but when I read error occurs <strong>outOfMemoryError</strong>. I do not understand how to work with memory in the application. The following code does not work:</p>
  
  <pre><code>try {
  
      StringBuilder fileData = new StringBuilder(1000);
      BufferedReader reader = new BufferedReader(new FileReader(file));
  
      char[] buf = new char[8192];
      int bytesread = 0, 
          bytesBuffered = 0;
  
      while( (bytesread = reader.read( buf )) &gt; -1 ) {
  
          String readData = String.valueOf(buf, 0, bytesread);
          bytesBuffered += bytesread;
  
          fileData.append(readData); //this is error
  
          if (bytesBuffered &gt; 1024 * 1024) { 
              bytesBuffered = 0;
          }
      }
  
      System.out.println(fileData.toString().toCharArray());
  } finally {
  
  }
  </code></pre>