Objective:
- Understand the purpose and implementation of
FileInputStream
andFileOutputStream
in Java - Learn how to read from and write to files using byte stream classes
- Implement exception handling for file operations using try-catch blocks
- Create a practical file management application that demonstrates file I/O operations
- Apply proper resource management techniques when working with files
Prerequisites:
- Basic understanding of Java programming
- Familiarity with Java exception handling (try-catch blocks)
- Knowledge of basic Java data types and arrays
- Understanding of Java classes and object-oriented programming concepts
What You'll Achieve
- Develop a robust file management system that handles reading and writing operations
- Implement proper exception handling for file I/O operations
- Create methods for appending to existing files and creating new ones
- Build a simple text file editor with basic functionality
- Gain practical experience with Java's file handling mechanisms
Assignment Details
In this assignment, you will create a simple text file management system that demonstrates the use of FileInputStream
and FileOutputStream
. Your task is to implement a FileManager
class that allows users to perform various file
operations.
-
Create a FileManager class with the following methods:
createFile(String filename)
: Creates a new file with the specified namewriteToFile(String filename, String content, boolean append)
: Writes content to a file (append or overwrite)readFromFile(String filename)
: Reads and returns the content of a filecopyFile(String sourceFile, String destinationFile)
: Copies content from one file to another
-
Implement a
TextEditor
class that uses theFileManager
to provide the following functionality:- Open an existing text file and display its contents
- Create a new text file with user-provided content
- Append content to an existing file
- Save the current content to a file
-
Use proper exception handling throughout your code:
- Handle
FileNotFoundException
when attempting to read a non-existent file - Handle
IOException
for general I/O errors - Use try-with-resources to ensure proper resource management
- Implement appropriate error messages for different exception scenarios
- Handle
-
In the main method, demonstrate the use of your
TextEditor
class:- Create a simple menu-driven interface for the user to interact with
- Allow users to perform all the supported operations
- Implement proper input validation and error handling
- Display appropriate success and error messages
Example Output
Welcome to Simple Text Editor
--------------------------
1. Create a new file
2. Open an existing file
3. Append to a file
4. Copy a file
5. Exit
--------------------------
Enter your choice: 1
Enter filename: notes.txt
Enter content:
This is a test file.
It contains multiple lines.
This demonstrates file writing in Java.
File created successfully!
--------------------------
1. Create a new file
2. Open an existing file
3. Append to a file
4. Copy a file
5. Exit
--------------------------
Enter your choice: 2
Enter filename: notes.txt
File content:
This is a test file.
It contains multiple lines.
This demonstrates file writing in Java.
--------------------------
1. Create a new file
2. Open an existing file
3. Append to a file
4. Copy a file
5. Exit
--------------------------
Enter your choice: 3
Enter filename: notes.txt
Enter content to append:
This line is appended to the file.
Content appended successfully!
--------------------------
1. Create a new file
2. Open an existing file
3. Append to a file
4. Copy a file
5. Exit
--------------------------
Enter your choice: 4
Enter source filename: notes.txt
Enter destination filename: notes_backup.txt
File copied successfully!
--------------------------
1. Create a new file
2. Open an existing file
3. Append to a file
4. Copy a file
5. Exit
--------------------------
Enter your choice: 5
Thank you for using Simple Text Editor. Goodbye!
Starter Code
The TextEditor.java
and FileManager.java
files contain the following starter code:
package academy.javapro.lab;
import java.util.Scanner;
public class TextEditor {
private FileManager fileManager;
private Scanner scanner;
public TextEditor() {
this.fileManager = new FileManager();
this.scanner = new Scanner(System.in);
}
public void start() {
System.out.println("Welcome to Simple Text Editor");
// TODO: Implement menu-driven interface
// TODO: Implement methods for each menu option
}
// TODO: Implement methods for creating, opening, appending, and copying files
public static void main(String[] args) {
TextEditor editor = new TextEditor();
editor.start();
}
}
package academy.javapro.lab;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileManager {
// TODO: Implement createFile, writeToFile, readFromFile, and copyFile methods
}
- Use a do-while loop in the
start()
method to keep the text editor running until the user chooses to exit. - Remember to use try-with-resources when working with
FileInputStream
andFileOutputStream
to ensure they are properly closed. - When reading from a file, you may want to use a buffer to improve performance. You can read the file in chunks rather than byte by byte.
- For appending to a file, use the
FileOutputStream
constructor with the append parameter set to true. - When copying files, use a buffer to read from the source and write to the destination in chunks.
- Don't forget to handle both
FileNotFoundException
andIOException
in your try-catch blocks. - Consider using a
StringBuilder
to build the file content string when reading from a file.
- Fork the repository
- Clone your fork
- Navigate into the repository
- Implement the required methods in both
TextEditor
andFileManager
classes - Test your implementation with various inputs
- Git add, commit, and push to your fork
- Submit a pull request
- Set the title of the pull request to your first name and last name
- In the comment, briefly explain your implementation approach and any challenges you faced
Remember, the goal is to learn and have fun! Don't hesitate to ask for help if you get stuck.