flush() just makes sure that any buffered data is written to disk (in this case - more generally, flushed through whatever IO channel you're using). You can still write to the stream (or writer) afterwards.

close() flushes the data and indicates that there isn't any more data. It closes any file handles, sockets or whatever. You then can't write to the stream (or writer) any more.

Note that without calls to flush() data can still be written to the IO channel in question - it's just that some data might be buffered.

close() generally calls flush() as well, but it's recently been pointed out to me that in some JDK implementations, any exceptions thrown by flushing as part of closing are swallowed :(

Answer from Jon Skeet on Stack Overflow
🌐
Coderanch
coderanch.com › t › 442764 › java › fileWriter-flush
Does fileWriter.flush() is always necessary? (I/O and Streams forum at Coderanch)
April 25, 2009 - steve souza wrote:Note close calls flush, so calling both is redundant. I would always call close. I'm not sure what happens in your code if an exception is thrown. It may make sense to call close in a finally clause. http://java.sun.com/j2se/1.4.2/docs/api/java/io/Writer.html#close() Are you sure that close call flush?
Discussions

java.io.FileWriter + flush() - Oracle Forums
I am using a FileWriter within my web app to log application events. After every write I do a flush(). The problem is the writes usually don't show up in the log file until I shutdown Tomcat. If I sw... More on forums.oracle.com
🌐 forums.oracle.com
May 14, 2008
java - When to flush a BufferedWriter - Stack Overflow
In a Java program (Java 1.5), I have a BufferedWriter that wraps a Filewriter, and I call write() many many times... The resulting file is pretty big... Among the lines of this file, some of them are incomplete... Do I need to call flush each time I write something (but I suspect it would be ... More on stackoverflow.com
🌐 stackoverflow.com
java - Is flush() useless for FileWriter since it has no buffer? - Stack Overflow
When you use the flush() method while using a BufferedWriter, that makes sense because you might want to clear the stream immediately so that the person doesn't have to wait until all the writes ar... More on stackoverflow.com
🌐 stackoverflow.com
Help with Java.io.PrintWriter.flush()? Also a bit of a rant.
The meaning of "flush" is pretty consistent through the entire IO framework, and it isn't specific to the PrintWriter class. You can find good documentation in the base Writer class , for example: Flushes the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams. If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive. For example, if you are writing to a file, it is inefficient to write every single byte individually to the file; it is more efficient to write larger chunks to the file. Therefore, the FileWriter implementation might store your written bytes in some kind of buffer (such as a byte array) until it has a sufficiently large chunk to write to the file, so that it can be done efficiently. By invoking the flush() method, you can explicitly "flush" the buffer, which basically means that it take all of the bytes stored in the buffer and write them to the file/destination immediately, and clear/reset the buffer. Typically, you don't need to worry about explicitly calling flush(), as it will be done automatically when you close the writer or outputstream. More on reddit.com
🌐 r/learnjava
6
0
October 19, 2020
🌐
Tutorialspoint
tutorialspoint.com › java › io › writer_flush.htm
Java - Writer flush() method
The following example shows the usage of Writer flush() method. package com.tutorialspoint; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; public class WriterDemo { public static void main(String[] args) { try { Writer writer = new BufferedWriter(new FileWriter("flush1.txt")); writer.write("First part of the content."); writer.flush(); // Forces the buffer to write to file System.out.println("Buffer flushed after first write."); writer.write(" Second part added."); writer.flush(); // Flush again before closing System.out.println("Buffer flushed after second write."); writer.close(); } catch (IOException e) { e.printStackTrace(); } } } Let us compile and run the above program, this will produce the following result− ·
🌐
Tabnine
tabnine.com › home page › code › java › java.io.filewriter
java.io.FileWriter.flush java code examples | Tabnine
FileWriter fw = new FileWriter(temp); yaml.dump(confMap, fw); fw.flush(); fw.close(); origin: commons-io/commons-io · @Test public void testCopy_String_Writer() throws Exception { final File destination = TestUtils.newFile(getTestDirectory(), "copy6.txt"); String str; try (FileReader fin = new FileReader(m_testFile)) { // Create our String. Rely on testReaderToString() to make sure this is valid. str = IOUtils.toString(fin); } try (FileWriter fout = new FileWriter(destination)) { CopyUtils.copy(str, fout); fout.flush(); TestUtils.checkFile(destination, m_testFile); TestUtils.checkWrite(fout); } TestUtils.deleteFile(destination); } origin: scouter-project/scouter ·
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › FileWriter.html
FileWriter (Java Platform SE 8 )
March 16, 2026 - java.io.FileWriter · All Implemented Interfaces: Closeable, Flushable, Appendable, AutoCloseable · public class FileWriter extends OutputStreamWriter · Convenience class for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › io › FileWriter.html
FileWriter (Java Platform SE 7 )
java.io.FileWriter · All Implemented Interfaces: Closeable, Flushable, Appendable, AutoCloseable · public class FileWriter extends OutputStreamWriter · Convenience class for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable.
🌐
Baeldung
baeldung.com › home › java › java io › difference between flush() and close() in java filewriter
Difference Between flush() and close() in Java FileWriter | Baeldung
July 6, 2024 - In other words, invoking flush() ensures that the buffered data is promptly written to disk, allowing continued write or append operations to the file without closing the stream. Conversely, when close() is called, it writes the existing buffered ...
Find elsewhere
🌐
Oracle
forums.oracle.com › ords › apexds › post › java-io-filewriter-flush-1354
java.io.FileWriter + flush() - Oracle Forums
May 14, 2008 - I am using a FileWriter within my web app to log application events. After every write I do a flush(). The problem is the writes usually don't show up in the log file until I shutdown Tomcat. If I sw...
🌐
Jenkov
jenkov.com › tutorials › java-io › filewriter.html
Java FileWriter
The Java FileWriter's flush() method flushes all data written to the FileWriter to the underlying file. The data might be buffered in OS memory somewhere, even if your Java code has written it to the FileWriter.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › BufferedWriter.html
BufferedWriter (Java Platform SE 8 )
March 16, 2026 - Ordinarily this method stores characters from the given array into this stream's buffer, flushing the buffer to the underlying stream as needed. If the requested length is at least as large as the buffer, however, then this method will flush the buffer and write the characters directly to the ...
🌐
Quora
quora.com › What-does-it-mean-to-flush-a-file-in-java
What does it mean to flush a file in java? - Quora
Unless you are writing to that file with an append flag set to True. For example, if you are using FileWriter: FileWriter fw = new FileWriter(filename,true); True in the second parameter will tell the writer to append to the existing file.
🌐
GeeksforGeeks
geeksforgeeks.org › python › file-flush-method-in-python
File flush() method in Python - GeeksforGeeks
July 12, 2025 - The flush() method clears the internal buffer during write operations, ensuring data is immediately saved to a file. It doesn't apply to reading, as reading simply accesses data already stored in the file.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-filewriter-example
Java FileWriter Example | DigitalOcean
August 4, 2022 - When flush() method is called it immediately writes the data to the output file. package com.journaldev.io.filewriter; import java.io.FileWriter; /** * Java write file with FileWriter flush() method * * @author pankaj * */ public class FileWriterFlushExample { public static void main(String[] ...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › io › BufferedWriter.html
BufferedWriter (Java Platform SE 7 )
Ordinarily this method stores characters from the given array into this stream's buffer, flushing the buffer to the underlying stream as needed. If the requested length is at least as large as the buffer, however, then this method will flush the buffer and write the characters directly to the ...
🌐
Reddit
reddit.com › r/learnjava › help with java.io.printwriter.flush()? also a bit of a rant.
r/learnjava on Reddit: Help with Java.io.PrintWriter.flush()? Also a bit of a rant.
October 19, 2020 -

So I am confused with the flush() method of the PrintWriter class. According to all documentation descriptions, it 'flushes' the stream, whatever that means. Luckily I got a better explanation somewhere else, but even then it didn't explain how I'd seen it function. GeeksforGeeks said " it means to clear the stream of any element that may be or maybe not inside the stream". Ok, so I assume that just means it empties the stream, yes?

No, that is not what it does. The given code from the GeeksforGeeks website (found here) is as follows:

// Java program to demonstrate 
// PrintWriter flush() method 
  
import java.io.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // The string to be written in the Writer 
        String str = "GeeksForGeeks"; 
  
        try { 
  
            // Create a PrintWriter instance 
            PrintWriter writer 
                = new PrintWriter(System.out); 
  
            // Write the above string to this writer 
            // This will put the string in the stream 
            // till it is printed on the console 
            writer.write(str); 
  
            // Now clear the stream 
            // using flush() method 
            writer.flush(); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
} 

With output as follows:

GeeksForGeeks

Simple enough right? Well if you remove line 26 of the code above (writer.flush()) then you get no output. So that means that when you flush the input stream, it actually goes somewhere, it doesn't just get erased. So why isn't that put into the documentation and explanation?

I am upset about this whole thing and I'm annoyed that the official documentation doesn't elaborate more on what 'flush' means in the context of the method, class, etc. But aside from me being upset, I would like someone to either explain whats going on here or to link me to some documentation that actually gets it right.

Top answer
1 of 4
4
The meaning of "flush" is pretty consistent through the entire IO framework, and it isn't specific to the PrintWriter class. You can find good documentation in the base Writer class , for example: Flushes the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams. If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive. For example, if you are writing to a file, it is inefficient to write every single byte individually to the file; it is more efficient to write larger chunks to the file. Therefore, the FileWriter implementation might store your written bytes in some kind of buffer (such as a byte array) until it has a sufficiently large chunk to write to the file, so that it can be done efficiently. By invoking the flush() method, you can explicitly "flush" the buffer, which basically means that it take all of the bytes stored in the buffer and write them to the file/destination immediately, and clear/reset the buffer. Typically, you don't need to worry about explicitly calling flush(), as it will be done automatically when you close the writer or outputstream.
2 of 4
1
Other answers have addressed the buffer flushing so I'll comment on the example code. That implementation's leaky; doesn't directly close the writer and certainly doesn't do it in a finally block. It should use "try with resources" like this try (PrintWriter writer = new PrintWriter(System.out) { ... } https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
🌐
TMSVR
tmsvr.com › java-file-writing-i-o-performance
Java File writing I/O performance
February 13, 2026 - Only when the buffer is full, or when a flush() operation is explicitly called, is the buffered data written to the underlying stream or file. This drastically reduces the number of I/O operations, leading to significant performance gains.
🌐
Studytonight
studytonight.com › java-file-io › filewriter-flush-method-in-java
FileWriter flush() Method in Java - Studytonight
March 4, 2021 - This method is used to flush the writer, which means that this method will remove all the data present inside the writer. import java.io.FileWriter; import java.io.IOException; class StudyTonight { public static void main(String[] args) throws ...