當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。