Java中CharArrayWriter類的close()方法用於關閉當前流。
用法:
public void close()
參數:此方法不接受任何參數。
返回值:此方法關閉當前流。但是,它不會釋放緩衝區。
以下示例程序旨在說明上述方法:
示例1:
// Java program to illustrate
// the close() 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());
// Close the current stream
char_array1.close();
}
}
輸出:
Size of char_array1 : 5
示例2:
// Java program to illustrate
// the close() 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());
// Close the current stream
char_array1.close();
}
}
輸出:
Size of char_array1 : 6
參考: https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#close()
相關用法
- Java CharArrayWriter flush()用法及代碼示例
- Java CharArrayWriter size()用法及代碼示例
- Java CharArrayWriter toString()用法及代碼示例
- Java CharArrayWriter toCharArray()用法及代碼示例
- Java CharArrayWriter reset()用法及代碼示例
- Java Formatter close()用法及代碼示例
- Java PrintStream close()用法及代碼示例
- Java Scanner close()用法及代碼示例
- Java Reader close()用法及代碼示例
- Java StringReader close()用法及代碼示例
- Java ByteArrayInputStream close()用法及代碼示例
- Java CharArrayReader close()用法及代碼示例
- Java StringWriter close()用法及代碼示例
- Java Writer close()用法及代碼示例
- Java PrintWriter close()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 CharArrayWriter close() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。