JAVA伊奥。Java中的StringBufferInputStream类

io.StringBufferInputStream class in Java

null

JAVA伊奥。StringBufferInputStream 类帮助创建一个输入流,在该流中,可以从字符串中读取字节。如果我们使用这个类,我们只能读取字符串中每个字符的低8位。 但如果我们使用 字节数组输入流 ,不限制只读取字符串中每个字符的低8位。 此类已被Oracle弃用,不应再使用。

宣言:

public class StringBufferInputStream
   extends InputStream

建造师:

  • StringBufferInputStream(字符串str): 创建字符串输入流以从指定字符串读取数据。

方法:

  • read(): JAVA伊奥。StringBufferInputStream。读() 从输入流读取字节数据,如果到达流的末尾,则返回-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.
  • 读取(字节[]缓冲区,整数偏移量,整数最大值): JAVA伊奥。StringBufferInputStream。读取(字节[]缓冲区,整数偏移量,整数最大值)) 从缓冲区读取从偏移位置到maxlen的数据字节,如果到达流的末尾,则返回-1。 语法:
    public int read(byte[] buffer, int offset, int maxlen))
    Parameters : 
    buffer : destination buffer to be read into  
    offset : starting position from where to store characters
    maxlen : maximum no. of characters to be read
    Return  :
    Returns all the characters read
    -1 : when end of file is reached.
  • 重置(): JAVA伊奥。StringBufferInputStream。重置() 重置输入流,并从输入流中存在的“缓冲区”的第一个字符开始读取。 语法:
    public void reset()
    Parameters : 
    -----------
    Return  :
    void
    
  • 跳过(长b): JAVA伊奥。StringBufferInputStream。跳过(长b) 跳过“b”字节。如果到达文件末尾,将跳过几个字节。 语法:
    public long skip(long b)
    Parameters : 
    b : no. of bytes to be skipped
    Return  :
    no. of bytes skipped
    
  • 可用(): JAVA伊奥。StringBufferInputStream。可用() 告诉可读取的字节总数。 语法:
    public int available()
    Parameters : 
    ----------------
    Return  :
    total no. of bytes that can be read
    

// Java program illustrating the working of StringBufferInputStream class methods
// read(), skip(), available(), reset()
// read(char[] char_array, int offset, int maxlen)
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
String str1 = "Hello Geeks" ;
String str2 = "GeeksForGeeks" ;
StringBufferInputStream Geek_buffer1 = new StringBufferInputStream(str1);
StringBufferInputStream Geek_buffer2 = new StringBufferInputStream(str2);
// USe of available() : to count total bytes to be read
System.out.println( "Use of available() 1 : " + Geek_buffer1.available());
int a = 0 ;
System.out.print( "Use of read() method : " );
// Use of read() method : reading each byte one by one
while ((a = Geek_buffer1.read()) != - 1 )
{
// Converting byte to char
char c1 = ( char )a;
System.out.println(c1);
// Use of skip() method
long char_no = Geek_buffer1.skip( 1 );
System.out.println( "Characters Skipped : " + (c1+ 1 ));
}
System.out.println( "" );
// USe of available() : to count total bytes to be read
System.out.println( "Use of available() 2 : " + Geek_buffer2.available());
byte [] buffer = new byte [ 15 ];
// Use of read(char[] char_array, int offset, int maxlen):
// reading a part of array
Geek_buffer2.read(buffer, 1 , 2 );
int b = 0 ;
System.out.print( "read(char[] char_array, int offset, int maxlen): " );
while ((b = Geek_buffer2.read()) != - 1 )
{
char c2 = ( char )b;
System.out.print(c2);
}
System.out.println( "" );
// Use of reset() : to reset str1 for reading again
Geek_buffer1.reset();
int i = 0 ;
System.out.print( "Use of read() method again after reset() : " );
// Use of read() method : reading each character one by one
while ((i = Geek_buffer1.read()) != - 1 )
{
char c3 = ( char )i;
System.out.print(c3);
}
}
}


输出:

Use of available() 1 : 11
Use of read() method : H
Characters Skipped : 73
l
Characters Skipped : 109
o
Characters Skipped : 112
G
Characters Skipped : 72
e
Characters Skipped : 102
s
Characters Skipped : 116

Use of available() 2 : 13
Use of read(char[] char_array, int offset, int maxlen) method : eksForGeeks

Use of read() method again after reset() : Hello Geeks

. 本文由 莫希特·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

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