6

I have below code

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, the file is created successfully, but the created file is empty.

What is wrong with the code?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74

5 Answers5

20

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

fileWriter.flush()
fileWriter.close()

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.

Jindra Helcl
  • 3,457
  • 1
  • 20
  • 26
4

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();
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
3

You need to close the filewriter else the current buffer will not flush and will not allow you to write to the file.

fileWriter.flush(); //just makes sure that any buffered data is written to disk
fileWriter.close(); //flushes the data and indicates that there isn't any more data.

From the Javadoc

Close the stream, flushing it first. Once a stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

Try this:

import java.io.*;

public class Hey

{
    public static void main(String ar[])throws IOException
    {

            File file = new File("c://temp//Hello1.txt");
            // creates the file
            file.createNewFile();
            // creates a FileWriter Object
            FileWriter writer = new FileWriter(file); 
            // Writes the content to the file
            writer.write("This\n is\n an\n example\n"); 
            writer.flush();
            writer.close();
    }
}
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
1

Please try this :

  try
    {
        FileWriter fileWriter = new FileWriter("C:\\temp\\test.txt");
        fileWriter.write("Hi this is sasi This test writing");
        fileWriter.append("test");
        fileWriter.flush(); // empty buffer in the file
        fileWriter.close(); // close the file to allow opening by others applications
    }
    catch(IOException ioException)
    {
        ioException.printStackTrace();
    }
romfret
  • 391
  • 2
  • 11