Java CharBuffer flip()方法及示例

Java CharBuffer flip()方法及示例

java.nio.CharBuffer类flip() 方法是用来翻转这个缓冲区。限制被设置为当前位置,然后位置被设置为零。如果定义了标记,那么它将被丢弃。在一连串的通道读或放操作之后,调用这个方法为一连串的通道写或相对的获取操作做准备。

例如

buf.put(magic); // 预先添加标题

in.read(buf); // 将数据读入缓冲区的其余部分

buf.flip(); // 翻转缓冲区

out.write(buf); // 将标题+数据写入通道

当把数据从一个地方传输到另一个地方时,这个方法经常与 compact() 方法一起使用。

语法

public final CharBuffer flip()

返回值: 该方法返回这个缓冲区。

下面是说明flip()方法的例子。

例子 1 :

// Java program to demonstrate
// flip() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declare and initialize the char array
        char[] cb = { 'a', 'b', 'c' };
  
        // wrap the char array into CharBuffer
        // using wrap() method
        CharBuffer charBuffe
            r
            = CharBuffer.wrap(cb);
  
        // set position at index 1
        charBuffer.position(1);
  
        // print the char buffer
        System.out.println("CharBuffer before flip: "
                           + Arrays.toString(
                                 charBuffer.array())
                           + "\nPosition: "
                           + charBuffer.position()
                           + "\nLimit: "
                           + charBuffer.limit());
  
        // Flip the charBuffer
        // using flip() method
        charBuffer.flip();
  
        // print the byte buffer
        System.out.println("\nCharBuffer after flip: "
                           + Arrays.toString(
                                 charBuffer.array())
                           + "\nPosition: "
                           + charBuffer.position()
                           + "\nLimit: "
                           + charBuffer.limit());
    }
}

输出:

CharBuffer before flip: [a, b, c]
Position: 1
Limit: 3

CharBuffer after flip: [a, b, c]
Position: 0
Limit: 1

例子 2 :

// Java program to demonstrate
// flip() 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);
  
        // append char value in charBuffer
        // using append() method
        charBuffer.append('a');
        charBuffer.append('b');
  
        // print the char buffer
        System.out.println("CharBuffer before flip: "
                           + Arrays.toString(
                                 charBuffer.array())
                           + "\nPosition: "
                           + charBuffer.position()
                           + "\nLimit: "
                           + charBuffer.limit());
  
        // Flip the charBuffer
        // using flip() method
        charBuffer.flip();
  
        // print the char buffer
        System.out.println("CharBuffer before flip: "
                           + Arrays.toString(
                                 charBuffer.array())
                           + "\nPosition: "
                           + charBuffer.position()
                           + "\nLimit: "
                           + charBuffer.limit());
    }
}

输出:

CharBuffer before flip: [a, b, c,  ]
Position: 3
Limit: 4
CharBuffer before flip: [a, b, c,  ]
Position: 0
Limit: 3

参考资料: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#flip-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 参考指南