使用FileWriter和BufferedWriter 往txt文本中追加或覆盖内容

public static void main(String[] args) throws Exception {
        String filePath = "D:\\测试文件\\20230310-006\\errLog.txt";
        File file = new File(filePath);
        // txt 文本换行符
        String symbol = "\r\n";
        //判断文件是否存在
        if (!file.exists()) {
            // 文件不存在创建文件
            file.createNewFile();
        }
        // 这里true为追加文件内容,false为覆盖文件内容
        FileWriter fileWriter = new FileWriter(file, true);
        BufferedWriter bw = new BufferedWriter(fileWriter);
        bw.write("你个老六" + symbol);
        bw.close();
        fileWriter.close();
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容