Working with the File System in Java: From Basics to Advanced Techniques
Working with the file system is one of the fundamental tasks in programming. Java provides developers with powerful and flexible tools for managing files and directories. In this article, we will thoroughly examine how to work with the file system in Java, using both the classic java.io package and the modern java.nio.file package. You will learn how to create, read, write files, manage directories, and handle exceptions.
Basics of Working with Files in Java: The File Class
The java.io.File class is the cornerstone of working with the file system in Java. It represents an abstract representation of a path to a file or directory. It is important to understand that File does not store the contents of a file, but only provides methods for checking properties and performing operations.
Let's look at basic operations with File:
import java.io.File;import java.io.IOException;
public class FileExample { public static void main(String[] args) { // Creating a File object File file = new File("example.txt"); File directory = new File("myDirectory");
// Checking existence System.out.println("File exists: " + file.exists()); System.out.println("Directory exists: " + directory.exists());
// Creating a new file try { if (file.createNewFile()) { System.out.println("File created: " + file.getName()); } else { System.out.println("File already exists"); } } catch (IOException e) { System.err.println("Error creating file: " + e.getMessage()); }
// Creating a directory if (directory.mkdir()) { System.out.println("Directory created: " + directory.getName()); }
// Getting file information System.out.println("Absolute path: " + file.getAbsolutePath()); System.out.println("File size: " + file.length() + " bytes"); System.out.println("Readable: " + file.canRead()); System.out.println("Writable: " + file.canWrite());
// Deleting a file if (file.delete()) { System.out.println("File deleted"); } }}The File class also allows working with lists of files in a directory:
File currentDir = new File(".");String[] contents = currentDir.list();for (String item : contents) { System.out.println(item);}Reading and Writing Files: I/O Streams
For direct work with file contents, Java uses I/O streams. The main classes for working with text files are FileReader/FileWriter and BufferedReader/BufferedWriter.
Reading a File Line by Line
import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;
public class ReadFileExample { public static void main(String[] args) { String fileName = "input.txt"; try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) { String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { System.err.println("Error reading file: " + e.getMessage()); } }}Note the try-with-resources construct. It automatically closes the stream, preventing resource leaks.
Writing to a File
import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;
public class WriteFileExample { public static void main(String[] args) { String fil