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


Java CharArrayReader read(char[], int, int)用法及代碼示例


Java中CharArrayReader類的read(char [],int,int)方法用於將指定長度的字符以指定的偏移量讀取到數組中。此方法將流阻塞到:

  • 它已從流中獲取了一些輸入。
  • 發生了一些IOException
  • 閱讀時已到達流的末尾。

用法:

public int read(char[] charArray, int offset, int length)



參數:此方法接受三個強製參數:

  • charArray這是要寫入流中的字符數組。
  • offset這是要在數組中寫入字符的偏移量索引。
  • length這是要寫入數組的字符數。

返回值:此方法返回一個整數值,該整數值是從流中讀取的字符數。如果未讀取任何字符,則返回-1。

異常:此方法引發以下異常:

  • IOException:輸入輸出時發生錯誤。
  • IndexOutOfBoundsException:如果偏移值不在字符數組範圍內

以下方法說明了read(char [],int,int)方法的用法方式:

程序1:

// Java program to demonstrate 
// CharArrayReader read(char[], int, int) method 
  
import java.io.*; 
import java.util.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        try { 
  
            char[] str = { 'G', 'e', 'e', 'k', 's', 
                           'F', 'o', 'r', 
                           'G', 'e', 'e', 'k', 's' }; 
  
            // Create a CharArrayReader instance 
            CharArrayReader reader 
                = new CharArrayReader(str); 
  
            // Get the character array 
            // to be read from the stream 
            char[] charArray = new char[5]; 
  
            // Get the offset index 
            int offset = 0; 
  
            // Get the length 
            int length = 5; 
  
            // Read the charArray 
            // to this reader using read() method 
            // This will put the str in the stream 
            // till it is read by the reader 
            reader.read(charArray, offset, length); 
  
            // Print the read charArray 
            System.out.println( 
                Arrays.toString(charArray)); 
  
            reader.close(); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
輸出:
[G, e, e, k, s]

程序2:

// Java program to demonstrate 
// CharArrayReader read(char[], int, int) method 
  
import java.io.*; 
import java.util.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        try { 
  
            char[] str = { 'G', 'e', 'e', 'k', 's', 
                           'F', 'o', 'r', 
                           'G', 'e', 'e', 'k', 's' }; 
  
            // Create a CharArrayReader instance 
            CharArrayReader reader 
                = new CharArrayReader(str); 
  
            // Get the character array 
            // to be read from the stream 
            char[] charArray 
                = new char[13]; 
  
            // Get the offset index 
            int offset = 0; 
  
            // Get the length 
            int length = 13; 
  
            // Read the charArray 
            // to this reader using read() method 
            // This will put the str in the stream 
            // till it is read by the reader 
            reader.read(charArray, offset, length); 
  
            // Print the read charArray 
            System.out.println( 
                Arrays.toString(charArray)); 
  
            reader.close(); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
輸出:
[G, e, e, k, s, F, o, r, G, e, e, k, s]

參考:<a href =“ https://docs.oracle.com/javase/9​​/docs/api/java/io/CharArrayReader.html#read-char:A-int-int-” https://docs.oracle.com/javase/9 /docs /api /java /io /CharArrayReader.html#read-char:A-int-int-



相關用法


注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 CharArrayReader read(char[], int, int) method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。