Java的字符读写类CharArrayReader和CharArrayWriter使用示例
CharArrayReader
CharArrayReader是字符数组输入流。它和ByteArrayInputStream类似,只不过ByteArrayInputStream是字节数组输入流,而CharArray是字符数组输入流。CharArrayReader是用于读取字符数组,它继承于Reader。操作的数据是以字符为单位!
CharArrayReader函数列表:
CharArrayReader(char[]buf) CharArrayReader(char[]buf,intoffset,intlength) voidclose() voidmark(intreadLimit) booleanmarkSupported() intread() intread(char[]buffer,intoffset,intlen) booleanready() voidreset() longskip(longcharCount)
示例代码:
关于CharArrayReader中API的详细用法,参考示例代码(CharArrayReaderTest.java):
importjava.io.CharArrayReader; importjava.io.CharArrayWriter; importjava.io.IOException; publicclassCharArrayReaderTest{ privatestaticfinalintLEN=5; //对应英文字母“abcdefghijklmnopqrstuvwxyz” privatestaticfinalchar[]ArrayLetters=newchar[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; publicstaticvoidmain(String[]args){ tesCharArrayReader(); } /** *CharArrayReader的API测试函数 */ privatestaticvoidtesCharArrayReader(){ try{ //创建CharArrayReader字符流,内容是ArrayLetters数组 CharArrayReadercar=newCharArrayReader(ArrayLetters); //从字符数组流中读取5个字符 for(inti=0;i<LEN;i++){ //若能继续读取下一个字符,则读取下一个字符 if(car.ready()==true){ //读取“字符流的下一个字符” chartmp=(char)car.read(); System.out.printf("%d:%c\n",i,tmp); } } //若“该字符流”不支持标记功能,则直接退出 if(!car.markSupported()){ System.out.println("makenotsupported!"); return; } //标记“字符流中下一个被读取的位置”。即--标记“f”,因为因为前面已经读取了5个字符,所以下一个被读取的位置是第6个字符” //(01),CharArrayReader类的mark(0)函数中的“参数0”是没有实际意义的。 //(02),mark()与reset()是配套的,reset()会将“字符流中下一个被读取的位置”重置为“mark()中所保存的位置” car.mark(0); //跳过5个字符。跳过5个字符后,字符流中下一个被读取的值应该是“k”。 car.skip(5); //从字符流中读取5个数据。即读取“klmno” char[]buf=newchar[LEN]; car.read(buf,0,LEN); System.out.printf("buf=%s\n",String.valueOf(buf)); //重置“字符流”:即,将“字符流中下一个被读取的位置”重置到“mark()所标记的位置”,即f。 car.reset(); //从“重置后的字符流”中读取5个字符到buf中。即读取“fghij” car.read(buf,0,LEN); System.out.printf("buf=%s\n",String.valueOf(buf)); }catch(IOExceptione){ e.printStackTrace(); } } }
运行结果:
0:a 1:b 2:c 3:d 4:e buf=klmno buf=fghij
CharArrayWriter
CharArrayReader用于写入数据符,它继承于Writer。操作的数据是以字符为单位!
CharArrayWriter函数列表
CharArrayWriter() CharArrayWriter(intinitialSize) CharArrayWriterappend(CharSequencecsq,intstart,intend) CharArrayWriterappend(charc) CharArrayWriterappend(CharSequencecsq) voidclose() voidflush() voidreset() intsize() char[]toCharArray() StringtoString() voidwrite(char[]buffer,intoffset,intlen) voidwrite(intoneChar) voidwrite(Stringstr,intoffset,intcount) voidwriteTo(Writerout)
示例代码:
关于CharArrayWriter中API的详细用法,参考示例代码(CharArrayWriterTest.java):
importjava.io.CharArrayReader; importjava.io.CharArrayWriter; importjava.io.IOException; publicclassCharArrayWriterTest{ privatestaticfinalintLEN=5; //对应英文字母“abcdefghijklmnopqrstuvwxyz” privatestaticfinalchar[]ArrayLetters=newchar[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; publicstaticvoidmain(String[]args){ tesCharArrayWriter(); } /** *CharArrayWriter的API测试函数 */ privatestaticvoidtesCharArrayWriter(){ try{ //创建CharArrayWriter字符流 CharArrayWritercaw=newCharArrayWriter(); //写入“A”个字符 caw.write('A'); //写入字符串“BC”个字符 caw.write("BC"); //System.out.printf("caw=%s\n",caw); //将ArrayLetters数组中从“3”开始的后5个字符(defgh)写入到caw中。 caw.write(ArrayLetters,3,5); //System.out.printf("caw=%s\n",caw); //(01)写入字符0 //(02)然后接着写入“123456789” //(03)再接着写入ArrayLetters中第8-12个字符(ijkl) caw.append('0').append("123456789").append(String.valueOf(ArrayLetters),8,12); System.out.printf("caw=%s\n",caw); //计算长度 intsize=caw.size(); System.out.printf("size=%s\n",size); //转换成byte[]数组 char[]buf=caw.toCharArray(); System.out.printf("buf=%s\n",String.valueOf(buf)); //将caw写入到另一个输出流中 CharArrayWritercaw2=newCharArrayWriter(); caw.writeTo(caw2); System.out.printf("caw2=%s\n",caw2); }catch(IOExceptione){ e.printStackTrace(); } } }
运行结果:
caw=ABCdefgh0123456789ijkl size=22 buf=ABCdefgh0123456789ijkl caw2=ABCdefgh0123456789ijkl