本文共 991 字,大约阅读时间需要 3 分钟。
java中IO流字符流中的编码
InputStreamReader(InputStream in, CharsetDecoder dec) 创建使用给定字符集解码器的 InputStreamReader
OutputStreamWriter(OutputStream out, CharsetEncoder enc) 创建使用给定字符集编码器的 OutputStreamWriter
package com.itheima_09;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;/* * 字符流中的编码 * * 字符流 = 字节流 + 编码 * */public class EncoderDemo2 { public static void main(String[] args) throws IOException { //method(); OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("b.txt"),"UTF-8"); //OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("b.txt")); String s = "迎娶白富美"; osw.write(s); osw.close(); } private static void method() throws IOException, UnsupportedEncodingException { FileWriter fw = new FileWriter("b.txt"); String s = "月薪过万"; byte[] bys = s.getBytes("UTF-8"); //fw.write(s); fw.write(new String(bys)); fw.close(); } }
转载地址:http://enjqb.baihongyu.com/