• 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();
            }
        }
    }
    
  • 相关阅读:
    [CF1042F]Leaf Sets
    [CF1051F]The Shortest Statement
    [洛谷P1792][国家集训队]种树
    [CF484E]Sign on Fence
    [洛谷P2216][HAOI2007]理想的正方形
    [洛谷P4389]付公主的背包
    [洛谷P4726]【模板】多项式指数函数
    服务器上Ubuntu系统安装
    删除ubuntu系统
    Win10下安装Ubuntu16.04双系统
  • 原文地址:https://www.cnblogs.com/ming-szu/p/8905737.html
Copyright © 2020-2023  润新知