文字出力ストリームの書き込み
OutputStreamバイト出力ストリームをデータに使用する場合、バイトタイプのすべてのデータが使用されます。多くの場合、文字列の出力の方が便利です。そのため、文字出力ストリームはJDK1.1で導入されました。
public abstract class Writer extends Object implements Appendable, Closeable, Flushable
主な方法:
- 出力文字列配列:public void write(char [] cbuf)throws IOException
- 出力文字列:public void write(String str)throws IOException
例:書き込みを使用して出力する
package 字节流与字符流;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Writer字符流输出 {
public static void main(String[] args) throws IOException {
File file = new File("F:"+File.separator+"Test"+File.separator+"test.txt");
if(file.getParentFile().exists()){ //没有父路径
file.getParentFile().mkdirs(); //创建一个父路径
}
Writer writer = new FileWriter(file,true); //实例化File,要求每次执行内容被覆盖可以将true去掉
writer.write("加入的内容");
writer.append("我是追加的");
writer.close();
}
}
利点:文字列を使用して直接実行できます。Writerは、中国語のデータを便利に処理できる文字ストリームです。