fileWriter-2023-3-3

package JavaSE.Lesson18;

import java.io.FileWriter;
import java.io.IOException;

public class FileWriterDemo01 {
    public static void main(String[] args) {
        FileWriter fw = null;
        String filePath = "e:\\Demo04.txt";
        char[] chars = {'a','b','c'};

        try {
            fw = new FileWriter(filePath,true);
            //1.写入一个
            fw.write('H');
            //2.通过字符数组写入
            fw.write(chars);
            //3.写入字符数组指定部分
            fw.write("飞剑学习JAVA".toCharArray(),0,5);
            //4.写入字符串
            fw.write("飞剑一定要努力");
            //5.指定字符串的某个部分
            fw.write("上海天津",0,2);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("程序结束....");


    }
}
posted @ 2023-03-03 21:53  Rui2022  阅读(12)  评论(0编辑  收藏  举报