• Java InputStreamReader类的简介说明


    转自:

    Java InputStreamReader类的简介说明

    下文讲述java中InputStreamReader类简介说明,如下所示:

    InputStreamReader类的功能:
      将字节流转换为字符流
    当不指定字符集编码,该解码过程将使用平台默认的字符编码,如:GBK
    

    InputStreamReader类的定义

    public class InputStreamReader extends Reader
    

    InputStreamReader类的构造函数

    InputStreamReader(InputStream in)   //创建一个使用默认字符集的 InputStreamReader
    InputStreamReader(InputStream in, Charset cs)   //创建使用给定字符集的 InputStreamReader
    InputStreamReader(InputStream in, CharsetDecoder dec)   //创建使用给定字符集解码器的 InputStreamReader
    InputStreamReader(InputStream in, String charsetName)   //创建使用指定字符集的 InputStreamReader
    

    InputStreamReader类的方法

    void close() //关闭该流并释放与之关联的所有资源。
    String getEncoding() //返回此流使用的字符编码的名称。
    int read() //读取单个字符。
    int read(char[] cbuf, int offset, int length) //将字符读入数组中的某一部分。
    boolean ready() //判断此流是否已经准备好用于读取

    class TestClass {  
        public static void transReadNoBuf() throws IOException {  
             //读取字节流  
            //InputStream in = System.in;//读取键盘的输入。  
            InputStream in = new FileInputStream("D:\\java265.txt");//读取文件的数据。  
            //将字节流向字符流的转换。要启用从字节到字符的有效转换,  
            //可以提前从底层流读取更多的字节.  
            InputStreamReader isr = new InputStreamReader(in);//读取  
            //综合到一句。  
            //InputStreamReader isr = new InputStreamReader(  
            //new FileInputStream("D:\\java265.txt"));  
                  
            char []cha = new char[1024];  
            int len = isr.read(cha);  
            System.out.println(new String(cha,0,len));  
            isr.close();  
      
        }  
        public static void transReadByBuf() throws IOException {     
            //读取字节流  
            //InputStream in = System.in;//读取键盘上的数据  
            InputStream in = new FileInputStream("D:\\java265.txt");//读取文件上的数据。  
            //将字节流向字符流的转换。  
            InputStreamReader isr = new InputStreamReader(in);//读取  
            //创建字符流缓冲区  
            BufferedReader bufr = new BufferedReader(isr);//缓冲  
            //BufferedReader bufr = new BufferedReader(  
            //new InputStreamReader(new FileInputStream("D:\\java265.txt")));可以综合到一句。  
            /*int ch =0; 
            ch = bufr.read(); 
            System.out.println((char)ch); 
            */  
            String line;  
            while((line = bufr.readLine())!=null){  
                System.out.println(line);  
            }  
            isr.close();  
        }  
    }
    

  • 相关阅读:
    【Hive SQL 每日一题】找出各个商品销售额的中位数
    怎么理解 Spring 是一个 IoC 容器
    SV(1)- 数据类型
    win10+ubuntu双系统下载ubuntu方法(卸载系统不完整会进入grub)
    Android 13.0 系统设置 app详情页默认关闭流量数据的开关
    SAP数据元素描述增强修改
    1-(3-磺酸基)丙基-1-甲基-2-吡咯烷酮三氟甲磺酸盐[C3SO3Hnmp]CF3SO3
    文盘Rust——子命令提示,提高用户体验
    如何提取 x64 程序那些易失的方法参数
    BSV 上的私钥谜题
  • 原文地址:https://blog.csdn.net/qq_25073223/article/details/126259470