Setting UTF-8 Encoding in PrintStream

This section provides a tutorial example on how to set UTF-8 encoding an output PrintStream to correctly print non-ASCII characters.

In order to print non-ASCII characters stored in Java strings, you need to set the correct encoding in the PrintStream object.

Here is how I fixed the problem you saw in the previous section:

import java.io.*;
public class HelloUtf8ConvertedFixed {
   public static void main(String[] a) {
      try {
         PrintStream out = 
            new PrintStream("\\herong\\Hello.txt", "UTF-8");
         System.setOut(out);
         System.out.println("Hello world!");
         System.out.println("\u4e16\u754c\u4f60\u597d\uff01");
      } catch (Exception e) {
      }
   }
}

Check the output of this program:

C:\herong>javac HelloUtf8ConvertedFixed.java

C:\herong>java HelloUtf8ConvertedFixed

C:\herong>type Hello.txt

Hello world!
世界你好!

Excellent! This final program shows how Chinese characters can be correctly encoded in the Java source file, and correctly printed in the output file.

Last update: 2015.

Table of Contents

 About This Book

 Java Tools Terminology

 Installing Java 8 on Windows

 'javac' - The Java Program Compiler

 'java' - The Java Program Launcher

 'jdb' - The Java Debugger

 'jconsole' - Java Monitoring and Management Console

 'jstat' - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jvisualvm (Java VisualVM) - JVM Visual Tool

 'jar' - The JAR File Tool

 'javap' - The Java Class File Disassembler

 'keytool' - Public Key Certificate Tool

'native2ascii' - Native-to-ASCII Encoding Converter

 'native2ascii' - Encoding Converter Command and Options

 'javac' Using CP1252 to Process Source File

 UTF-8 to \udddd Conversion with 'native2ascii -encoding'

Setting UTF-8 Encoding in PrintStream

 Converting \udddd Sequences Back with "-reverse" Option

 Outdated Tutorials

 References

 PDF Printing Version