Java中BufferedWriter類的flush()方法用於從緩衝的編寫器流中刷新字符。
用法:
public void flush() throws IOException
指定者:該方法由Flushable接口的flush()方法指定。
覆蓋:該方法覆蓋Writer類的flush()方法。
參數:此方法不接受任何參數。
返回值:此方法不返回任何值。
異常:如果發生I /O錯誤,則此方法將引發IOException。
以下示例程序旨在說明IO包中BufferedWriter類中的flush()方法:
程序1:
// Java program to illustrate
// BufferedWriter flush() method
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
// Create the string Writer
StringWriter stringWriter
= new StringWriter();
// Convert stringWriter to
// bufferedWriter
BufferedWriter buffWriter
= new BufferedWriter(
stringWriter);
// Write "GEEKS" to buffer writer
buffWriter.write(
"GEEKSFORGEEKS", 0, 5);
// Flush the buffer writer
buffWriter.flush();
System.out.println(
stringWriter.getBuffer());
}
}
輸出:
GEEKS
程序2:
// Java program to illustrate
// BufferedWriter flush() method
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
// Create the string Writer
StringWriter stringWriter
= new StringWriter();
// Convert stringWriter to
// bufferedWriter
BufferedWriter buffWriter
= new BufferedWriter(
stringWriter);
// Write "GEEKS" to buffered writer
buffWriter.write(
"GEEKSFORGEEKS", 0, 5);
// Flush the buffered writer
buffWriter.flush();
System.out.println(
stringWriter.getBuffer());
// Write "GEEKSFORGEEKS"
// to buffered writer
buffWriter.write(
"GEEKSFORGEEKS", 5, 8);
// Flush the buffered writer
buffWriter.flush();
System.out.println(
stringWriter.getBuffer());
}
}
輸出:
GEEKS GEEKSFORGEEKS
參考文獻:
https://docs.oracle.com/javase/10/docs/api/java/io/BufferedWriter.html#flush()
相關用法
- Java BufferedWriter newLine()用法及代碼示例
- Java BufferedWriter close()用法及代碼示例
- Java BufferedWriter write()用法及代碼示例
- Java Writer flush()用法及代碼示例
- Java Formatter flush()用法及代碼示例
- Java Console flush()用法及代碼示例
- Java PrintStream flush()用法及代碼示例
- Java PrintWriter flush()用法及代碼示例
- Java StringWriter flush()用法及代碼示例
- Java BufferedOutputStream flush()用法及代碼示例
- Java CharArrayWriter flush()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java lang.Long.builtcount()用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
注:本文由純淨天空篩選整理自pp_pankaj大神的英文原創作品 BufferedWriter flush() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。