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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM