read From Txt file using UTF-16 encoding - Android java.io

Android examples for java.io:UTF

Description

read From Txt file using UTF-16 encoding

Demo Code

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class Main {

  public static String readFromTxt(File file, int i) {
    String s = "";
    int count = 1;
    StringBuffer sb = new StringBuffer();
    try {/*from  ww w .  ja va2s  .c  o  m*/
      BufferedReader br = new BufferedReader(
          new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-16")));
      while ((s = br.readLine()) != null) {
        if (count == i) {
          sb.append(s);
        }
        count++;
      }
      br.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return sb.toString();
  }

}

Related Tutorials