JAVA伊奥。Java中的CharArrayReader类

CharArrayReader Class in Java

null

JAVA伊奥。CharArrayReader 类使用字符数组创建字符缓冲区。

宣言:

public class CharArrayReader   extends Reader

建造师:

  • CharArrayReader(char[]char_数组): 从指定的字符数组创建CharArrayReader。
  • CharArrayReader(char[]char_数组,int offset,int maxlen): 从字符数组的指定部分创建CharArrayReader。

方法:

  • read():java。伊奥。CharArrayReader。读() 读取单个字符,如果到达流的末尾,则返回-1。 语法:
public int read()Parameters : -----------Return  :Returns read character as an integer ranging from range 0 to 65535.-1 : when end of file is reached.
  • read(char[]char_数组,int offset,int maxlen):java。伊奥。CharArrayReader。读取(char[]char_数组,int offset,int maxlen)) 读取单个字符,如果到达流的末尾,则返回-1 语法:
public int read(char[] char_array, int offset, int maxlen))Parameters : char_array : destination array  offset : starting position from where to store charactersmaxlen : maximum no. of characters to be readReturn  :Returns all the characters read-1 : when end of file is reached.
  • ready():java。伊奥。CharArrayReader。就绪() 检查流是否准备好读取。 CharArrayReader随时都可以阅读。 语法:
public boolean ready()Parameters : -----------Return  :true if CharArrayReader is ready to be read.
  • 跳过(长字符):java。伊奥。CharArrayReader。跳过(长字符) 跳过字符数。如果n为负,则此方法不执行任何操作并返回0。 语法:
public long skip(long char)Parameters : char_no : char no. of characters to be skippedReturn  :no. of characters skippedException : IOException : In case of I/O error occurs

JAVA

// Java program illustrating the working of CharArrayReader class methods
// read(), skip(), ready()
// read(char[] char_array, int offset, int maxlen)
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// Initializing the character array
char [] geek = { 'G' , 'E' , 'E' , 'K' , 'S' };
// Initializing the char_array
CharArrayReader char_array1 = new CharArrayReader(geek);
CharArrayReader char_array2 = new CharArrayReader(geek);
// Use of ready() method
boolean check1 = char_array1.ready();
if (check1 == true )
System.out.println( "char_array1 is ready" );
else
System.out.println( "char_array1 is not ready" );
int a = 0 ;
System.out.print( "Use of read() method : " );
// Use of read() method : reading each character one by one
while ((a = char_array1.read()) != - 1 )
{
char c1 = ( char )a;
System.out.println(c1);
// Use of skip() method
long char_no = char_array1.skip( 1 );
System.out.println( "Characters Skipped : " +(c1+ 1 ));
}
System.out.println( "" );
// Use of ready() method
boolean check2 = char_array2.ready();
if (check2 == true )
System.out.println( "char_array2 is ready" );
else
System.out.println( "char_array2 is not ready" );
// Use of read(char[] char_array, int offset, int maxlen) : reading a part of array
char_array2.read(geek, 1 , 2 );
int b = 0 ;
System.out.print( "Use of read(char[] char_array, int offset, int maxlen) method : " );
while ((b = char_array2.read()) != - 1 )
{
char c2 = ( char )b;
System.out.print(c2);
}
}
}


输出:

char_array1 is readyUse of read() method : GCharacters Skipped : 72ECharacters Skipped : 70SCharacters Skipped : 84char_array2 is readyUse of read(char[] char_array, int offset, int maxlen) method : EKS
  • mark(int readLimit):java。伊奥。CharArrayReader。标记(int readLimit) 标记流中可读取字符的当前位置。此方法始终调用reset()方法。对reset()的后续调用会将流重新定位到此点。 语法:
public long mark(int readLimit)Parameters : readLimit : No. of characters that can be read up to the markReturn  :voidException : IOException : In case of I/O error occurs
  • markSupported():java。伊奥。CharArrayReader。markSupported() 说明流是否支持mark方法。 语法:
public boolean markSupported()Parameters : -------Return  :true if the mark method is supported by the streamException : IOException : In case of I/O error occurs
  • reset():java。伊奥。CharArrayReader。重置() 将流重置为最近的标记,如果从未标记,则重置为开始。 语法:
public void reset()Parameters : -------Return  :voidException : IOException : In case of I/O error occurs
  • close():java。伊奥。CharArrayReader。关闭() 关闭流并重新分配分配给它的资源。 语法:
public void close()Parameters : -------Return  :voidException : IOException : In case of I/O error occurs

JAVA

// Java program illustrating the working of FilterInputStream method
// mark(), reset()
// markSupported(), close()
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws Exception
{
// Initializing CharArrayReader
CharArrayReader char_array = null ;
char [] geek = { 'H' , 'E' , 'L' , 'L' , 'O' , 'G' , 'E' , 'E' , 'K' , 'S' };
try
{
char_array = new CharArrayReader(geek);
// read() method : reading and printing Characters
// one by one
System.out.println( "Char : " +( char )char_array.read());
System.out.println( "Char : " +( char )char_array.read());
System.out.println( "Char : " +( char )char_array.read());
// mark() : read limiing the 'geek' input stream
char_array.mark( 0 );
System.out.println( "mark() method comes to play" );
System.out.println( "Char : " +( char )char_array.read());
System.out.println( "Char : " +( char )char_array.read());
System.out.println( "Char : " +( char )char_array.read());
// Use of markSupported() :
boolean check = char_array.markSupported();
if (check == true )
System.out.println( "mark() supported" );
if (char_array.markSupported())
{
// reset() method : repositioning the stream to
// marked positions.
char_array.reset();
System.out.println( "reset() invoked" );
System.out.println( "Char : " +( char )char_array.read());
System.out.println( "Char : " +( char )char_array.read());
}
else
System.out.println( "mark() method not supported." );
}
catch (Exception excpt)
{
// in case of I/O error
excpt.printStackTrace();
}
finally
{
// Use of close() : releasing the resources back to the
// GarbageCollector when closes
if (char_array != null )
char_array.close();
}
}
}


输出:

Char : HChar : EChar : Lmark() method comes to playChar : LChar : OChar : Gmark() supportedreset() invokedChar : LChar : O

本文由 莫希特·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享