diff --git a/javaSE-day09/src/com/inmind/writer04/Demo12.java b/javaSE-day09/src/com/inmind/writer04/Demo12.java new file mode 100644 index 0000000..f0c350e --- /dev/null +++ b/javaSE-day09/src/com/inmind/writer04/Demo12.java @@ -0,0 +1,45 @@ +package com.inmind.writer04; + +import java.io.FileWriter; +import java.io.IOException; + +/* + 字符输出流写数据 + 在java中使用抽象类Writer表示字符输出流 + 常用子类 FileWriter + + 构造方法: + FileWriter(File file) 给一个File对象构造一个FileWriter对象。 + FileWriter(String fileName) 构造一个给定文件名的FileWriter对象。 + + FileWriter(File file, boolean append) 给一个File对象构造一个FileWriter对象。 + FileWriter(String fileName, boolean append) 构造一个FileWriter对象,给出一个带有布尔值的文件名,表示是否附加写入的数据。 + + 常用方法: + abstract void close() 关闭资源 + void write(char[] cbuf) 写入一个字符数组。 + abstract void write(char[] cbuf, int off, int len) 写入字符数组的一部分。 + void write(int c) 写一个字符 + void write(String str) 写一个字符串 + void write(String str, int off, int len) 写一个字符串的一部分。 + + 使用步骤: + 1.创建对象 + 2.调用写方法 + 3.刷新 + 4.释放资源 + */ +public class Demo12 { + public static void main(String[] args) throws IOException { + //向a.txt,覆盖输出内容 + FileWriter fw = new FileWriter("a.txt",true); + + fw.write('我'); + char[] chars = {'在','哪'}; + fw.write(chars); + + fw.write("我想吃饭"); + + fw.close(); + } +}