进阶day10-转换流_按照指定编码写数据(OutputStreamWriter)
This commit is contained in:
40
javaSE-day10/src/com/inmind/transfer_stream02/Demo10.java
Normal file
40
javaSE-day10/src/com/inmind/transfer_stream02/Demo10.java
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package com.inmind.transfer_stream02;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
|
||||||
|
/*
|
||||||
|
转换流_按照指定编码写数据(OutputStreamWriter)
|
||||||
|
OutputStreamWriter是从字符流到字节流的桥梁: 编码
|
||||||
|
|
||||||
|
构造方法:
|
||||||
|
OutputStreamWriter(OutputStream out) 创建一个使用默认字符编码的OutputStreamWriter。
|
||||||
|
OutputStreamWriter(OutputStream out, String charsetName) 创建一个使用命名字符集的OutputStreamWriter。
|
||||||
|
|
||||||
|
常用方法:
|
||||||
|
close
|
||||||
|
flush
|
||||||
|
5个写方法
|
||||||
|
*/
|
||||||
|
public class Demo10 {
|
||||||
|
//使用转换输出流,指定编码方式GBK输出内容到文件中
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//OutputStreamWriter(OutputStream out, String charsetName) 创建一个使用命名字符集的OutputStreamWriter。
|
||||||
|
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("file_gbk.txt"),"gbk");
|
||||||
|
osw.write("我是谁我在哪111");
|
||||||
|
osw.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//使用转换输出流,指定编码方式utf-8输出内容到文件中
|
||||||
|
public static void method(String[] args) throws IOException {
|
||||||
|
//OutputStreamWriter(OutputStream out) 创建一个使用默认字符编码的OutputStreamWriter。
|
||||||
|
// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("file_utf8.txt"));
|
||||||
|
//OutputStreamWriter(OutputStream out, String charsetName) 创建一个使用命名字符集的OutputStreamWriter。
|
||||||
|
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("file_utf8.txt"),"utf-8");
|
||||||
|
osw.write("我是谁我在哪111");
|
||||||
|
osw.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user