Writing a file in UTF-8 encoding using a Writer

You can specify the encoding UTF-8 with OutputStreamWriter. This class will convert the outgoing Unicode to UTF-8 bytes.

Main.java:

import java.io.*;
 
public class Main {
   public static void main(String args[]) {
      try {
         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("test.utf"), "UTF8");
         BufferedWriter bw = new BufferedWriter(osw);
         bw.write("hu0000ello");
         bw.close();
      }
      catch(FileNotFoundException e) {
         System.out.println(e);
      }
      catch(IOException e) {
         System.out.println(e);
      }
   }
}