Java -> Writing to a file with different encodings.

Just some code in action and a little discussion.

This is the code I have:
/* User: koray@tugay.biz Date: 20/01/16 Time: 15:22 */
 
import java.io.*;
import java.nio.charset.Charset;
 
public class TestClass {
 
    public static void main(String[] args) throws Exception {
        final String text = "Koray Tugay.";
        createFileAndSaveTextToDiskWithEncoding(text, "UTF-8");
        createFileAndSaveTextToDiskWithEncoding(text, "UTF-16");
        createFileAndSaveTextToDiskWithEncoding(text, "UTF-32");
    }
 
    public static void createFileAndSaveTextToDiskWithEncoding(String text, String encoding) throws IOException {
        final File file = new File(encoding + ".txt");
        final OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file), Charset.forName(encoding));
        outputStreamWriter.write(text);
        outputStreamWriter.flush();
        outputStreamWriter.close();
    }
}

And when the code is executed I will see the following files in my disk: