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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。