java中如何将输出结果放入文件中

在某位置建立*.txt文本,复制代码,测试一下输出“123”

public class Test {
    public static void rwFile(){
        FileWriter fw = null;
        try {
            fw = new FileWriter("C:\\text.txt", true);//路径一定要用"\\"
                fw.write("123");//这里向文件中输入结果123
                fw.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) {
        rwFile();
    }
}
    原文作者:麋路l
    原文地址: https://blog.csdn.net/weixin_43967295/article/details/92796578
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