Read text File and save to String - Android java.io

Android examples for java.io:Text File

Description

Read text File and save to String

Demo Code

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {

  public static String getFileOutputString(String path) {
    try {//  ww  w.j  a va  2  s  .  c o  m
      BufferedReader bufferedReader = new BufferedReader(new FileReader(path), 8192);
      StringBuilder sb = new StringBuilder();
      String line = null;
      while ((line = bufferedReader.readLine()) != null) {
        sb.append("\n").append(line);
      }
      bufferedReader.close();
      return sb.toString();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

}

Related Tutorials