You're not closing the writer, so all the data is probably just staying in the internal buffer.

Ideally, use a try-with-resources statement, or close it in a finally block.

int idbuffersize1;
char[] a = new char[1000];
FileWriter idcopy = new FileWriter(idFile);
try {  
    while ((idbuffersize1 = br.read(a)) > 0) {
        idcopy.write(a, 0, idbuffersize1);
    }
} finally {
    idcopy.close();
}

As an aside, I personally don't like FileWriter, as it always uses the system default encoding, rather than allowing you to specify an encoding. I prefer using a FileOutputStream wrapped in an OutputStreamWriter.

Answer from Jon Skeet on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_files_write.asp
Java Write To Files
This puts the writer into append mode: import java.io.FileWriter; import java.io.IOException; public class AppendToFile { public static void main(String[] args) { // true = append mode try (FileWriter myWriter = new FileWriter("filename.txt", true)) ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › filewriter-class-in-java
Java FileWriter Class - GeeksforGeeks
November 4, 2025 - Example 1: Writing Characters to a File using FileWriter class ... import java.io.FileWriter; import java.io.IOException; import java.util.*; class WriteFile { public static void main(String[] args) { Scanner scn = new Scanner(System.in); String ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › FileWriter.html
FileWriter (Java Platform SE 8 )
April 21, 2026 - Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.
🌐
Baeldung
baeldung.com › home › java › java io › java filewriter
Java FileWriter | Baeldung
February 8, 2025 - FileWriter is a specialized OutputStreamWriter for writing character files. It doesn’t expose any new operations but works with the operations inherited from the OutputStreamWriter and Writer classes. Until Java 11, the FileWriter worked with the default character encoding and default byte buffer size.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-filewriter-example
Java FileWriter Example | DigitalOcean
August 4, 2022 - FileWriter can handle Unicode strings while FileOutputStream writes bytes to a file and do not accepts characters or strings hence it needs to wrapped up by OutputStreamWriter to accept strings. Also check java write file for more about how to write file in java.
🌐
Android Developers
developer.android.com › api reference › filewriter
FileWriter | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Medium
medium.com › javarevisited › filereader-and-filewriter-in-java-simplified-file-handling-fc58014d4f6d
FileReader and FileWriter in Java: Simplified File Handling | by WhatInDev | Javarevisited | Medium
December 25, 2024 - This makes it a natural choice for text-based file output. write(int c): Writes a single character to the file. ... close(): Closes the stream, ensuring that all data is flushed to the file and resources are released.
Find elsewhere
🌐
Coderanch
coderanch.com › t › 279182 › java › Starting-line-FileWriter-class
Starting a new line with FileWriter class (I/O and Streams forum at Coderanch)
August 25, 2008 - programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... I created a simple test for FileWriter class, here's the code: The problem is, it writes all the text in one line.
🌐
Medium
medium.com › geekculture › using-printwriter-vs-filewriter-in-java-2958df85f105
Using PrintWriter vs FileWriter in Java | by Anna Scott | Geek Culture | Medium
August 19, 2021 - Using PrintWriter vs FileWriter in Java PrintWriter and FileWriter are JAVA classes that allow writing data into files. public static void main(String[] args) { //creating object of class File …
Top answer
1 of 2
3

You should put writer.close() after the while loop, and preferable, into the finally section.

If there is no requirement to store partially-processed files (as in most cases), you may remove flush at all. In the other case, it is better to leave it where it is.

The generic case of resource usage on Java 7+ looks like follows (this syntax is called try-with-resources:

try (
    Resource resource1 = // Resource 1 initialization
    Resource resource2 = // Resource 2 initialization
    ...
) {
    // Resource utilization
} catch (XXXException e) {
    // Something went wrong
}

Resource are freed (closed) automatically by try-with-resources.

If you need to use Java 6 or earlier, the above code could be roughly translated to the following (actually there are some subtle differences, that is not important at this level of details).

try {
    Resource1 resource1 = // Resource initialization
    try {
        Resource2 resource2 = // Resource initialization
        try {
            // Resource utilization
        } finally {
            // Free resource2
            resource2.close();
        }
    } finally {
        // Free resource1
        resource1.close();
    }
} catch (XXXException e) {
    // Something went wrong
}

Notice, how nested try-finally blocks used for resource management.

In your particular case we need to manage two resources: Reader and Writer, so the code will look as follows:

try (
        // Notice, we build BufferedReader for the file in a single expression
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                new FileInputStream("sample.txt"),
                StandardCharsets.UTF_8 // Better replacement for Charset.forName("UTF-8")
        ));
        // Alternative way to do the same
        // BufferedReader reader = Files.newBufferedReader(Paths.get("sample.txt"), StandardCharsets.UTF_8);

        // Output charset for writer provided explicitly
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream("set.txt"),
                StandardCharsets.UTF_8
        ))
        // Alternative way to do the same
        // BufferedWriter writer = Files.newBufferedWriter(Paths.get("set.txt"), StandardCharsets.UTF_8)
) {
    String input;
    while ((input = reader.readLine()) != null) {
        String[] s = input.split(":");
        if (s[1].equals(text)) {
            writer.write(s[0] + "'s result is " + text);
            writer.newLine();
            break;
        }
    }
} catch (IOException e) {
    // Error handling
}

Or, using pre-Java7 syntax:

try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            new FileInputStream("sample.txt"),
            Charset.forName("UTF-8")
    ));
    try {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream("set.txt"),
                Charset.forName("UTF-8")
        ));
        try {
            String input;
            while ((input = reader.readLine()) != null) {
                String[] s = input.split(":");
                if (s[1].equals(text)) {
                    writer.write(s[0] + "'s result is " + text);
                    writer.newLine();
                    break;
                }
            }
        } finally {
            writer.close();
        }
    } finally {
        reader.close();
    }
} catch (IOException e) {
    // Error handling
}
2 of 2
2

