Java Tutorial/File/OutputStreamWriter — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 05:19, 1 июня 2010

OutputStreamWriter

  1. An OutputStreamWriter is a bridge from character streams to byte streams.
  2. Characters written to an OutputStreamWriter are encoded into bytes using a specified character set.

The OutputStreamWriter class has four constructors:



public OutputStreamWriter (OutputStream out)
public OutputStreamWriter (OutputStream out, java.nio.charset. Charset cs)
public OutputStreamWriter (OutputStream out, java.nio.charset. CharsetEncoder enc)
public OutputStreamWriter (OutputStream out, String encoding)





Writing ISO Latin-1 Encoded Data

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class Main {
  public static void main(String[] argv) throws Exception {
    Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("outfilename"),
        "8859_1"));
    out.write("asdf");
    out.close();
  }
}





Writing UTF-8 Encoded Data

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class Main {
  public static void main(String[] argv) throws Exception {
    Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("outfilename"),
        "UTF8"));
    out.write("asdf");
    out.close();
  }
}