Working with the File System in Java: In Practice
\n\nIn modern Java development, working with the file system is one of the fundamental tasks. Whether it's saving configurations, processing logs, or loading user data, the ability to competently interact with files and directories is necessary for every developer. In this article, we will cover the practical aspects of working with the file system in Java, using both the classic approach (the File class) and the modern NIO.2 API (Path, Files).
The material is oriented towards practitioners: you will get ready-made code examples that can be immediately used in your projects. We will look at creating, reading, writing, copying, and deleting files, as well as working with directories.
\n\nThe Classic Approach: The File Class
\n\nBefore the advent of Java 7, the main tool for working with the file system was the java.io.File class. Despite being considered legacy, it can still be found in older projects. Let's look at the basic operations.
Creating a file:
\nimport java.io.File;\nimport java.io.IOException;\n\npublic class FileExample {\n public static void main(String[] args) {\n File file = new File("example.txt");\n try {\n if (file.createNewFile()) {\n System.out.println("File created: " + file.getName());\n } else {\n System.out.println("File already exists.");\n }\n } catch (IOException e) {\n System.out.println("Error creating file: " + e.getMessage());\n }\n }\n}\n\nChecking file existence and properties:
\nFile file = new File("example.txt");\nif (file.exists()) {\n System.out.println("File exists");\n System.out.println("Absolute path: " + file.getAbsolutePath());\n System.out.println("Size: " + file.length() + " bytes");\n System.out.println("Readable: " + file.canRead());\n System.out.println("Writable: " + file.canWrite());\n}\n\n- \n
exists()— checks if the file or directory exists. \n isFile()— checks if the object is a file. \n isDirectory()— checks if the object is a directory. \n length()— returns the file size in bytes. \n delete()— deletes a file or an empty directory. \n
The disadvantage of the File class is the lack of support for symbolic links, metadata, and limited exception handling. For new projects, it is recommended to use NIO.2.
The Modern Approach: NIO.2 (Path and Files)
\n\nStarting with Java 7, the JDK includes the java.nio.file package, providing a more flexible and performant API. The main classes are: Path (represents a path to a file or directory) and Files (contains static methods for operations).
Creating a file using Files:
\nimport java.nio.file.Files;\nimport java.nio.file.Path;\nimport java.nio.file.Paths;\nimport java.io.IOException;\n\npublic class NioExample {\n public static void main(String[] args) {\n Path path = Paths.get("nio_example.txt");\n try {\n Files.createFile(path);\n System.out.println("File created: " + path.toAbsolutePath());\n } catch (IOException e) {\n System.out.println("Error: " + e.getMessage());\n }\n }\n}\n\nCreating directories:
\nPath dirPath = Paths.get("myDir/subDir");\ntry {\n Files.createDirectories(dirPath); // creates all missing parent directories\n System.out.println("Directory created: " + dirPath);\n} catch (IOException e) {\n e.printStackTrace();\n}\n\nReading and writing files:
\nThe simplest way to read