write To text file using UTF-16 encoding - Android java.io

Android examples for java.io:UTF

Description

write To text file using UTF-16 encoding

Demo Code

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

public class Main {

  public static void writeToState(String fileName, String content) {
    try {//from ww w.jav  a 2s . co  m
      OutputStreamWriter write = null;
      BufferedWriter out = null;
      if (fileName != null) {
        try {
          write = new OutputStreamWriter(new FileOutputStream(fileName, true), Charset.forName("UTF-16"));
          out = new BufferedWriter(write);
        } catch (Exception e) {
        }
      }
      out.write(content);
      out.flush();
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

}

Related Tutorials