Java Tutorial/File/OutputStreamWriter

Материал из Java эксперт
Версия от 05:19, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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();
  }
}