IO--int read(char cbuf[],int off,int len)
Posted ming_szu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(); } } }
以上是关于IO--int read(char cbuf[],int off,int len)的主要内容,如果未能解决你的问题,请参考以下文章
IO流之BufferedReader/BufferedWriter