package com.tjrac;

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

/**
 * 数据以字符的方式写入文件
 */
public class FileWriterDemo {
    public static void main(String[] args) throws IOException {
        //实例化file对象
        //true
        File file = new File("mydata.txt");
        FileWriter fw = null;
        try {
            //实例化FileWriter对象
            //追加模式true,false覆盖
            fw = new FileWriter(file, false);
            //把字符串写入文件中
            String str = "hello java";
            //刷新
            fw.flush();
            fw.write(str);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                fw.close();//关闭流
            }
        }
    }
}