Yeah. Use the constructor like this:

FileWriter writer = new FileWriter("myfile.csv",true);
Answer from Geo on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › FileWriter.html
FileWriter (Java Platform SE 8 )
April 21, 2026 - java.io.Writer · java.io.OutputStreamWriter · 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 ...
🌐
W3Schools
w3schools.com › java › java_files_write.asp
Java Write To Files
If you are just starting with Java, the easiest way to write text to a file is by using the FileWriter class. In the example below, we use FileWriter together with its write() method to create and write some text into a file.
🌐
GeeksforGeeks
geeksforgeeks.org › java › filewriter-class-in-java
Java FileWriter Class - GeeksforGeeks
November 4, 2025 - The FileWriter class in Java is used to write character data to files.
🌐
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 ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.io.filewriter
FileWriter Class (Java.IO) | Microsoft Learn
Writes text to character files using a default buffer size. [Android.Runtime.Register("java/io/FileWriter", DoNotGenerateAcw=true)] public class FileWriter : Java.IO.OutputStreamWriter
🌐
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 · 中文 – 简体
Find elsewhere
🌐
Java
download.java.net › java › early_access › panama › docs › api › java.base › java › io › FileWriter.html
FileWriter (Java SE 19 & JDK 19 [build 1])
Constructs a FileWriter given the File to write, charset and a boolean indicating whether to append the data written. ... append - a boolean. If true, the writer will write the data to the end of the file rather than the beginning.
🌐
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 - I can't remember exactly what each one means, but if you add + "\r\n" I think that should solve your problem. I know that on Windows systems you need both characters in the text files, not just \n, which does work fine if you're just using the command prompt, but not for file writing.
Top answer
1 of 16
1877

Note that each of the code samples below may throw IOException. Try/catch/finally blocks have been omitted for brevity. See this tutorial for information about exception handling.

Note that each of the code samples below will overwrite the file if it already exists

Creating a text file:

PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
writer.println("The first line");
writer.println("The second line");
writer.close();

Creating a binary file:

byte data[] = ...
FileOutputStream out = new FileOutputStream("the-file-name");
out.write(data);
out.close();

Java 7+ users can use the Files class to write to files:

Creating a text file:

List<String> lines = Arrays.asList("The first line", "The second line");
Path file = Paths.get("the-file-name.txt");
Files.write(file, lines, StandardCharsets.UTF_8);
//Files.write(file, lines, StandardCharsets.UTF_8, StandardOpenOption.APPEND);

Creating a binary file:

byte data[] = ...
Path file = Paths.get("the-file-name");
Files.write(file, data);
//Files.write(file, data, StandardOpenOption.APPEND);
2 of 16
452

In Java 7 and up:

try (Writer writer = new BufferedWriter(new OutputStreamWriter(
              new FileOutputStream("filename.txt"), "utf-8"))) {
   writer.write("something");
}

There are useful utilities for that though:

  • FileUtils.writeStringtoFile(..) from commons-io
  • Files.write(..) from guava

Note also that you can use a FileWriter, but it uses the default encoding, which is often a bad idea - it's best to specify the encoding explicitly.

Below is the original, prior-to-Java 7 answer


Writer writer = null;

try {
    writer = new BufferedWriter(new OutputStreamWriter(
          new FileOutputStream("filename.txt"), "utf-8"));
    writer.write("Something");
} catch (IOException ex) {
    // Report
} finally {
   try {writer.close();} catch (Exception ex) {/*ignore*/}
}

See also: Reading, Writing, and Creating Files (includes NIO2).

🌐
W3Schools
w3schools.com › JAVA › java_bufferedwriter.asp
Java BufferedWriter
Java Examples Java Videos Java ... Syllabus Java Study Plan Java Interview Q&A ... The BufferedWriter class is used to write text to a file, one line or one string at a time....
🌐
o7planning
o7planning.org › 13383 › java-filewriter
Java FileWriter Tutorial with Examples | o7planning.org
ECMAScript / Javascript · TypeScript ... dụng tiện ích · VirtualBox · VmWare · FileWriter · Examples · FileWriter is a subclass of OutputStreamWriter, used to write text files....
🌐
Litux
litux.nl › Books › Books › www.leothreads.com › e-book › oreillybookself › java › javanut › ch24_22.htm
[Chapter 24] 24.22 java.io.FileWriter (JDK 1.1)
FileWriter is a convenience subclass of OutputStreamWriter that is useful when you want to write text (as opposed to binary data) to a file. You create a FileWriter by specifying the file to be written to, and optionally specifying whether the data should be appended to the end of an existing ...
🌐
Programiz
programiz.com › java-programming › bufferedwriter
Java BufferedWriter (With Examples)
When we run the program, the file output.txt is filled with the text represented by the string data. To close the buffered writer, we can use the close() method. Once the close() method is called, we cannot use the writer to write the data. To learn more, visit Java BufferedWriter (official ...
🌐
Oracle
docs.oracle.com › en › java › javase › 26 › docs › api › java.base › java › io › FileWriter.html
FileWriter (Java SE 26 & JDK 26)
March 16, 2026 - Constructs a FileWriter given a file name, charset and a boolean indicating whether to append the data written. ... Closes the stream, flushing it first. ... Flushes the stream. ... Returns the name of the character encoding being used by this stream. ... Writes a portion of an array of characters. ... Writes a single character. ... Writes a portion of a string. ... Appends the specified character to this writer...
🌐
GitHub
github.blog › home › ai & ml › github copilot › how to write a great agents.md: lessons from over 2,500 repositories
How to write a great agents.md: Lessons from over 2,500 repositories - The GitHub Blog
November 25, 2025 - Learn how to write effective agents.md files for GitHub Copilot with practical tips, real examples, and templates from analyzing 2,500+ repositories.
🌐
Baeldung
baeldung.com › home › java › java io › java – write to file
Java - Write to File | Baeldung
December 1, 2023 - Note how we’re not only writing a raw String to a file, but also some formatted text with the printf method. We can create the writer using FileWriter, BufferedWriter, or even System.out.
🌐
Classpath
developer.classpath.org › doc › java › io › FileWriter.html
Class FileWriter - java.io - developer.classpath.org!
java.io.Writer · java.io.OutputStreamWriter · java.io.FileWriter · Implemented Interfaces: Appendable, Closeable, Flushable · public class FileWriter · extends OutputStreamWriter · This is a convenience class for writing to files. It creates an FileOutputStream and initializes an ...
🌐
Programiz
programiz.com › java-programming › filewriter
Java FileWriter (With Examples)
In this tutorial, we will learn about Java FileWriter and its methods with the help of examples. The FileWriter class of the java.io package can be used to write data (in characters) to files.
🌐
Humanlayer
humanlayer.dev › blog › writing-a-good-claude-md
Writing a good CLAUDE.md | HumanLayer Blog
November 25, 2025 - Similarly, coding agent harnesses such as Claude Code usually require you to manage agents' memory explicitly. CLAUDE.md (or AGENTS.md) is the only file that by default goes into every single conversation you have with the agent.