From 8f84f047006fd59bd55bbb2636ac993d5e5e19de Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Sat, 21 Mar 2026 14:44:39 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E9=98=B6day09-=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E6=B5=81=E5=86=99=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/inmind/writer04/Demo12.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 javaSE-day09/src/com/inmind/writer04/Demo12.java 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(); + } +}