IO--int read(char cbuf[],int off,int len)

1.InputStreamReader  

Reader与Writer是基于字符的IO操作接口,而InputStreamReader的read方法就是以字符为单位的读方法

/**
     * Reads characters into a portion of an array.
     *
     * @param      cbuf     Destination buffer
     * @param      offset   Offset at which to start storing characters
     * @param      length   Maximum number of characters to read
     *
     * @return     The number of characters read, or -1 if the end of the
     *             stream has been reached
     *
     * @exception  IOException  If an I/O error occurs
     */
    public int read(char cbuf[], int offset, int length) throws IOException {
        return sd.read(cbuf, offset, length);
    }

 三个参数:cbuf[]是char数组用于储存读到的字符,offset是指从cbuf[]第几位开始储存而不是指从读文件第几个字符开始读,length指每次读多少位

使用例程

package IO;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class testReader {
    public static void main(String argv[]){new testReader().testRead();}

    public void testRead(){
        try{
            StringBuffer sb=new StringBuffer();
            char[] cBuf=new char[1024];
            File file=new File("D:\javaCode\testSE\src\IO\file.txt");
            if(!file.exists()){
                System.out.println("not exists");
                return;
            }
            FileReader f=new FileReader(file);
            while(f.read(cBuf,10,2)>0){
                sb.append(cBuf);
                System.out.println(sb);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
原文地址:https://www.cnblogs.com/ming-szu/p/8905737.html