java.nio.CharBuffer類的rewind()方法用於倒帶此緩衝區。位置設置為零,標記被丟棄。假設已正確設置了限製,請在序列channel-write或get操作之前調用此方法。調用此方法既不會更改,也不會丟棄標記的值。
用法:
public ByteBuffer rewind()
返回值:此方法返回此緩衝區。
下麵是說明rewind()方法的示例:
範例1:
// Java program to demonstrate
// rewind() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// defining and allocating CharBuffer
// using allocate() method
CharBuffer charBuffer = CharBuffer.allocate(4);
// put char value in charBuffer
// using put() method
charBuffer.put('a');
charBuffer.put((char)105);
// print the char buffer
System.out.println("Buffer before operation:"
+ Arrays.toString(
charBuffer.array())
+ "\nPosition:"
+ charBuffer.position()
+ "\nLimit:"
+ charBuffer.limit());
// rewind the Buffer
// using rewind() method
charBuffer.rewind();
// print the charbuffer
System.out.println("\nBuffer after operation:"
+ Arrays.toString(
charBuffer.array())
+ "\nPosition:"
+ charBuffer.position()
+ "\nLimit:"
+ charBuffer.limit());
}
}
輸出:
Buffer before operation:[a, i,, ] Position:2 Limit:4 Buffer after operation:[a, i,, ] Position:0 Limit:4
範例2:
// Java program to demonstrate
// rewind() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// defining and allocating CharBuffer
// using allocate() method
CharBuffer charBuffer
= CharBuffer.allocate(5);
// put byte value in charBuffer
// using put() method
charBuffer.put('a');
charBuffer.put('b');
charBuffer.put('c');
// mark will be going to discarded by rewind()
charBuffer.mark();
// print the buffer
System.out.println("Buffer before operation:"
+ Arrays.toString(
charBuffer.array())
+ "\nPosition:"
+ charBuffer.position()
+ "\nLimit:"
+ charBuffer.limit());
// Rewind the Buffer
// using rewind() method
charBuffer.rewind();
// print the buffer
System.out.println("\nBuffer after operation:"
+ Arrays.toString(
charBuffer.array())
+ "\nPosition:"
+ charBuffer.position()
+ "\nLimit:"
+ charBuffer.limit());
}
}
輸出:
Buffer before operation:[a, b, c,, ] Position:3 Limit:5 Buffer after operation:[a, b, c,, ] Position:0 Limit:5
參考: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#rewind-
相關用法
- Java ByteBuffer rewind()用法及代碼示例
- Java FloatBuffer rewind()用法及代碼示例
- Java Buffer rewind()用法及代碼示例
- Java CharBuffer read()用法及代碼示例
- Java CharBuffer order()用法及代碼示例
- Java CharBuffer mark()用法及代碼示例
- Java CharBuffer limit()用法及代碼示例
- Java CharBuffer position()用法及代碼示例
- Java CharBuffer length()用法及代碼示例
- Java CharBuffer reset()用法及代碼示例
- Java CharBuffer clear()用法及代碼示例
- Java CharBuffer subSequence()用法及代碼示例
- Java CharBuffer charAt()用法及代碼示例
- Java CharBuffer append()用法及代碼示例
- Java CharBuffer flip()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 CharBuffer rewind() methods in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。