当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.io.StringBufferInputStream用法及代码示例


io.StringBufferInputStream class in Java

java.io.StringBufferInputStream类有助于创建一个输入流,可以在其中读取字符串中的字节。如果我们使用这个类,我们只能读取字符串中每个字符的低 8 位。
但如果我们使用Java.io.ByteArrayInputStream,不限制只读取字符串中每个字符的低 8 位。
Oracle 已弃用此类,不应再使用。

声明:

public class StringBufferInputStream
   extends InputStream

构造函数:

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

方法:

  • read():java.io.StringBufferInputStream.read()从输入流中读取数据字节,如果到达流末尾则返回 -1。
    Syntax :
    public int read()
    Parameters : 
    -----------
    Return  :
    Returns read character as an integer ranging from range 0 to 65535.
    -1 : when end of file is reached.
  • 读取(字节[]缓冲区,int偏移量,int maxlen):java.io.StringBufferInputStream.read(byte[] 缓冲区, int 偏移量, int maxlen))从缓冲区中从偏移位置开始读取数据字节,直到 maxlen,如果到达流末尾,则返回 -1。
    Syntax :
    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.
  • reset():java.io.StringBufferInputStream.reset()重置输入流并从输入流中存在的 ‘buffer’ 的第一个字符开始读取。
    Syntax :
    public void reset()
    Parameters : 
    -----------
    Return  :
    void
    
  • 跳过(长b):java.io.StringBufferInputStream.skip(long b)跳过 ‘b’ 字节。如果到达文件末尾,则会跳过几个字节。
    Syntax :
    public long skip(long b)
    Parameters : 
    b : no. of bytes to be skipped
    Return  :
    no. of bytes skipped
    
  • available():java.io.StringBufferInputStream.available()告诉总数。可供读取的字节数。
    Syntax :
    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("\nUse 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

.



相关用法


注:本文由纯净天空筛选整理自佚名大神的英文原创作品 Java.io.StringBufferInputStream Class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。