title: The best way to read a huge file (for example a very large text document)
body: <p>I'm new to java ...in my current project I need to read and write a very huge text file (1 GB - 5 GB) ... first i used this classes : <strong>BufferedReader</strong> and <strong>BufferedWriter</strong> </p>
  
  <pre><code>public static String read(String dir) {
      BufferedReader br;
      String result = "", line;
      try {
          br = new BufferedReader(new InputStreamReader(new FileInputStream(dir), "UTF-8"));
          while ((line = br.readLine()) != null) {
              result += line + "\n";
          }
      } catch (IOException ex) {
          //do something
      }
      return result;
  }
  
  public static void write(String dir, String text) {
      BufferedWriter bw;
      try {
          bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dir), "UTF-8"));
          bw.write("");
          for (int i = 0; i &lt; text.length(); i++) {
              if (text.charAt(i) != '\n') {
                  bw.append(text.charAt(i));
              } else {
                  bw.newLine();
              }
          }
          bw.flush();
      } catch (IOException ex) {
          //do something
      }
  }
  </code></pre>
  
  <p>this classes works very good but not for Huge files...</p>
  
  <p>then I used <strong>MappedByteBuffer</strong> for the <code>read()</code> method (I dont't know how to write a file using this class) :</p>
  
  <pre><code>public static String read(String dir) {
      FileChannel fc;
      String s = "";
      try {
          fc = new RandomAccessFile(dir, "r").getChannel();
          MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
          buffer.load();
          buffer.force();
          for (int i = 0; i &lt; buffer.limit(); i++) {
              s += (char) buffer.get();
          } //I know the problem is here
          buffer.clear();
          inChannel.close();
      } catch (IOException e) {
          //do something
      }
      return s;
  }
  </code></pre>
  
  <p>But still can't read large files(over 30-40 MB), even the NotePad is faster than my app :))</p>
  
  <p>and also another problem is I don't know how to change encoding in second way(for example "UTF-8", "ANSI",...)</p>
  
  <p>so guys, please tell me which is the best way to read and write laaaarge files?
  any idea?</p>