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


Java CharBuffer read()用法及代碼示例


java.nio.CharBuffer類的read()方法用於將字符讀入指定的字符緩衝區。該緩衝區用作字符as-is的存儲庫:所做的唯一更改是放置操作的結果。不執行緩衝區的翻轉或倒帶。

用法:

public int read(CharBuffer target)

參數:此方法使用緩衝區讀取字符。


返回值:此方法返回添加到緩衝區的字符數;如果此字符源在其末尾,則返回-1。

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

  • IOException-如果發生I /O錯誤
  • NullPointerException -如果target為null
  • ReadOnlyBufferException-如果目標是隻讀緩衝區

下麵是說明read()方法的示例:

範例1:

// Java program to demonstrate 
// read() method 
  
import java.nio.*; 
import java.util.*; 
import java.io.IOException; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        try { 
  
            // Declare and initialize the char array 
            char[] cb1 = { 'x', 'y', 'z' }; 
            char[] cb2 = { 'a', 'b', 'c', 'd', 'e' }; 
  
            // wrap the char array into CharBuffer 
            // using wrap() method 
            CharBuffer charBuffer1 
                = CharBuffer.wrap(cb1); 
  
            // wrap the char array into CharBuffer 
            // using wrap() method 
            CharBuffer charBuffer2 
                = CharBuffer.wrap(cb2); 
  
            // print the byte buffer 
            System.out.println("CharBuffer Before operation is:"
                               + Arrays.toString( 
                                     charBuffer1.array()) 
                               + "\nTarget Charbuffer:"
                               + Arrays.toString( 
                                     charBuffer2.array())); 
  
            // Get the value of the number of Character 
            // read from the charBuffer 
            // using read() method 
            int value 
                = charBuffer1 
                      .read(charBuffer2); 
  
            // print the byte buffer 
            System.out.println("\nCharBuffer After operation is:"
                               + Arrays.toString( 
                                     charBuffer1.array()) 
                               + "\nTarget Charbuffer:"
                               + Arrays.toString( 
                                     charBuffer2.array()) 
                               + "\nno of value changed:"
                               + value); 
        } 
        catch (IOException e) { 
            System.out.println("an I/O error occurs"); 
            System.out.println("Exception throws:" + e); 
        } 
        catch (NullPointerException e) { 
            System.out.println("target charbuffer is null"); 
            System.out.println("Exception throws:" + e); 
        } 
        catch (ReadOnlyBufferException e) { 
            System.out.println("target is a read only buffer"); 
            System.out.println("Exception throws:" + e); 
        } 
    } 
}
輸出:
CharBuffer Before operation is:[x, y, z]
Target Charbuffer:[a, b, c, d, e]

CharBuffer After operation is:[x, y, z]
Target Charbuffer:[x, y, z, d, e]
no of value changed:3

範例2:對於NullPointerException

// Java program to demonstrate 
// read() method 
  
import java.nio.*; 
import java.util.*; 
import java.io.IOException; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        try { 
  
            // Declare and initialize the char array 
            char[] cb1 = { 'x', 'y', 'z' }; 
  
            // wrap the char array into CharBuffer 
            // using wrap() method 
            CharBuffer charBuffer1 
                = CharBuffer.wrap(cb1); 
  
            // print the byte buffer 
            System.out.println("CharBuffer Before operation is:"
                               + Arrays.toString( 
                                     charBuffer1.array())); 
  
            // Get the value of number of Character 
            // read from the charBuffer 
            // using read() method 
            int value = charBuffer1.read(null); 
        } 
  
        catch (IOException e) { 
            System.out.println("\nan I/O error occurs"); 
            System.out.println("Exception throws:" + e); 
        } 
  
        catch (NullPointerException e) { 
            System.out.println("\ntarget charbuffer is null"); 
            System.out.println("Exception throws:" + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
            System.out.println("\ntarget is a read only buffer"); 
            System.out.println("Exception throws:" + e); 
        } 
    } 
}
輸出:
CharBuffer Before operation is:[x, y, z]

target charbuffer is null
Exception throws:java.lang.NullPointerException

範例3:對於ReadOnlyBufferException

// Java program to demonstrate 
// read() method 
  
import java.nio.*; 
import java.util.*; 
import java.io.IOException; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        try { 
  
            // Declare and initialize the char array 
            char[] cb1 = { 'x', 'y', 'z' }; 
            char[] cb2 = { 'a', 'b', 'c', 'd', 'e' }; 
  
            // wrap the char array into CharBuffer 
            // using wrap() method 
            CharBuffer charBuffer1 
                = CharBuffer.wrap(cb1); 
  
            // wrap the char array into CharBuffer 
            // using wrap() method 
            CharBuffer charBuffer2 
                = CharBuffer.wrap(cb2); 
  
            // print the byte buffer 
            System.out.println("CharBuffer Before operation is:"
                               + Arrays.toString( 
                                     charBuffer1.array()) 
                               + "\nTarget Charbuffer:"
                               + Arrays.toString( 
                                     charBuffer2.array())); 
  
            // converting Charbuffer to readonlybuff 
            CharBuffer readonlybuff 
                = charBuffer2.asReadOnlyBuffer(); 
  
            // Get the value of number of Character 
            // read from the charBuffer 
            // using read() method 
            int value = charBuffer1.read(readonlybuff); 
  
            // print the byte buffer 
            System.out.println("\nCharBuffer After operation is:"
                               + Arrays.toString( 
                                     charBuffer1.array()) 
                               + "\nTarget Charbuffer:"
                               + Arrays.toString( 
                                     charBuffer2.array()) 
                               + "\nno of value changed:"
                               + value); 
        } 
        catch (IOException e) { 
            System.out.println("\nan I/O error occurs"); 
            System.out.println("Exception throws:" + e); 
        } 
        catch (NullPointerException e) { 
            System.out.println("\ntarget charbuffer is null"); 
            System.out.println("Exception throws:" + e); 
        } 
        catch (ReadOnlyBufferException e) { 
            System.out.println("\ntarget is a read only buffer"); 
            System.out.println("Exception throws:" + e); 
        } 
    } 
}
輸出:
CharBuffer Before operation is:[x, y, z]
Target Charbuffer:[a, b, c, d, e]

target is a read only buffer
Exception throws:java.nio.ReadOnlyBufferException

參考: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#read-java.nio.CharBuffer-



相關用法


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