read File by encoding - Android java.io

Android examples for java.io:UTF

Description

read File by encoding

Demo Code

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.util.Log;

public class Main {

  private static final String TAG = "";

  public static String readFile(File file, String encoding) {
    StringBuilder builder = new StringBuilder();
    try (final InputStream is = new FileInputStream(file);
        final InputStreamReader isr = new InputStreamReader(is, encoding);
        final BufferedReader reader = new BufferedReader(isr)) {
      String line = null;/* w  w w .  ja  v  a 2s. c  o m*/
      while ((line = reader.readLine()) != null) {
        builder.append(line + "\n");
      }
    } catch (Exception e) {
      Log.e(TAG, "File not readable");
      Log.w(TAG, e);
    }
    return builder.toString();
  }

}

Related Tutorials