In Java, managing files and directories is an essential skill, especially when building applications that interact with the file system. The Java NIO (New I/O) API provides a more efficient and flexible way to handle file and directory operations compared to the older File API.
Creating, Deleting, and Renaming Files
Java provides various ways to create, delete, and rename files using the Files
class from the java.nio.file
package.
- Creating Files: To create a new file, we can use the
Files.createFile()
method. If the file already exists, an exception will be thrown.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.nio.file.*; import java.io.IOException; public class FileOperations { public static void main(String[] args) { Path path = Paths.get("example.txt"); try { Files.createFile(path); System.out.println("File created successfully!"); } catch (IOException e) { System.out.println("Error creating file: " + e.getMessage()); } } } |
- Deleting Files: To delete a file, we can use the
Files.delete()
method. It throws an exception if the file doesn’t exist.
1 2 3 4 5 6 |
try { Files.delete(path); System.out.println("File deleted successfully!"); } catch (IOException e) { System.out.println("Error deleting file: " + e.getMessage()); } |
- Renaming Files: To rename a file, we can use the
Files.move()
method. This method can also be used to move files to different locations.
1 2 3 4 5 6 7 |
Path newPath = Paths.get("renamed_example.txt"); try { Files.move(path, newPath); System.out.println("File renamed successfully!"); } catch (IOException e) { System.out.println("Error renaming file: " + e.getMessage()); } |
Checking if a File Exists
To check if a file exists, you can use the Files.exists()
method. It returns true
if the file exists, and false
otherwise.
1 2 3 4 5 |
if (Files.exists(path)) { System.out.println("File exists!"); } else { System.out.println("File does not exist."); } |
Getting File Properties
Java allows you to retrieve file properties such as size, last modified date, and file permissions using the Files
class.
- File Size: To get the size of a file, use the
Files.size()
method, which returns the size in bytes.
1 2 3 4 5 6 |
try { long size = Files.size(path); System.out.println("File size: " + size + " bytes"); } catch (IOException e) { System.out.println("Error getting file size: " + e.getMessage()); } |
- Last Modified Date: To get the last modified date of a file, use the
Files.getLastModifiedTime()
method.
1 2 3 4 5 6 |
try { FileTime fileTime = Files.getLastModifiedTime(path); System.out.println("Last modified: " + fileTime); } catch (IOException e) { System.out.println("Error getting last modified date: " + e.getMessage()); } |
- File Permissions: To check the file permissions, you can use
Files.isReadable()
,Files.isWritable()
, andFiles.isExecutable()
methods.
1 2 3 4 5 6 7 8 9 |
if (Files.isReadable(path)) { System.out.println("File is readable."); } if (Files.isWritable(path)) { System.out.println("File is writable."); } if (Files.isExecutable(path)) { System.out.println("File is executable."); } |
Creating Directories and Subdirectories
To create directories and subdirectories, use the Files.createDirectory()
and Files.createDirectories()
methods. The createDirectories()
method is particularly useful as it creates any nonexistent parent directories as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Path dirPath = Paths.get("myDir"); try { Files.createDirectory(dirPath); System.out.println("Directory created!"); } catch (IOException e) { System.out.println("Error creating directory: " + e.getMessage()); } // Creating parent directories if they don't exist Path subDirPath = Paths.get("parentDir/subDir"); try { Files.createDirectories(subDirPath); System.out.println("Subdirectories created!"); } catch (IOException e) { System.out.println("Error creating subdirectories: " + e.getMessage()); } |
Relative and Absolute File Paths
A file path can be absolute or relative. Absolute paths provide the full path from the root directory, while relative paths are based on the current working directory.
- Absolute Path: To get the absolute path of a file, use the
toAbsolutePath()
method.
1 2 3 |
Path relativePath = Paths.get("example.txt"); Path absolutePath = relativePath.toAbsolutePath(); System.out.println("Absolute path: " + absolutePath); |
- Relative Path: Relative paths are useful when you want to specify a file location relative to the current working directory.
1 2 |
Path relativePath = Paths.get("documents/example.txt"); System.out.println("Relative path: " + relativePath); |