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


Java CharArrayWriter reset()用法及代码示例


Java中CharArrayWriter类的reset()方法用于重置缓冲区,以便可以再次使用它而不会丢弃已分配的缓冲区。

用法

public void reset()

参数:此方法不接受任何参数。


返回值:此方法不返回任何内容。

以下示例程序旨在说明上述方法:

示例1:

// Java program to illustrate 
// the reset() method 
  
import java.io.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws IOException 
    { 
  
        // Initailizing the character array 
        char[] geek = { 'G', 'E', 'E', 'K', 'S' }; 
  
        // Initailizing 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()); 
    } 
}
输出:
Size of char_array1 : 5
Size of char_array1 : 0

示例2:

// Java program to illustrate 
// the reset() method 
  
import java.io.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws IOException 
    { 
  
        // Initailizing the character array 
        char[] geek 
            = { 'G', 'O', 'P', 'A', 'L', 'L' }; 
  
        // Initailizing 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://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#reset()



相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 CharArrayWriter reset() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。