First of all, you call the flush method of a writer, whenever you want the current buffer to be written immediately. If you just write a file completely without any intermediate operation on your output, you do not need to call it explicitly, since the close call will do that for you.

Secondly, you only call the close method of the top-level reader or writer, in your case BufferedWriter. The close call is forwarded to the other assigned readers or writers. Multiple consecutive close calls do not have any effect on a previously closed instance, see here.

As a general note to using readers and writers, consider this pattern:

// This writer must be declared before 'try' to
// be visible in the finally block
AnyWriter writer = null;

try {

    // Instantiate writer here, because it can already
    // throw an IOException
    writer = new AnyWriter();

    // The the writing in a loop or as you wish
    // If you need to write out the buffer in 
    // between, call flush

} catch (IOException e) {

    // Something went wrong while writing

} finally {

    try {
        if (writer != null)
            writer.close();
    } catch (IOException e) {
        // Exception while trying to close
    }

}

The finally block is ALWAYS executed. If you need a more compact syntax and you use at least Java 7, you can have a look at the try-with notation here.

🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › FileWriter.html
FileWriter (Java SE 11 & JDK 11 )
January 20, 2026 - Constructs a FileWriter given a file name and a boolean indicating whether to append the data written, using the platform's default charset.
🌐
Baeldung
baeldung.com › home › java › java io › guide to filewriter vs. bufferedwriter
Guide to FileWriter vs. BufferedWriter | Baeldung
June 30, 2024 - Specifically, FileWriter extends OutputStreamWriter, as we’ve just seen in the UML diagram, and OutputStreamWriter uses StreamEncoder, whose code contains DEFAULT_BYTE_BUFFER_SIZE = 8192 up to JDK18 and MAX_BYTE_BUFFER_CAPACITY = 8192 in later versions. StreamEncoder isn’t a public class in the JDK API. It’s an internal class in the sun.nio.cs package that is used within the Java framework to handle encoding of character streams.
🌐
Scientech Easy
scientecheasy.com › home › blog › filewriter in java (with example)
FileWriter in Java (with Example) - Scientech Easy
February 10, 2025 - In this program, we have created a FileWriter object, specifying the name of file in the form of either String or File reference. The write() method inherited from Writer class has been used to write lines of text.
🌐
Quora
quora.com › How-do-you-use-a-file-writer-in-Java
How to use a file writer in Java - Quora
Answer: In the future if you have more questions like this and require only a technical example, I would suggest downloading the Codota plugin. You can browse thousands of code snippets from the comfort of your IDE, and easily find examples on how to use various libraries. And with a second click...
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-write-to-file
Java Write to File - 4 Ways to Write File in Java | DigitalOcean
August 3, 2022 - FileOutputStream: FileWriter and BufferedWriter are meant to write text to the file but when you need raw stream data to be written into file, you should use FileOutputStream to write file in java. Files: Java 7 introduced Files utility class and we can write a file using its write function. Internally it’s using OutputStream to write byte array into file. Here is the example ...
🌐
Team Treehouse
teamtreehouse.com › community › java-file-copy-problem-using-filewriter-class
Java file copy problem using FileWriter class (Example) | Treehouse Community
September 18, 2018 - try{ FileReader fr = new FileReader("./poem.txt"); FileWriter fw = new FileWriter("./poem_new.txt"); int x = fr.read(); while(x != -1){ //System.out.printf("value is %s %n",x); fw.write(x); x = fr.read(); } }