zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
    
  • 相关阅读:
    大型网站随着业务的增长架构演进
    springboot日志logback配置
    一些容易出错的细节
    从一个下载优化说起
    徒手优化冒泡排序
    php设计模式之观察者模式
    php设计模式之抽象工厂模式
    phper談談最近重構代碼的感受(3)
    php设计模式----工厂模式
    偏执的我从Linux到Windows的感受
  • 原文地址:https://www.cnblogs.com/ming-szu/p/8905737.html
Copyright © 2011-2022 走看看