Programming 1
Tutorial 11
Activity 1
Task
Write a program to create a file called [Link] (located at the program's
working directory). Ask the user to enter his/her name. Write it to the file.
Instructions
The shortest way to write a String to a file is using PrintWriter. Use the
println() method from PrintWriter to do so.
The alternative way is to use BufferedWriter, which requires you to first
create a FileWriter object. Use the write() method from the BufferedWriter
class.
Either way, do not forget to call close() on the PrintWriter or BufferedWriter
object to save data to file.
Activity 2
Task
Write a program to check if the file called [Link] exists or not. If it
doesn't exist, show the message: The file does not exist! on the screen. If it
does, ask the user: Do you want to delete the file (yes/no)?. If the user
types yes, delete the file and show the message: File was deleted!.
(*) Note: To create the file for deleting, run the program in Activity 1.
Activity 3
Task
Create an ArrayList of Strings. Add 3 Strings to it. Use ObjectOutputStream to
write the ArrayList object to a file called [Link]. Refer to Example 4 from
Lecture 9 for an example.
Activity 4
Task
Use ObjectInputStream to load an ArrayList of Strings from the file [Link]
produced by Activity 3 and display the Strings on the screen (if failed, check
the correctness of Activity 3). Refer to Example 5 from Lecture 9 for an
example.