Java Tutorial/File/OutputStreamWriter
OutputStreamWriter
- An OutputStreamWriter is a bridge from character streams to byte streams.
- 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();
}
}