Java - File Handling - Hands-on
Learning Objectives
- Understand how to perform file operations in Java.
- Implement reading, writing, checking existence, and copying files.
- Parse a CSV file and construct Java objects.
Exercise 1: Reading a File
Problem Statement
Write a program that reads the contents of a text file and displays it on the console. Handle exceptions for missing or unreadable files.
Instructions:
- Accept a filename as input.
- Read and print the file content line by line.
- Handle
FileNotFoundExceptionandIOExceptionappropriately.
Expected Output:
- The content of the file.
- An error message if the file is missing or unreadable.
Exercise 2: Writing to a File
Problem Statement
Write a program that takes user input and writes it to a text file.
Instructions:
- Accept a filename and content from the user.
- Write the content to the specified file.
- Handle exceptions during the writing process.
Expected Output:
- Confirmation message after successful writing.
- An error message if the operation fails.
Exercise 3: Checking if a File Exists
Problem Statement
Write a program that checks whether a given file exists or not.
Instructions:
- Accept a filename as input.
- Check if the file exists using Java’s File API.
- Display an appropriate message.
Expected Output:
- A message confirming whether the file exists or not.
Exercise 4: Copying a File from Source to Destination
Problem Statement
Write a program to copy a file from a source path to a destination path.
Instructions:
- Accept source and destination file paths as input.
- Copy the file contents from the source to the destination.
- Handle exceptions such as
FileNotFoundExceptionandIOException.
Expected Output:
- Confirmation that the file was copied successfully.
- An error message if the operation fails.
Exercise 5: Reading and Parsing a CSV File
Problem Statement
Write a program that reads a CSV file, parses each row, and constructs Java objects from the data.
Instructions:
- Define a class representing the CSV data (e.g.,
Personwith fieldsname,age,email). - Read the CSV file line by line.
- Split each line and map it to an object.
- Handle exceptions for missing or invalid data.
Expected Output:
- List of Java objects constructed from the CSV file.
- An error message if parsing fails.
Exercise 6: Deleting a File
Problem Statement
Write a program that deletes a specified file and handles potential exceptions.
Instructions:
- Accept a filename as input.
- Check if the file exists and delete it.
- Handle any exceptions that may occur.
Expected Output:
- Confirmation that the file was deleted.
- An error message if deletion fails.
Exercise 7: Appending Data to a File
Problem Statement
Modify an existing file by appending new data instead of overwriting it.
Instructions:
- Accept a filename and content from the user.
- Open the file in append mode and write new data.
- Handle exceptions during the writing process.
Expected Output:
- Confirmation that the data was appended.
- An error message if the operation fails.
Exercise 8: Listing Files in a Directory
Problem Statement
Write a program that lists all files in a specified directory.
Instructions:
- Accept a directory path as input.
- Display all files and subdirectories inside it.
- Handle exceptions if the directory does not exist.
Expected Output:
- A list of files and directories in the specified location.
- An error message if the directory is invalid.
🔗 Related Topics: