Professional Documents
Culture Documents
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:
Example Output:
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...