FileWriter未写入文件 [英] FileWriter is not writing in to a file

查看:392
本文介绍了FileWriter未写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

    try
    {
        FileWriter fileWriter = new FileWriter("C:\\temp\\test.txt");
        fileWriter.write("Hi this is sasi This test writing");
        fileWriter.append("test");
    }
    catch(IOException ioException)
    {
        ioException.printStackTrace();
    }

执行后,文件创建成功,但创建的文件为空

after executing it file is created successfully but the created file is empty

那么代码有什么问题?

推荐答案

您必须关闭FileWriter,否则不会刷新当前缓冲区.您可以直接调用flush方法.

You must close the FileWriter, otherwise it won't flush the current buffer. You can call the flush method directly..

fileWriter.flush()
fileWriter.close()

如果要关闭文件,则无需使用flush方法.例如,如果您的程序运行了一段时间并在文件中输出了某些内容,而您想在其他地方进行检查,则可以使用flush.

You don't need to use the flush method if you are closing the file. The flush can be used for example if your program runs for a while and outputs something in a file and you want to check it elsewhere.

这篇关于FileWriter未写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