📜  Java I/O-writer类

📅  最后修改于: 2020-09-27 04:43:38             🧑  作者: Mango

Java writer

它是用于写入字符流的抽象类。子类必须实现的方法是write(char [],int,int),flush()和close()。大多数子类将覆盖此处定义的某些方法,以提供更高的效率和/或功能。

领域

Modifier and Type Field Description
protected Object lock The object used to synchronize operations on this stream.

建设者

Modifier Constructor Description
protected Writer() It creates a new character-stream writer whose critical sections will synchronize on the writer itself.
protected Writer(Object lock) It creates a new character-stream writer whose critical sections will synchronize on the given object.

方法

Modifier and Type Method Description
Writer append(char c) It appends the specified character to this writer.
Writer append(CharSequence csq) It appends the specified character sequence to this writer
Writer append(CharSequence csq, int start, int end) It appends a subsequence of the specified character sequence to this writer.
abstract void close() It closes the stream, flushing it first.
abstract void flush() It flushes the stream.
void write(char[] cbuf) It writes an array of characters.
abstract void write(char[] cbuf, int off, int len) It writes a portion of an array of characters.
void write(int c) It writes a single character.
void write(String str) It writes a string.
void write(String str, int off, int len) It writes a portion of a string.

Java Writer示例

import java.io.*;
public class WriterExample {
public static void main(String[] args) {
try {
Writer w = new FileWriter("output.txt");
String content = "I love my country";
w.write(content);
w.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}

输出:

Done

output.txt:

I love my country