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:

 * Copyright (c) 2005 All Rights Reserved.
public class HelloUtf8ConvertedFixed {
   public static void main(String[] a) {
      try {
         PrintStream out =
            new PrintStream("Hello.txt", "UTF-8");
         System.out.println("Hello world!");
      } catch (Exception e) {

Check the output of this program:

herong> javac

herong> java HelloUtf8ConvertedFixed

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.

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

 javac - The Java Program Compiler

 java - The Java Program Launcher

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 jpackage - Binary Package Builder

 javadoc - The Java Document Generator

 jdeps - The Java Class Dependency Analyzer

 jdeprscan - The Java Deprecated API Scanner

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jhsdb - The Java HotSpot Debugger

 jvisualvm (Java VisualVM) - JVM Visual Tool

 jmc - Java Mission Control

 javap - The Java Class File Disassembler

 keytool - Public Key Certificate Tool

 jarsigner - JAR File Signer

 jshell - Java Language Shell

 jrunscript - Script Code Shell

 Miscellaneous Tools

native2ascii - Native-to-ASCII Encoding Converter

 native2ascii - Encoding Converter Command and Options

 javac - Using CP1252 to Process Source File

 "native2ascii -encoding" - UTF-8 to uXXXX Conversion

Setting UTF-8 Encoding in PrintStream

 Converting uXXXX Sequences Back with "-reverse" Option

 JAB (Java Access Bridge) for Windows

 Archived Tutorials


 Full Version in PDF/EPUB