JDK Tutorials - Herong's Tutorial Examples - v6.32, by Herong Yang
Viewing Encoded Text Files in Web Browsers
This section provides a tutorial example on how to view text files with different encodings with Web browser Internet Explorer. The encoded text file should be modified to add proper HTML tags using the sample program EncodingHtml.java.
Now, we have our greeting messages saved in many different encodings. The next question is how do display them as glyph of the corresponding languages on the screen. One of the ways I have used in the past is to run a multi-language enabled Web browser like IE to view the text files. To do this, we have to mark up the text into a html file, by using a program like this one:
/* EncodingHtml.java * Copyright (c) HerongYang.com. All Rights Reserved. * This program allows you to mark up a text file into html file. */ import java.io.*; import java.util.*; class EncodingHtml { public static void main(String[] a) { String inFile = a[0]; String inCharsetName = a[1]; String outFile = inFile + ".html"; try { InputStreamReader in = new InputStreamReader( new FileInputStream(inFile), inCharsetName); OutputStreamWriter out = new OutputStreamWriter( new FileOutputStream(outFile), inCharsetName); writeHead(out, inCharsetName); int c = in.read(); int n = 0; while (c!=-1) { out.write(c); n++; c = in.read(); } writeTail(out); in.close(); out.close(); System.out.println("Number of characters: "+n); } catch (IOException e) { System.out.println(e.toString()); } } public static void writeHead(OutputStreamWriter out, String cs) throws IOException { out.write("<html><head>\n"); out.write("<meta http-equiv=\"Content-Type\""+ " content=\"text/html; charset="+cs+"\">\n"); out.write("</head><body><pre>"); } public static void writeTail(OutputStreamWriter out) throws IOException { out.write("</pre></body></html>\n"); } }
Now, let's compile this program and run it with hello.utf-8:
herong> javac EncodingHtml.java herong> java EncodingHtml hello.utf-8 utf-8
If you have installed IE with the Chinese language supports, you should be able to open the output file, hello.utf-8.html, and enjoy reading the messages in English, Simplified Chinese, and Traditional Chinese.
Then, run EncodingHtml.java with other encodings,
herong> java EncodingHtml hello.gbk gbk herong> java EncodingHtml hello.big5 big5 herong> java EncodingHtml hello.shift_jis shift_jis
View the output files with IE, and compare the results:
If you manually change the setting of View/Encoding, IE will not be able to show the message with the right glyph.
Table of Contents
Date, Time and Calendar Classes
Date and Time Object and String Conversion
Number Object and Numeric String Conversion
Locales, Localization Methods and Resource Bundles
Calling and Importing Classes Defined in Unnamed Packages
HashSet, Vector, HashMap and Collection Classes
Character Set Encoding Classes and Methods
►Encoding Conversion Programs for Encoded Text Files
\uxxxx - Entering Unicode Data in Java Programs
HexWriter.java - Converting Encoded Byte Sequences to Hex Values
EncodingConverter.java - Encoding Conversion Sample Program
►Viewing Encoded Text Files in Web Browsers
Unicode Signs in Different Encodings
Datagram Network Communication
DOM (Document Object Model) - API for XML Files
DTD (Document Type Definition) - XML Validation
XSD (XML Schema Definition) - XML Validation
XSL (Extensible Stylesheet Language)
Message Digest Algorithm Implementations in JDK
Private key and Public Key Pair Generation
PKCS#8/X.509 Private/Public Encoding Standards
Digital Signature Algorithm and Sample Program
"keytool" Commands and "keystore" Files
KeyStore and Certificate Classes
Secret Key Generation and Management
Cipher - Encryption and Decryption
The SSL (Secure Socket Layer) Protocol
SSL Socket Communication Testing Programs