Java I/O How to - Write UTF-8 Encoded Data








Question

We would like to know how to write UTF-8 Encoded Data.

Answer

 //from  ww w  .j  a v  a2s. co m


import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class Main {
  public static void main(String[] argv) throws Exception {

    Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("outfilename"),
        "UTF8"));
    out.write("asdf");
    out.close();
  }
}