NBKRIST JavaHub

Understanding Files and Directories in Java

Expert View πŸ’‘
From my experience, file handling in Java is one of the most fundamental skills a developer must have. Whether it’s logs, reports, configuration, or data exchange β€” files are the heart of persistence. Java’s evolution from java.io.File to java.nio.file reflects how file I/O became faster, safer, and more modern.

What is a File and Directory in Java?

In Java, a file represents a data storage entity (text, image, binary, etc.), while a directory is a folder that can contain files or subdirectories. The java.io.File class allows us to interact with the file system easily.

πŸ’‘ Tip: Always check whether a path points to a file or directory using isFile() or isDirectory().

Example: Checking File or Directory


import java.io.File;

public class FileCheck {
  public static void main(String[] args) {
    File f = new File("sample.txt");
    if (f.isFile()) System.out.println("It is a file.");
    else if (f.isDirectory()) System.out.println("It is a directory.");
    else System.out.println("Path does not exist.");
  }
}
  
πŸ‘‰ Hover to view output

Creating and Deleting Files


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

public class FileCreateDelete {
  public static void main(String[] args) {
    try {
      File file = new File("demo.txt");
      if (file.createNewFile())
        System.out.println("File created: " + file.getName());
      else
        System.out.println("File already exists.");
      if (file.delete())
        System.out.println("File deleted successfully.");
    } catch (IOException e) {
      System.out.println("An error occurred.");
    }
  }
}
  
πŸ‘‰ Hover to view output

Listing Directory Contents


import java.io.File;

public class DirectoryList {
  public static void main(String[] args) {
    File dir = new File(".");
    String[] files = dir.list();
    for (String name : files)
      System.out.println(name);
  }
}
  
πŸ‘‰ Hover to view output

Reading and Writing Files


import java.io.*;

public class FileReadWrite {
  public static void main(String[] args) {
    try {
      FileWriter fw = new FileWriter("hello.txt");
      fw.write("Welcome to NBKRIST JavaHub!");
      fw.close();
      BufferedReader br = new BufferedReader(new FileReader("hello.txt"));
      System.out.println(br.readLine());
      br.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
  
πŸ‘‰ Hover to view output

Modern Approach: java.nio.file (NIO.2)

Introduced in Java 7, the java.nio.file package modernizes file handling with Path and Files classes. It provides methods for reading, writing, copying, moving, and deleting files efficiently.


import java.nio.file.*;
import java.io.IOException;

public class NIOExample {
  public static void main(String[] args) throws IOException {
    Path p = Path.of("nbkrist.txt");
    Files.writeString(p, "NBKRIST: JavaHub Rocks!");
    String content = Files.readString(p);
    System.out.println(content);
  }
}
  
πŸ‘‰ Hover to view output
πŸ’‘ Tip: Use Files and Path for better readability, performance, and modern file operations.

Practice Questions 🧠

1. How can you check if a path is a file or directory?
2. How to create a new file in Java?
3. What are advantages of NIO.2 over java.io.File?

Key Takeaways πŸ“˜

⬆
⬇