FileWriter is not writing in to a file

后端 未结 5 1005
谎友^
谎友^ 2021-01-15 09:04

I have below code

    try
    {
        FileWriter fileWriter = new FileWriter(\"C:\\\\temp\\\\test.txt\");
        fileWriter.write(\"Hi this is sasi This t         


        
5条回答
  •  一向
    一向 (楼主)
    2021-01-15 09:16

    Closing was missing. Hence not the last buffered data was not written to disk.

    Closing also happens with try-with-resources. Even when an exception would be thrown. Also a flush before close is not needed, as a close flushes all buffered data to file.

    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();
    }
    

提交回复
热议问题