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


Java ByteBuffer getChar()用法及代碼示例


getChar()

java.nio.ByteBuffer類的getChar()方法用於獲取讀取char值的方法

讀取此緩衝區當前位置的下兩個字節,根據當前字節順序將它們組成一個char值,然後將該位置加2。

用法:


public abstract char getChar()

返回值:此方法返回緩衝區當前位置的char值

拋出:此方法拋出BufferUnderflowException-如果緩衝區的當前位置不小於其限製,則拋出此異常。

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

範例1:

// Java program to demonstrate 
// getChar() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the ByteBuffer 
        int capacity = 50; 
  
        // Creating the ByteBuffer 
        try { 
  
            // creating object of ByteBuffer 
            // and allocating size capacity 
            ByteBuffer bb = ByteBuffer.allocate(capacity); 
  
            // putting the string in the bytebuffer 
            bb.asCharBuffer().put("Geeks"); 
  
            // rewind the Bytebuffer 
            bb.rewind(); 
  
            // Declaring the variable 
            char c; 
  
            // print the ByteBuffer 
            System.out.println("Original ByteBuffer: "); 
            while ((c = bb.getChar()) != 0) 
                System.out.print(c + " "); 
  
            // rewind the Bytebuffer 
            bb.rewind(); 
  
            // Reads the char at this buffer's current position 
            // using getChar() method 
            char value = bb.getChar(); 
  
            // print the char value 
            System.out.println("\n\nByte Value: " + value); 
  
            // Reads the  char at this buffer's next position 
            // using getChar() method 
            char value1 = bb.getChar(); 
  
            // print the char value 
            System.out.print("\nNext Byte Value: " + value1); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("\nException Thrown: " + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("\nException Thrown: " + e); 
        } 
  
        catch (BufferUnderflowException e) { 
  
            System.out.println("\nException Thrown: " + e); 
        } 
    } 
}
輸出:
Original ByteBuffer: 
G e e k s 

Byte Value: G

Next Byte Value: e

範例2:

// Java program to demonstrate 
// getChar() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the ByteBuffer 
        int capacity = 8; 
  
        // Creating the ByteBuffer 
        try { 
  
            // creating object of ByteBuffer 
            // and allocating size capacity 
            ByteBuffer bb = ByteBuffer.allocate(capacity); 
  
            // putting the string in the bytebuffer 
            bb.asCharBuffer().put("abc"); 
  
            // rewind the Bytebuffer 
            bb.rewind(); 
  
            // Declaring the variable 
            char c; 
  
            // print the ByteBuffer 
            System.out.print("Original ByteBuffer: "); 
            while ((c = bb.getChar()) != 0) 
                System.out.print(c + " "); 
  
            // rewind the Bytebuffer 
            bb.rewind(); 
  
            // Reads the char at this buffer's current position 
            // using getChar() method 
            char value = bb.getChar(); 
  
            // print the char value 
            System.out.println("\n\nFirst char Value: " + value); 
  
            // Reads the  char at this buffer's next position 
            // using getChar() method 
            char value1 = bb.getChar(); 
  
            // print the char value 
            System.out.println("\nSecond char Value: " + value1); 
  
            // Reads the  char at this buffer's next position 
            // using getChar() method 
            char value2 = bb.getChar(); 
  
            // print the char value 
            System.out.println("\nThird char Value: " + value2); 
  
            // Reads the  char at this buffer's next position 
            // using getChar() method 
            System.out.print("\nsince the buffer current position is incremented"); 
            System.out.print(" to greater than its limit "); 
            char value3 = bb.getChar(); 
            char value4 = bb.getChar(); 
        } 
  
        catch (BufferOverflowException e) { 
  
            System.out.println("\nException Thrown: " + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("\nException Thrown: " + e); 
        } 
  
        catch (BufferUnderflowException e) { 
  
            System.out.println("\nException Thrown: " + e); 
        } 
    } 
}
輸出:
Original ByteBuffer: a b c 

First char Value: a

Second char Value: b

Third char Value: c

since the buffer current position is incremented to greater than its limit 
Exception Thrown: java.nio.BufferUnderflowException
get(int索引)

