PrintWriter: close() : PrintWriter « java.io « Java by API






PrintWriter: close()

   


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

public class Main {
  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());
    }
  }
}

   
    
    
  








Related examples in the same category

1.new PipedWriter()
2.new PrintWriter(BufferedWriter, boolean autoFlush)
3.new PrintWriter(Writer out)
4.PrintWriter: print(char c)
5.PrintWriter: print(int i)
6.PrintWriter: println(Object x)
7.PrintWriter: println(String x)
8.PrintWriter: println(char x)
9.PrintWriter: println(double x)
10.PrintWriter: println(float x)
11.PrintWriter: println(int x)
12.PrintWriter: println(long x)
13.PrintWriter: println()
14.Writer: write(char[] cbuf)