java.io.Writer 類是一個抽象類。它用於寫入字符流。
Java 中 Writer 類的聲明
public abstract class Writer extends Object implements Appendable, Closeable, Flushable
Java Writer 類的構造函數
- 受保護Writer():創建一個新的字符流,它本身可以在編寫器上同步。
- 受保護的作家(對象 obj):創建一個新的字符流,它本身可以在給定對象上同步 - ‘obj’。
Java Writer 類的方法
有一些與 Java Writer 類相關的方法,如下所述:
Method |
Description |
---|---|
append(char SW) | 向 writer 追加一個字符 |
追加(CharSequence char_sq) | 將指定的字符序列附加到 writer |
追加(CharSequence char_sq,int開始,int結束) | 將字符序列的指定部分附加到 writer |
flush() |
刷新寫入器流。 刷新一個流調用將刷新鏈中的所有其他緩衝區。 |
write(int char) | 將單個字符寫入字符流。 |
write(string str) | 將字符串寫入字符流。 |
write(string str, int offset , int maxlen) | 將字符串的某些部分寫入字符串。 |
寫(char[] 載體) | 將字符數組寫入字符流。 |
write(char[] carray, int 偏移量, int maxlen) | 將字符數組的某些部分寫入字符流。 |
close() | 關閉字符串緩衝區。因為,關閉字符流,首先刷新它。 |
1. 寫(int char)
java.io.Writer.write(int char) 將單個字符寫入字符流。正在寫入的字符包含在 ‘char’ 整數值的 16 個低位中,其餘 16 個高位將被該方法忽略。
Syntax: public void write(int char) Parameters : char : int value of the character to be written. Return : void Exception : -> IOException : if in case I/O error occurs.
2. 寫入(字符串str):
java.io.Writer.write(String str) 將字符串寫入字符流。
Syntax: public void write(String str) Parameters : str : string to be written to the character stream. Return : void Exception : -> IOException : if in case I/O error occurs.
3. 寫入(字符串str,int偏移量,int maxlen)
java.io.Writer.write(String str, int offset, int maxlen) 將字符串的某些部分寫入字符流。
Syntax: public void write(String str, int offset, int maxlen) Parameters : str : string to be written to the character stream. offset : start position of the String maxlen : maximum length upto which string has to written Return : void Exception : -> IOException : if in case I/O error occurs. -> IndexOutOfBoundsException : if offset is -ve or offset + maxlen = -ve || > maxlen
4. write(char[] 數組)
java.io.Writer.write(char[] carray) 將字符數組寫入字符流。
Syntax: public void write(char[] carray) Parameters : carray : character array to be written to the character stream Return : void Exception : -> IOException : if in case I/O error occurs.
5. write(char[] carray, int 偏移量, int maxlen)
java.io.Writer.write(char[] carray, int offset, int maxlen) 將字符數組的某些部分寫入字符流。
Syntax: public abstract void write(char[] carray, int offset, int maxlen) Parameters : carray : character to be written to the character stream offset : start position of the character array maxlen : maximum no. of the character of the carray has to written Return : void Exception : -> IOException : if in case I/O error occurs.
6.close()
java.io.Writer.close() 關閉字符流,首先刷新它。
Syntax: public abstract void close() Parameters : ----------- Return : void Exception : -> IOException : if in case I/O error occurs.
7.flush()
java.io.Writer.flush() 刷新 Writer 流。刷新一個流調用將刷新鏈中的所有其他緩衝區。
Syntax: public void flush() Parameters : ----- Return : void Exception : -> IOException : if in case I/O error occurs.
8. 追加(char Sw)
java.io.Writer.append(char Sw) 將單個字符追加到 Writer 中。
Syntax: public Writer append(char Sw) Parameters : Sw : character to be append Return : Writer Exception : -> IOException : if in case I/O error occurs.
9. 追加(CharSequence char_sq)
java.io.Writer.append(CharSequence char_sq) 將指定的字符序列附加到 Writer 中。
Syntax: public Writer append(CharSequence char_sq) Parameters : char_sq : Character sequence to append. Return : Writer, if char sequence is null, then NULL appends to the Writer. Exception : -> IOException : if in case I/O error occurs.
10.追加(CharSequence char_sq,int開始,int結束)
java.io.Writer.append(CharSequence char_sq, int 開始, int 結束)將字符序列的指定部分附加到 Writer。
Syntax: public Writer append(CharSequence char_sq, int start, int end) Parameters : char_sq : Character sequence to append. start : start of character in the Char Sequence end : end of character in the Char Sequence Return : void Exception : -> IOException : if in case I/O error occurs. -> IndexOutOfBoundsException : if start or end are -ve or start > end or end > char_sq.length
Java Writer 類示例
示例 1:
Java
// Java program illustrating the working of Writer class
// methods write(int char), write(String str), close()
// write(String str, int offset, int maxlen), flush()
// write(char[] carray, int offset, int maxlen),
// write(char[] carray)
import java.io.*;
public class NewClass {
public static void main(String[] args)
throws IOException
{
char[] carray = { 'G', 'E', 'E', 'K', 'S' };
// Initializing Writer
Writer geek_writer1 = new PrintWriter(System.out);
Writer geek_writer2 = new PrintWriter(System.out);
Writer geek_writer3 = new PrintWriter(System.out);
Writer geek_writer4 = new PrintWriter(System.out);
Writer geek_writer5 = new PrintWriter(System.out);
// Use of write(int char) : to write a character
geek_writer1.write(71);
geek_writer1.write(70);
geek_writer1.write(71);
// Use of flush() method
System.out.print("Using write(int char[]) : ");
geek_writer1.flush();
String str = "Hello Geeks";
// Use of write(String str) : to write string
geek_writer2.write(str);
// Value written by write(String str)
System.out.print("\nUsing write(String str) : ");
geek_writer2.flush();
// Use of write(String str, int offset, int maxlen)
//: to write part of string
geek_writer3.write(str, 2, 4);
geek_writer3.write(str, 5, 6);
// Value written by write(String str, int offset,
// int maxlen)
System.out.print(
"\nUsing write(str, offset, maxlen) : ");
geek_writer3.flush();
geek_writer4.write(carray);
System.out.print("\nUsing write(char[] carray) : ");
geek_writer4.flush();
// Use of write(char[] carray, int offset, int
// maxlen): to write part of char array
geek_writer5.write(carray, 1, 3);
// Value written by write(String str, int offset,
// int maxlen)
System.out.print(
"\nUsing write(carray, offset, maxlen) : ");
geek_writer5.flush();
// Use of close() method
geek_writer1.close();
geek_writer2.close();
geek_writer3.close();
geek_writer4.close();
geek_writer5.close();
}
}
Using write(int char[]) : GFG Using write(String str) : Hello Geeks Using write(str, offset, maxlen) : llo Geeks Using write(char[] carray) : GEEKS Using write(carray, offset, maxlen) : EEK
示例 2:
Java
// Java program illustrating the working of Writer class
// methods append(CharSequence char_sq), append(char Sw)
// append(CharSequence char_sq, int start,int end)
// flush()
import java.io.*;
public class NewClass {
public static void main(String[] args)
throws IOException
{
// Initializing String Writer
Writer geek_writer1 = new PrintWriter(System.out);
Writer geek_writer2 = new PrintWriter(System.out);
Writer geek_writer3 = new PrintWriter(System.out);
// Use of write(int char) : to write a character
geek_writer1.append('G');
geek_writer1.append('G');
geek_writer1.append('G');
geek_writer1.append('G');
geek_writer1.append('G');
// Use of append(char Sw)
System.out.print("append(char Sw) : ");
geek_writer1.flush();
// Initializing Character Sequence
CharSequence char_sq1 = "1 Hello 1";
CharSequence char_sq2 = " : 2 Geeks 2";
// Use of append(CharSequence char_sq)
geek_writer2.append(char_sq1);
geek_writer2.append(char_sq2);
System.out.print("\nappend(char_sq) : ");
geek_writer2.flush();
// Use of append(CharSequence char_sq,int start,int
// end)
geek_writer3.append(char_sq1, 0, 3);
geek_writer3.append(char_sq2, 3, 6);
System.out.print("\nappend(char_sq,start,end) : ");
geek_writer3.flush();
}
}
append(char Sw) : GGGGG append(char_sq) : 1 Hello 1 : 2 Geeks 2 append(char_sq,start,end) : 1 H2 G
相關用法
- Java Java.io.Writer.append()用法及代碼示例
- Java Java.io.Writer.close()用法及代碼示例
- Java Java.io.Writer.flush()用法及代碼示例
- Java Java.io.Writer.write()用法及代碼示例
- Java Java.io.Writer用法及代碼示例
- Java Java.io.BufferedInputStream.available()用法及代碼示例
- Java Java.io.BufferedInputStream.close()用法及代碼示例
- Java Java.io.BufferedInputStream.read()用法及代碼示例
- Java Java.io.BufferedInputStream.reset()用法及代碼示例
- Java Java.io.BufferedInputStream.skip()用法及代碼示例
- Java Java.io.BufferedOutputStream.flush()用法及代碼示例
- Java Java.io.BufferedOutputStream.Write()用法及代碼示例
- Java Java.io.BufferedReader.Close()用法及代碼示例
- Java Java.io.BufferedReader.mark()用法及代碼示例
- Java Java.io.BufferedReader.markSupported()用法及代碼示例
- Java Java.io.BufferedReader.read()用法及代碼示例
- Java Java.io.BufferedReader.readline()用法及代碼示例
- Java Java.io.BufferedReader.ready()用法及代碼示例
- Java Java.io.BufferedReader.reset()用法及代碼示例
- Java Java.io.BufferedReader.skip()用法及代碼示例
- Java Java.io.BufferedWriter.close()用法及代碼示例
- Java Java.io.BufferedWriter.flush()用法及代碼示例
- Java Java.io.BufferedWriter.newLine()用法及代碼示例
- Java Java.io.BufferedWriter.write()用法及代碼示例
- Java Java.io.ByteArrayInputStream.available()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.io.Writer Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。