Uses a PrintWriter to write two Chinese characters and read them back. : InputStreamReader « File « Java Tutorial





import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class MainClass {
  public static void main(String[] args) {
    try {
      char[] chars = new char[2];
      chars[0] = '\u4F60';
      chars[1] = '\u597D';
      String encoding = "GB18030";
      File textFile = new File("C:\\temp\\myFile.txt");
      PrintWriter writer = new PrintWriter(textFile,

      encoding);
      writer.write(chars);
      writer.close();

      // read back
      InputStreamReader reader = new InputStreamReader(new FileInputStream(textFile), encoding);
      char[] chars2 = new char[2];
      reader.read(chars2);
      System.out.print(chars2[0]);
      System.out.print(chars2[1]);
      reader.close();
    } catch (IOException e) {
      System.out.println(e.toString());
    }
  }
}










11.15.InputStreamReader
11.15.1.InputStreamReader
11.15.2.Uses a PrintWriter to write two Chinese characters and read them back.
11.15.3.Reading ISO Latin-1 Encoded Data
11.15.4.Get a class of an object
11.15.5.Deal with Keyboard Input with BufferedReader