codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

43 / 65 topics
43Java Files44Java Create & Write Files45Java Read Files46Java Delete Files47Java I/O Streams48FileInputStream & FileOutputStream49BufferedReader & BufferedWriter
Tutorials/Java Programming/Java Files
☕Java Programming

Java Files

Updated 2026-05-12
30 min read

Java Files

In this tutorial, we will explore how to work with files in Java using the File class. Understanding file handling is crucial for any application that needs to interact with data stored on disk. Whether you're reading from or writing to files, Java provides a robust set of classes and methods to manage these operations efficiently.

Introduction

Java's java.io.File class provides an abstract representation of file and directory pathnames. It allows you to perform basic operations such as checking if a file exists, getting the name of a file, and determining its size. These operations are fundamental for any application that needs to interact with files on disk.

Core Content

Understanding the File Class

The File class is part of the java.io package and represents the pathnames of files and directories. It provides methods to check if a file exists, retrieve the name of the file, get the size of the file, and more.

Creating a File Object

To work with a file in Java, you first need to create an instance of the File class by providing the path to the file.

CreateFileObject.java
1import java.io.File;
2
3public class CreateFileObject {
4 public static void main(String[] args) {
5 // Creating a File object for an existing file
6 File file = new File("example.txt");
7
8 System.out.println("File path: " + file.getAbsolutePath());
9 };
10}
Output
File path: /path/to/your/example.txt

Basic File Methods

Checking if a File Exists

The exists() method of the File class checks whether the file or directory denoted by this abstract pathname exists.

CheckIfFileExists.java
1import java.io.File;
2
3public class CheckIfFileExists {
4 public static void main(String[] args) {
5 File file = new File("example.txt");
6
7 if (file.exists()) {
8 System.out.println("The file exists.");
9 } else {
10 System.out.println("The file does not exist.");
11 }
12 }
13}
Output
The file exists.

Getting the Name of a File

The getName() method returns the name of the file or directory denoted by this abstract pathname.

GetFileName.java
1import java.io.File;
2
3public class GetFileName {
4 public static void main(String[] args) {
5 File file = new File("example.txt");
6
7 System.out.println("File name: " + file.getName());
8 }
9}
Output
File name: example.txt

Getting the Size of a File

The length() method returns the length of the file in bytes. If the pathname denotes a directory, then this value is not defined.

GetFileSize.java
1import java.io.File;
2
3public class GetFileSize {
4 public static void main(String[] args) {
5 File file = new File("example.txt");
6
7 if (file.exists()) {
8 System.out.println("File size: " + file.length() + " bytes");
9 } else {
10 System.out.println("The file does not exist.");
11 }
12 }
13}
Output
File size: 1024 bytes

Practical Example

Let's create a practical example that combines the use of the File class and its methods to check if a file exists, get its name, and print its size.

FileOperations.java
1import java.io.File;
2
3public class FileOperations {
4 public static void main(String[] args) {
5 // Specify the path to the file
6 String filePath = "example.txt";
7
8 // Create a File object
9 File file = new File(filePath);
10
11 // Check if the file exists
12 if (file.exists()) {
13 // Get and print the file name
14 System.out.println("File name: " + file.getName());
15
16 // Get and print the file size
17 System.out.println("File size: " + file.length() + " bytes");
18 } else {
19 System.out.println("The file does not exist.");
20 }
21 }
22}
Output
File name: example.txt
File size: 1024 bytes

Summary

ConceptDescription
File ClassRepresents the pathnames of files and directories.
exists()Checks if a file or directory exists.
getName()Returns the name of the file or directory.
length()Returns the size of the file in bytes.

What's Next?

In the next tutorial, we will learn how to create and write files using Java. This will build on what you've learned about handling files and will introduce more advanced file operations such as writing data to a file.

Stay tuned for more!


PreviousJava try-with-resourcesNext Java Create & Write Files

Recommended Gear

Java try-with-resourcesJava Create & Write Files