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();
            }
    }































  • 相关阅读:
    luogu_1659【题解】manacher 啦啦队排练
    manacher算法
    luogu_4503【题解】企鹅QQ 哈希
    luogu_3966【题解】单词 AC自动机
    字符串 AC自动机
    luogu_3275【题解】糖果 差分约束
    luogu_4568 飞行路线 分层图
    luogu_4551【题解】最长异或路径 trie树
    luogu_1041【题解】搜索 传染病控制
    [题解/模板]扫描线
  • 原文地址:https://www.cnblogs.com/meihao1203/p/9181971.html
Copyright © 2011-2022 走看看