🌐
W3Schools
w3schools.com › java › java_files.asp
Java Files
If you don't know what a package is, read our Java Packages Tutorial. The File class has many useful methods for creating and getting information about files. For example:
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › Files.html
Files (Java Platform SE 8 )
April 21, 2026 - For example, suppose we want to iterate over the files ending with ".java" in a directory:
🌐
GeeksforGeeks
geeksforgeeks.org › java › file-class-in-java
Java File Class - GeeksforGeeks
November 3, 2025 - Example 1: Program to check if a file or directory physically exists or not. ... import java.io.File; // Displaying file property class CheckFileExist { public static void main(String[] args){ // Accept file name or directory name through // ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-files-nio-files-class
Java Files - java.nio.file.Files Class | DigitalOcean
August 4, 2022 - Pre Visit Directory: D:\pankaj Pre Visit Directory: D:\pankaj\java6 Pre Visit Directory: D:\pankaj\java6\Files Visit File: D:\pankaj\java6\Files\file.txt.txt Post Visit Directory: D:\pankaj\java6\Files Post Visit Directory: D:\pankaj\java6 Pre Visit Directory: D:\pankaj\java7 Pre Visit Directory: D:\pankaj\java7\Files Visit File: D:\pankaj\java7\Files\file.txt.txt Post Visit Directory: D:\pankaj\java7\Files Post Visit Directory: D:\pankaj\java7 Pre Visit Directory: D:\pankaj\java8 Pre Visit Directory: D:\pankaj\java8\Files Visit File: D:\pankaj\java8\Files\file.txt.txt Post Visit Directory: D:\pankaj\java8\Files Post Visit Directory: D:\pankaj\java8 Post Visit Directory: D:\pankaj · Notice that all the files and folders are processed recursively.
🌐
Programiz
programiz.com › java-programming › file
Java File (With Examples)
In this tutorial, we will learn about the Java File class with the help of examples. The File class of the java.io package is used to perform various operations on files and directories
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › file-handling-in-java
File Handling in Java - GeeksforGeeks
File Created! In Java, I/O streams are used to perform input and output operations on files.
Published   March 13, 2026
🌐
Mkyong
mkyong.com › home › java › java files.find examples
Java Files.find examples - Mkyong.com
December 2, 2020 - This example uses Files.find() to find files that matched filename google.png, starting from the folder C:\\test, and included all levels of its subfolders. ... package com.mkyong.io.api; import java.io.IOException; import java.nio.file.Files; ...
Find elsewhere
🌐
Jenkov
jenkov.com › tutorials › java-io › file.html
Java File
December 17, 2019 - You can use the Java File class to create directories if they don't already exists. The File class contains the method mkdir() and mkdirs() for that purpose. The mkdir() method creates a single directory if it does not already exist. Here is an example of creating a single directory via the Java File class:
🌐
Tutorialspoint
tutorialspoint.com › java › java_files_io.htm
Java - Files and I/O
Failure indicates that the path specified in the File object already exists, or that the directory cannot be created because the entire path does not exist yet. The mkdirs() method creates both a directory and all the parents of the directory. Following example creates "/tmp/user/java/bin" directory −
🌐
Dev.java
dev.java › learn › modernio
Common I/O Tasks in Modern Java - Dev.java
Your favorite JSON library is likely to have methods for reading from a file or URL. For example, with Jackson jr: Here is how to read the dog image from the preceding call: This is better than passing an input stream to the read method, because the library can use additional information from the URL to determine the image type. The java.nio.file.Files class provides a comprehensive set of file operations, such as creating, copying, moving, and deleting files and directories.
🌐
W3Schools
w3schools.com › java › java_files_create.asp
Java Create Files
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP W3.CSS C C++ C# HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS ... Variables Print Variables Multiple Variables Identifiers Constants (Final) Real-Life Examples Code Challenge Java Data Types
🌐
Microsoft Learn
learn.microsoft.com › en-us › azure › storage › files › storage-java-how-to-use-file-storage
Develop for Azure Files with Java | Microsoft Learn
To use Java file I/O libraries, you must first mount a file share. See the following resources for guidance on how to mount a file share using SMB or NFS: ... The following code example shows how to connect to a file share and list the directories in the share:
🌐
DEV Community
dev.to › be11amer › file-handling-in-java-a-comprehensive-guide-3nn2
File Handling in Java: A Comprehensive Guide - DEV Community
August 3, 2025 - Each entry (file or directory) can be extracted using the loop. Challenge: Write a Java program that reads all .txt files from a ZIP archive and prints their content to the console.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › file.html
Reading, Writing, and Creating Files (The Java™ Tutorials > Essential Java Classes > Basic I/O)
The following example, written for UNIX and other POSIX file systems, creates a log file with a specific set of file permissions. This code creates a log file or appends to the log file if it already exists. The log file is created with read/write permissions for owner and read only permissions for group. import static java.nio.file.StandardOpenOption.*; import java.nio.*; import java.nio.channels.*; import java.nio.file.*; import java.nio.file.attribute.*; import java.io.*; import java.util.*; public class LogFilePermissionsTest { public static void main(String[] args) { // Create the set of options for appending to the file.
🌐
Vogella
vogella.com › tutorials › JavaIO › article.html
Reading and writing files in Java (Input/Output) - Tutorial
February 26, 2026 - 1.4. Writing a file in Java · 1.5. List all files and sub-directories using Files.list() 1.6. How to identify the current directory · 2. Exercise: Reading and writing files · 3. Example: Recursively listing all files of a diretory · 4. Example: Deleting a directory with all subdirectories and files ·
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-read-file-line-by-line
Java Read File: Complete Guide with Examples | DigitalOcean
February 20, 2025 - Continue your learning with the Files API Doc (Java SE 8). You can use RandomAccessFile to open a file in read mode and then use its readLine method to read a file line-by-line. Here is an example program to read a file line-by-line with RandomAccessFile:
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).

🌐
Medium
medium.com › @AlexanderObregon › javas-files-write-method-explained-288e75dfc721
Java’s Files.write() Method Explained | Medium
March 20, 2025 - In the example above, using BufferedWriter requires explicit resource management, with a try-finally block to make sure that the writer is closed even if an exception occurs. This pattern, while effective, is cumbersome and can easily lead to bugs if not implemented correctly. ... import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.io.IOException; public class NewBufferedWriterExample { public static void main(String[] args) { Path path = Paths.get("example.txt"); String data = "Hello, World!"; try { Files.write(path, data.getBytes()); } catch (IOException e) { e.printStackTrace(); } } }
🌐
Visual Studio Code
code.visualstudio.com › docs › java › java-tutorial
Getting Started with Java in VS Code
November 3, 2021 - Create a folder for your Java program and open the folder with VS Code. Then in VS Code, create a new file and save it with the name Hello.java.