IO streams 6 --- FileReader use read (char [] cbuf) technology to read data --- Porter (still Silicon Valley)

  • FileReader character input stream
@Test
public void test2(){
    File file = new File("hello.txt");
    FileReader fr = null;
    try {
        fr = new FileReader(file);
        char[] cbuf = new char[5];
        //返回读取的长度
        int len;
        while ((len = fr.read(cbuf)) != -1){
            String str = new String(cbuf, 0, len);
            System.out.print(str);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if (fr != null){
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace (); 
            } 
        } 
    } 
}

  

Guess you like

Origin www.cnblogs.com/noyouth/p/11699248.html