s-day10--转换流_按照指定编码写数据(OutputStreamWriter)

This commit is contained in:
xx
2026-10-26 11:16:59 +08:00
parent f853eee1e3
commit a55901e29f
2 changed files with 31 additions and 1 deletions
@@ -0,0 +1,31 @@
package com.inmind.transferstream02;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
/*
11.转换流_按照指定编码写数据(OutputStreamWriter)
OutputStreamWriter是从字符流到字节流的桥梁: 编码
构造方法:
OutputStreamWriter(OutputStream out) 创建一个使用默认字符编码的OutputStreamWriter。
OutputStreamWriter(OutputStream out, String charsetName) 创建一个使用命名字符集的OutputStreamWriter。
常用方法:
close
flush
5个写方法
*/
public class Demo07 {
public static void main(String[] args) throws Exception{
//指定gbk编码方式写出内容到文件中
// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\io_test\\file_gbk.txt"),"gbk");
// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\io_test\\file_utf8.txt"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\io_test\\file1_utf8.txt"),"utf-8");
osw.write("中国");
osw.flush();
osw.close();
}
}