Java Files

Understanding file handling in Java programming

📁 What are Java Files?

Java provides powerful file handling capabilities through the File class and I/O streams. You can create, read, write, and delete files easily using built-in Java methods for data storage and manipulation.


// Simple file example
import java.io.File;

public class FileExample {
    public static void main(String[] args) {
        File myFile = new File("example.txt");
        System.out.println("File exists: " + myFile.exists());
    }
}
                                    

Output:

File exists: false

Key Java File Concepts

📄

File Class

Represents files and directories

File file = new File("data.txt");
📝

FileWriter

Writes text data to files

FileWriter writer = new FileWriter("file.txt");
📖

Scanner

Reads data from files

Scanner scanner = new Scanner(file);
🗂️

Path Operations

Handle file paths and directories

String path = file.getAbsolutePath();

🔹 Basic File Operations

Java provides several ways to work with files:

import java.io.File;
import java.io.IOException;

public class BasicFileOps {
    public static void main(String[] args) {
        try {
            // Create a File object
            File myFile = new File("myfile.txt");
            
            // Check if file exists
            if (myFile.exists()) {
                System.out.println("File exists!");
                System.out.println("File name: " + myFile.getName());
                System.out.println("File size: " + myFile.length() + " bytes");
            } else {
                System.out.println("File does not exist.");
            }
        } catch (Exception e) {
            System.out.println("An error occurred: " + e.getMessage());
        }
    }
}

Output:

File does not exist.

🔹 File Information Methods

Get useful information about files:

import java.io.File;

public class FileInfo {
    public static void main(String[] args) {
        File file = new File("example.txt");
        
        // File properties
        System.out.println("File name: " + file.getName());
        System.out.println("Absolute path: " + file.getAbsolutePath());
        System.out.println("Is file: " + file.isFile());
        System.out.println("Is directory: " + file.isDirectory());
        System.out.println("Can read: " + file.canRead());
        System.out.println("Can write: " + file.canWrite());
    }
}

Output:

File name: example.txt
Absolute path: /path/to/example.txt
Is file: false
Is directory: false
Can read: false
Can write: false

🔹 Exception Handling

Always handle exceptions when working with files:

import java.io.File;
import java.io.IOException;

public class SafeFileHandling {
    public static void main(String[] args) {
        try {
            File file = new File("test.txt");
            
            if (file.createNewFile()) {
                System.out.println("File created successfully!");
            } else {
                System.out.println("File already exists.");
            }
            
        } catch (IOException e) {
            System.out.println("Error occurred: " + e.getMessage());
        }
    }
}

🧠 Test Your Knowledge

Which class is used to represent files in Java?