📜  Java中的 CharArrayWriter reset() 方法及示例

📅  最后修改于: 2022-05-13 01:55:29.695000             🧑  作者: Mango

Java中的 CharArrayWriter reset() 方法及示例

Java中CharArrayWriter类的reset()方法用于重置缓冲区,以便在不丢弃已经分配的缓冲区的情况下再次使用它。
语法

public void reset()

参数:此方法不接受任何参数。
返回值:此方法不返回任何内容。
下面的程序说明了上述方法:
方案一:

Java
// Java program to illustrate
// the reset() method
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
 
        // Initializing the character array
        char[] geek = { 'G', 'E', 'E', 'K', 'S' };
 
        // Initializing the CharArrayWriter
        CharArrayWriter char_array1
            = new CharArrayWriter();
 
        for (int c = 72; c < 77; c++) {
            // Use of write(int char)
            // Writer int value to the Writer
            char_array1.write(c);
        }
 
        // Use of size() method
        System.out.println("\nSize of char_array1 : "
                           + char_array1.size());
 
        // Resets the current stream
        char_array1.reset();
 
        // Use of size() method
        System.out.println("Size of char_array1 : "
                           + char_array1.size());
    }
}


Java
// Java program to illustrate
// the reset() method
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
 
        // Initializing the character array
        char[] geek
            = { 'G', 'O', 'P', 'A', 'L', 'L' };
 
        // Initializing the CharArrayWriter
        CharArrayWriter char_array1
            = new CharArrayWriter();
 
        for (int c = 72; c < 78; c++) {
            // Use of write(int char)
            // Writer int value to the Writer
            char_array1.write(c);
        }
 
        // Use of size() method
        System.out.println("\nSize of char_array1 : "
                           + char_array1.size());
 
        // Resets the current stream
        char_array1.reset();
 
        // Use of size() method
        System.out.println("Size of char_array1 : "
                           + char_array1.size());
    }
}


输出:
Size of char_array1 : 5
Size of char_array1 : 0

方案二:

Java

// Java program to illustrate
// the reset() method
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
 
        // Initializing the character array
        char[] geek
            = { 'G', 'O', 'P', 'A', 'L', 'L' };
 
        // Initializing the CharArrayWriter
        CharArrayWriter char_array1
            = new CharArrayWriter();
 
        for (int c = 72; c < 78; c++) {
            // Use of write(int char)
            // Writer int value to the Writer
            char_array1.write(c);
        }
 
        // Use of size() method
        System.out.println("\nSize of char_array1 : "
                           + char_array1.size());
 
        // Resets the current stream
        char_array1.reset();
 
        // Use of size() method
        System.out.println("Size of char_array1 : "
                           + char_array1.size());
    }
}
输出:
Size of char_array1 : 6
Size of char_array1 : 0

参考:https: Java/io/CharArrayWriter.html#reset()