ByteBuffer的get(int index)方法用於讀取給定索引處的兩個字節,並根據當前字節順序將它們組成一個char值。

用法:

public abstract char getChar(int index)

參數:此方法將索引(將從中讀取Byte的索引)作為參數。


返回值:此方法返回給定索引處的char值。

異常:此方法引發IndexOutOfBoundsException。如果index為負或不小於緩衝區的限製,則拋出此異常。

以下示例說明了get(int index)方法:

範例1:

// Java program to demonstrate 
// getChar() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the ByteBuffer 
        int capacity = 50; 
  
        // Creating the ByteBuffer 
        try { 
  
            // creating object of ByteBuffer 
            // and allocating size capacity 
            ByteBuffer bb = ByteBuffer.allocate(capacity); 
  
            // putting the string in the bytebuffer 
            bb.asCharBuffer().put("abc"); 
  
            // rewind the Bytebuffer 
            bb.rewind(); 
  
            // Declaring the variable 
            char c; 
  
            // print the ByteBuffer 
            System.out.print("Original ByteBuffer: "); 
            while ((c = bb.getChar()) != 0) 
                System.out.print(c + " "); 
  
            // rewind the Bytebuffer 
            bb.rewind(); 
  
            // Reads the char at this buffer's at index 0 
            // using getChar() method 
            char value0 = bb.getChar(0); 
  
            // print the char value 
            System.out.println("\n\nchar Value at index 0: "
                               + value0); 
  
            // Reads the  char at this buffer's at index 2 
            // using getChar() method 
            char value1 = bb.getChar(2); 
  
            // print the char value 
            System.out.println("\nchar Value at index 2: "
                               + value1); 
  
            // Reads the  char at this buffer's at index 4 
            // using getChar() method 
            char value2 = bb.getChar(4); 
  
            // print the char value 
            System.out.println("\nchar Value at index 4: "
                               + value2); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("\nException Thrown: " + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("\nException Thrown: " + e); 
        } 
  
        catch (BufferUnderflowException e) { 
  
            System.out.println("\nException Thrown: " + e); 
        } 
    } 
}
輸出:
Original ByteBuffer: a b c 

char Value at index 0: a

char Value at index 2: b

char Value at index 4: c

範例2:

// Java program to demonstrate 
// getChar() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the ByteBuffer 
        int capacity = 50; 
  
        // Creating the ByteBuffer 
        try { 
  
            // creating object of ByteBuffer 
            // and allocating size capacity 
            ByteBuffer bb = ByteBuffer.allocate(capacity); 
  
            // putting the string in the bytebuffer 
            bb.asCharBuffer().put("abc"); 
  
            // rewind the Bytebuffer 
            bb.rewind(); 
  
            // Declaring the variable 
            char c; 
  
            // print the ByteBuffer 
            System.out.print("Original ByteBuffer: "); 
            while ((c = bb.getChar()) != 0) 
                System.out.print(c + " "); 
  
            // rewind the Bytebuffer 
            bb.rewind(); 
  
            // Reads the char at this buffer's at index 0 
            // using getChar() method 
            char value0 = bb.getChar(0); 
  
            // print the char value 
            System.out.println("\n\nchar Value at index 0: "
                               + value0); 
  
            // Reads the  char at this buffer's at index 2 
            // using getChar() method 
            char value1 = bb.getChar(2); 
  
            // print the char value 
            System.out.println("\nchar Value at index 2: "
                               + value1); 
  
            // Reads the  char at this buffer's at index 4 
            // using getChar() method 
            System.out.println("\nTrying to get the char"
                               + " at negative index "); 
            char value2 = bb.getChar(-4); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
  
            System.out.println("\nException Thrown: "
                               + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("\nException Thrown: "
                               + e); 
        } 
  
        catch (BufferUnderflowException e) { 
  
            System.out.println("\nException Thrown: "
                               + e); 
        } 
    } 
}
輸出:
Original ByteBuffer: a b c 

char Value at index 0: a

char Value at index 2: b

Trying to get the char at a negative index 

Exception Thrown: java.lang.IndexOutOfBoundsException

參考:



相關用法


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