19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

characters, Cp939 for Japanese characters, Cp933 for Korean characters, and<br />

Cp838 for Thai characters.<br />

The following code in Listing 35.8 creates a file using the GB18030 encoding<br />

(line 8). You have <strong>to</strong> read the text using the same encoding (line 12). The<br />

output is shown in Figure 35.12a.<br />

Listing 35.8 EncodingDemo.java<br />

<br />

<br />

1 import java.util.*;<br />

2 import java.io.*;<br />

3 import javax.swing.*;<br />

4<br />

5 public class EncodingDemo {<br />

6 public static void main(String[] args)<br />

7 throws IOException, FileNotFoundException {<br />

8 PrintWriter output = new PrintWriter("temp.txt", "GB18030");<br />

9 output.print("\u6B22\u8FCE Wel<strong>com</strong>e \u03b1\u03b2\u03b3");<br />

10 output.close();<br />

11<br />

12 Scanner input = new Scanner(new File("temp.txt"), "GB18030");<br />

13 JOptionPane.showMessageDialog(null, input.nextLine());<br />

14 }<br />

15 }<br />

(a) Using GB18030 encoding<br />

(b) Using default encoding<br />

Figure 35.12<br />

You can specify an encoding scheme for a text file.<br />

If you don’t specify an encoding in lines 8 and 12, the system’s default<br />

encoding scheme is used. The US default encoding is ASCII. ASCII code uses 8<br />

bits. <strong>Java</strong> uses the 16-bit Unicode. If a Unicode is not an ASCII code, the<br />

character '?' is written <strong>to</strong> the file. Thus, when you write \u6B22 <strong>to</strong> an ASCII<br />

file, the ? character is written <strong>to</strong> the file. When you read it back, you will<br />

see the ? character, as shown in Figure 35.12b.<br />

To find out the default encoding on your system, use<br />

<br />

System.out.println(System.getProperty("file.encoding"));<br />

The default encoding name is Cp1252 on Windows, which is a variation of ASCII.<br />

Key Terms<br />

locale 7<br />

resource bundle<br />

file encoding scheme<br />

32

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!