You are on page 1of 2

Problem C

Objectives:
To write data to a file using the PrintWriter class (§12.11.1).
To read data from a file using the Scanner class (§12.11.3).

Task: Develop a Java application that demonstrates writing data to a file using the
PrintWriter class and reading data from a file using the Scanner class.

Description: You have been assigned the task of creating a file input and output
application. The application should allow the user to write data to a file and read data
from a file using Java's PrintWriter and Scanner classes:

1. Create a class called "FileIOManager" with the following methods: a. A method


named "writeToFile" that accepts a file name/path and a string of data as
parameters. The method should write the data to the specified file using the
PrintWriter class. Handle any exceptions that may occur.
b. A method named "readFromFile" that accepts a file name/path as a parameter.
The method should read the data from the specified file using the Scanner class
and display it on the console. Handle any exceptions that may occur.
2. In the main method of the "FileIOManager" class, prompt the user to choose an
operation to perform (e.g., write to file, read from file).
3. Depending on the user's choice, prompt the user to provide the necessary inputs
(e.g., file name/path, data to write) and call the respective methods to perform
the operation.
4. Test your application by writing data to a file and then reading it back from the
file. Verify that the data is correctly written and read.

Example Output:

Welcome to the File I/O Manager! Choose an operation to perform:


1. Write to file
2. Read from file
3. Exit Enter your choice: 1
Enter the file name/path: data.txt Enter the data to write: Hello, World!
Data written to file successfully.
Choose an operation to perform:
1. Write to file
2. Read from file
3. Exit Enter your choice: 2
Enter the file name/path: data.txt
Data read from file: Hello, World!
Choose an operation to perform:
4. Write to file
1. Read from file
2. Exit Enter your choice: 3
Exiting...
Problem D(bonus)
Objectives:
To develop a program that replaces text in a file (§12.11.5).
To read data from the Web (§12.12).

Task: Create a program that replaces specific text in a file and retrieves data from a web
page. The program should allow the user to specify the file to modify, the text to replace,
and the replacement text. Additionally, the program should be able to fetch data from a
web page and display it on the console:
1. Create a class called "TextReplaceAndWebDataRetrieval" with the following
methods: a. A method named "replaceTextInFile" that accepts a file name/path,
the text to replace, and the replacement text as parameters. The method should
read the contents of the file, replace all occurrences of the specified text with the
replacement text, and write the modified contents back to the file. Handle any
exceptions that may occur.
b. A method named "fetchWebData" that accepts a URL as a parameter. The
method should retrieve the data from the web page specified by the URL and
display it on the console. Handle any exceptions that may occur.
2. In the main method of the "TextReplaceAndWebDataRetrieval" class, prompt the
user to choose an operation to perform (e.g., replace text in file, fetch web data).
3. Depending on the user's choice, prompt the user to provide the necessary inputs
(e.g., file name/path, text to replace, replacement text, URL) and call the
respective methods to perform the operation.
4. Test your program by replacing text in a file and retrieving data from a web page.
Verify that the text is replaced correctly and the web data is displayed on the
console.

Example Output:

Welcome to the Text Replace and Web Data Retrieval program! Choose an operation to perform:
1. Replace text in file
2. Fetch web data
3. Exit Enter your choice: 1
Enter the file name/path: data.txt Enter the text to replace: old_text Enter the replacement text:
new_text
Text replaced in file successfully.
Choose an operation to perform:
1. Replace text in file
2. Fetch web data
3. Exit Enter your choice: 2
Enter the URL: https://www.example.com
Web Data: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Choose an operation to perform:
1. Replace text in file
2. Fetch web data
3. Exit Enter your choice: 3
Exiting...

You might also like