write To Txt file using OutputStreamWriter - Android java.io

Android examples for java.io:OutputStreamWriter

Description

write To Txt file using OutputStreamWriter

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 writeToTxt(String fileName, String content) {
    try {//from  ww w .j a  va 2 s. c  o  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 + "\r\n");
      out.flush();
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

}

Related Tutorials