zoukankan      html  css  js  c++  java
  • Java——IO类,字符流读数据

    InputStreamReader读数据

    █ InputStreamReader读数据方法

         ☞public int read();              //读取单个字符。
         ☞public int read(char[] cbuf);             //将字符读入数组。
         ☞public int read(char[] cbuf,int off ,int len);            //将字符读入数组中的某一部分。

    public static void main(String[] args) throws IOException {

                    FileInputStream fis = new FileInputStream("1.txt");
                    InputStreamReader isr = new InputStreamReader(fis);   //没有写明编码格式,默认系统编码(utf-8);

                    //方法一:
            /*      int ch;
                    // while ((ch = (char) isr.read()) != -1) { //这里不能在这里强行转换成 char ,转换成 char 就永远不会等于 -1
                    while ((ch = isr.read()) != -1) {
                            System.out.print((char) ch);
                    } */

                    //方法二:
            /*     char[] charray = new char[10];
                    int leng = isr.read(charray);
                    System.out.println(charray);*/
    //方法三:
            /*     char[] charray2 = new char[10];
                    int leng2 = isr.read(charray2, 0,charray2.length);
                    System.out.println(charray2);*/

                    char[] charray2 = new char[10];
                    int leng;
                    while(  ( leng = isr.read(charray2,0,charray2.length )  ) != -1 ){
                            //System.out.print(charray2);      //这里不能直接输出数组,因为不能最后一才读进数组长度个数据,会导致输出的部分是上次读到数组里的
                            String charray2Str = new String(charray2,0,leng);
                            System.out.print(charray2Str);
                    }

                    fis.close();
                    isr.close();
            }
    }































  • 相关阅读:
    LeetCode: Text Justification 解题报告
    LeetCode: Evaluate Reverse Polish Notation 解题报告
    telnet服务搭建
    CentOS 7 快速部署 ELK
    CSVN配置自动备份策略
    使用mysqlproxy实现mysql读写分离
    linux挂载windows共享文件夹
    Centos7.3使用花生壳映射端口
    rpm使用
    linux运维注意事项
  • 原文地址:https://www.cnblogs.com/meihao1203/p/9181971.html
Copyright © 2011-2022 走看看