OutputStreamWriter 的write方法是不是只能写入文本文件
发布网友
发布时间:2022-04-25 04:46
我来回答
共1个回答
热心网友
时间:2023-10-17 18:19
它有4种构建函数:
OutputStreamWriter(OutputStream out)
OutputStreamWriter(OutputStream out, Charset cs)
OutputStreamWriter(OutputStream out, CharsetEncoder csencode)
OutputStreamWriter(OutputStream out, String charsetname)
至少 第3种 可以写 出 特殊编码文件,也就是2进制文件。
例如(抄袭来的程序):
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
public class write_binary {
public static void main(String args[]) throws java.io.IOException {
FileOutputStream fos = new FileOutputStream("data.dat");
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
BufferedWriter out = new BufferedWriter(osw);
String text = "á é í ó ú ñ €n";
out.write(text);
out.flush();
fos.close();
}
}