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);
Answer from Michael on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › File.html
File (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... An abstract representation of file and directory pathnames.
🌐
W3Schools
w3schools.com › java › java_files.asp
Java Files
Java has several methods for creating, reading, updating, and deleting files.
🌐
GeeksforGeeks
geeksforgeeks.org › java › file-class-in-java
Java File Class - GeeksforGeeks
November 3, 2025 - The File class in Java is used to represent the path of a file or folder. It helps in creating, deleting, and checking details of files or directories, but not in reading or writing data.
🌐
Oracle
docs.oracle.com › en › java › javase › 22 › docs › api › java.base › java › io › File.html
File (Java SE 22 & JDK 22)
July 16, 2024 - Instances of the File class are immutable; that is, once created, the abstract pathname represented by a File object will never change. The java.nio.file package defines interfaces and classes for the Java virtual machine to access files, file attributes, and file systems.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › io › File.html
File (Java SE 17 & JDK 17)
April 21, 2026 - Instances of the File class are immutable; that is, once created, the abstract pathname represented by a File object will never change. The java.nio.file package defines interfaces and classes for the Java virtual machine to access files, file attributes, and file systems.
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).

🌐
Wikipedia
en.wikipedia.org › wiki › Java_class_file
Java class file - Wikipedia
May 30, 2026 - A Java class file is a file (with the .class filename extension) containing Java bytecode that can be executed on the Java Virtual Machine (JVM). A Java class file is usually produced by a Java compiler from Java programming language source files ( .java files) containing Java classes ...
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java io › the java file class
The Java File Class | Baeldung
February 8, 2025 - In this tutorial, we’ll give an overview of the File class, which is part of the java.io API.
🌐
Baeldung
baeldung.com › home › java › java io › java – path vs file
Java – Path vs File | Baeldung
April 20, 2024 - Since the very first versions, Java has delivered its own java.io package, which contains nearly every class we might ever need to perform input and output operations. The File class is an abstract representation of file and directory pathnames:
🌐
W3Schools
w3schools.com › java › java_files_read.asp
Java Read Files
There are several classes you can use to read files in Java:
🌐
Android Developers
developer.android.com › api reference › file
File | 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 · 中文 – 简体
🌐
YouTube
youtube.com › watch
Java File Input/Output - It's Way Easier Than You Think - YouTube
Complete Java course: https://codingwithjohn.thinkific.com/courses/java-for-beginnersSource Code Available Here: https://codingwithjohn.com/file-io-sourceJav...
Published   April 26, 2021
🌐
Oracle
oracle.com › java › technical details
Code Conventions for the Java Programming Language: 2. File Names
This section lists commonly used file suffixes and names. Java Software uses the following file suffixes: Frequently used file names include: 1 · 2 · 3 · 4 · 5 · 6 · 7 · 8 · 9 · 10 ·
🌐
Oracle
java.com › en
Java | Oracle
Oracle Java is the #1 programming language and development platform. It reduces costs, shortens development timeframes, drives innovation, and improves application services. Java continues to be the development platform of choice for enterprises and developers · For End Users on a Desktop ...
🌐
Protocol Buffers
protobuf.dev › programming-guides › proto3
Language Guide (proto 3) | Protocol Buffers Documentation
For C++, the compiler generates a .h and .cc file from each .proto, with a class for each message type described in your file. For Java, the compiler generates a .java file with a class for each message type, as well as a special Builder class for creating message class instances.
🌐
W3Schools
w3schools.com › Java › java_fileinputstream.asp
Java FileInputStream
FileInputStream - best for binary data (images, audio, PDFs) or when you need full control of raw bytes. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
OneCompiler
onecompiler.com › java
Java Online Compiler
Write, Run & Share Java code online using OneCompiler's Java online compiler for free. It's one of the robust, feature-rich online compilers for Java language, running on Java 25. Getting started with the OneCompiler's Java editor is easy and fast.
🌐
CodeGym
codegym.cc › java blog › java io & nio › java.io.file class
Java.io.File Class
June 6, 2022 - Java File class is to manage information about files and directories. As you know, at the operating system level, files and directories are different objects. However in Java, they both are described by the same File class.
🌐
Playwright
playwright.dev › installation
Installation | Playwright Java
Get started by installing Playwright and running the example file to see it in action. App.java · pom.xml · src/main/java/org/example/App.java ·