Skip to content

The-Java-Bootcamp/10.0.1-lab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lab: 10.0.1

Objective:

  • Understand the purpose and implementation of FileInputStream and FileOutputStream 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.

  1. Create a FileManager class with the following methods:

    • createFile(String filename): Creates a new file with the specified name
    • writeToFile(String filename, String content, boolean append): Writes content to a file (append or overwrite)
    • readFromFile(String filename): Reads and returns the content of a file
    • copyFile(String sourceFile, String destinationFile): Copies content from one file to another
  2. Implement a TextEditor class that uses the FileManager 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
  3. 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
  4. 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
}

Hints

  1. Use a do-while loop in the start() method to keep the text editor running until the user chooses to exit.
  2. Remember to use try-with-resources when working with FileInputStream and FileOutputStream to ensure they are properly closed.
  3. 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.
  4. For appending to a file, use the FileOutputStream constructor with the append parameter set to true.
  5. When copying files, use a buffer to read from the source and write to the destination in chunks.
  6. Don't forget to handle both FileNotFoundException and IOException in your try-catch blocks.
  7. Consider using a StringBuilder to build the file content string when reading from a file.

Submission Instructions

  1. Fork the repository
  2. Clone your fork
  3. Navigate into the repository
  4. Implement the required methods in both TextEditor and FileManager classes
  5. Test your implementation with various inputs
  6. Git add, commit, and push to your fork
  7. Submit a pull request
  8. Set the title of the pull request to your first name and last name
  9. 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages