zoukankan      html  css  js  c++  java
  • 转换流

    public class OutputStreamWriterextends WriterOutputStreamWriter是从字符流到字节流的桥梁:
    使用指定的charset将写入的字符编码为字节。 它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集。
    
    public class InputStreamReader
    extends ReaderInputStreamReader是从字节流到字符流的桥梁:它读取字节,并使用指定的charset将其解码为字符。
    它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集。

    ------------------------------------------------------------------------------------------------------------------------
    OutputStreamWriter​构造方法
    OutputStreamWriter​(OutputStream out) 创建一个使用默认字符编码的OutputStreamWriter。
    OutputStreamWriter​(OutputStream out, String charsetName) 创建一个使用命名字符集的OutputStreamWriter。

    --------------------------------------------------------------------------------------------------------------
    InputStreamReader​构造方法
    InputStreamReader​(InputStream in) 创建一个使用默认字符集的InputStreamReader。
    InputStreamReader​(InputStream in, String charsetName) 创建一个使用命名字符集的InputStreamReader。
    
    
    package com.io.liushuaishuai;
    
    import java.io.*;
    import java.util.Arrays;
    
    /*
    public class OutputStreamWriterextends WriterOutputStreamWriter是从字符流到字节流的桥梁:
    使用指定的charset将写入的字符编码为字节。 它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集。
    
    public class InputStreamReader
    extends ReaderInputStreamReader是从字节流到字符流的桥梁:它读取字节,并使用指定的charset将其解码为字符。
    它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集。
    
     */
    public class convertStreamDemo01 {
        public static void main(String[] args) throws IOException {
            /*
     OutputStreamWriter​(OutputStream out) 创建一个使用默认字符编码的OutputStreamWriter。
    OutputStreamWriter​(OutputStream out, String charsetName) 创建一个使用命名字符集的OutputStreamWriter。
    
             */
    //        FileOutputStream fos = new FileOutputStream("myIOstream\fos.txt");
    //        OutputStreamWriter osw = new OutputStreamWriter(fos );
    
    //        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("myIOstream\fos.txt"));
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("myIOstream\fos.txt"),"GBK");
            osw.write("中国");
            osw.close();
    
            /*
    InputStreamReader​(InputStream in) 创建一个使用默认字符集的InputStreamReader。
    InputStreamReader​(InputStream in, String charsetName) 创建一个使用命名字符集的InputStreamReader。
    
             */
    
            InputStreamReader isr = new InputStreamReader(new FileInputStream("myIOstream\fos.txt"),"GBK");
            //两种方式  一次读取一个字符数据,一次读取一个字符数组数据
    
            //一次读取一个字符数据
            /*int ch;
            while ((ch = isr.read()) != -1) {
                System.out.print((char) ch);
            }*/
    
            //一次读取一个字符数组
            char[] chs = new char[1024];
            int len;
            while((len = isr.read(chs)) != -1) {
                System.out.println(new String(chs,0,len));
    //                        System.out.println(Arrays.toString(chs));//[中, 国,  ,  , ..... ,,]
            }
    
            isr.close();
    
    
    
    
    
    
        }
    }
    
    
    
     
  • 相关阅读:
    学习 iOS多线程开发 Demo示意
    python(数字Number)
    python(元组)
    python(列表)
    python(一)
    APP自动化测试框架搭建(转载)
    Appium真机运行Device Name获取方法
    关于android sdk manager在下载包时没有android intel x86 atom system image等项
    adb shell命令查看当前与用户交互的activity
    SDK更新
  • 原文地址:https://www.cnblogs.com/lsswudi/p/11424356.html
Copyright © 2011-2022 走看看