🚀 Why this is a great post:
- File Handling Mastery: Learn how to manipulate files in Java using both old-school and modern I/O techniques.
- Real-World Project: Build a simple file management system that lets you create, delete, move, and list files.
- Beginner to Intermediate Level: Perfect for those starting with file I/O in Java.
🛠️ Project Overview:
Feature | Tech Used |
---|---|
File Operations | Java I/O (File, FileInputStream, FileOutputStream, etc.) |
Simple CLI Interface | Java Standard Library |
Basic File Management | Create, List, Delete, Move |
📁 File Structure:
FileManager.java
: Main class for handling all file operations.FileManagerUI.java
: (Optional) A simple console-based user interface.
🧾 Full Source Code:
FileManager.java (Handles File Operations)
import java.io.*;
import java.nio.file.*;
import java.util.*;
public class FileManager {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("File Management System:");
System.out.println("1. Create a file");
System.out.println("2. List files in a directory");
System.out.println("3. Delete a file");
System.out.println("4. Move a file");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
createFile();
break;
case 2:
listFiles();
break;
case 3:
deleteFile();
break;
case 4:
moveFile();
break;
case 5:
System.out.println("Exiting...");
System.exit(0);
break;
default:
System.out.println("Invalid choice, please try again.");
}
}
}
private static void createFile() {
System.out.print("Enter file path to create: ");
String filePath = scanner.nextLine();
try {
File file = new File(filePath);
if (file.createNewFile()) {
System.out.println("File created: " + file.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred while creating the file.");
e.printStackTrace();
}
}
private static void listFiles() {
System.out.print("Enter directory path to list files: ");
String dirPath = scanner.nextLine();
File dir = new File(dirPath);
if (dir.exists() && dir.isDirectory()) {
String[] files = dir.list();
if (files != null && files.length > 0) {
System.out.println("Files in directory " + dirPath + ":");
for (String file : files) {
System.out.println(file);
}
} else {
System.out.println("No files in this directory.");
}
} else {
System.out.println("Invalid directory path.");
}
}
private static void deleteFile() {
System.out.print("Enter file path to delete: ");
String filePath = scanner.nextLine();
File file = new File(filePath);
if (file.exists() && file.delete()) {
System.out.println("File deleted: " + file.getName());
} else {
System.out.println("File not found or cannot be deleted.");
}
}
private static void moveFile() {
System.out.print("Enter source file path: ");
String sourcePath = scanner.nextLine();
System.out.print("Enter destination path: ");
String destPath = scanner.nextLine();
try {
Path source = Paths.get(sourcePath);
Path destination = Paths.get(destPath);
Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING);
System.out.println("File moved to: " + destPath);
} catch (IOException e) {
System.out.println("An error occurred while moving the file.");
e.printStackTrace();
}
}
}
🧠 Code Breakdown (Line-by-Line)
FileManager.java Breakdown
Scanner scanner = new Scanner(System.in);
- This initializes the
Scanner
class to take user input from the console.
- This initializes the
System.out.println("1. Create a file");
- This prints a menu offering different file operations like creating, listing, deleting, or moving files.
int choice = scanner.nextInt();
- Reads the user’s choice from the console and stores it in the variable
choice
.
- Reads the user’s choice from the console and stores it in the variable
File file = new File(filePath);
- Creates a new
File
object representing the file at the specifiedfilePath
.
- Creates a new
if (file.createNewFile()) { ... }
- Attempts to create the file at the specified path. If successful, it prints a success message.
String[] files = dir.list();
- Lists the names of files in the directory specified by the user.
file.delete();
- Deletes the specified file if it exists and prints a confirmation message.
Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING);
- Moves the file from the source path to the destination path, replacing the file at the destination if it exists.
🧪 How to Run the Application:
- Compile the
FileManager
class:javac FileManager.java
- Run the
FileManager
program:java FileManager
- Interact with the CLI:
The program will present a simple menu. Use the options to create, list, delete, or move files.
💡 Bonus Features to Implement:
- File Search: Implement a search feature that allows users to search for files by name or extension.
- Directory Creation: Add functionality to create directories.
- File Backup: Implement a backup feature to copy files to a backup folder.
- Log Operations: Track all file operations (create, delete, move) in a log file.
✍️ Final Thoughts:
This file management system is a practical project that covers the basics of file I/O in Java. It’s a simple, but powerful, tool that can be expanded with more advanced features like error handling, search functionality, and backup systems. Working with Java’s built-in I/O classes can help you gain a deeper understanding of how to manipulate files and directories in real-world applications.
This project is perfect for beginners who are looking to understand the fundamentals of file management in Java. With just a few lines of code, you’ve created a command-line tool for managing files, and you can easily extend it into a more complex project!