Programming 1
Tutorial 11
Activity 1
Write a program that carries out the following tasks:
Open a file with the name [Link]
Store the message "Hello, World!" in the file
Close the file
Open the same file again
Read the message into a String variable and print it out
Hint
When creating a File object using only the file name, the file should be created in the project’s
root folder. Use PrintWriter to write to the file. Use Scanner to read file’s content.
Deliverable
[Link]
Activity 2
Write a program that reads a file named [Link], removes any blank lines, and writes the
non-blank lines back to the file [Link].
Hint
Create a text file named [Link] with random content and several blank lines for this task.
Deliverable
[Link]
Activity 3
Create an ArrayList of Strings. Add 3 Strings to it. Use ObjectOutputStream to write the
ArrayList object to a file called [Link].
Deliverable
[Link]
Activity 4
Use ObjectInputStream to load an ArrayList of Strings from the file [Link] produced by
the last activity and display the Strings on the screen.
Deliverable
[Link]
Activity 5
Write a program that reads a file containing text. The file’s name is [Link] and its content is
given as below:
Mary had a little lamb
Whose fleece was white as snow.
And everywhere that Mary went,
The lamb was sure to go!
Read each line and write it to the output file, named [Link], preceded by line numbers. The
output file should contain:
1. Mary had a little lamb
2. Whose fleece was white as snow.
3. And everywhere that Mary went,
4. The lamb was sure to go!
Deliverable
[Link]