0% found this document useful (0 votes)
9 views2 pages

File Handling in Java: User Input & I/O

The document provides instructions for 4 programming activities: 1) Write a program to create a file called data.txt and write a user's name to it using PrintWriter or BufferedWriter. 2) Write a program to check if data.txt exists, and if not, display a message. If it does, prompt the user to delete it. 3) Create an ArrayList, add 3 Strings, and use ObjectOutputStream to write it to data.bin. 4) Use ObjectInputStream to load the ArrayList from data.bin and display the Strings.

Uploaded by

Sirius Frost
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

File Handling in Java: User Input & I/O

The document provides instructions for 4 programming activities: 1) Write a program to create a file called data.txt and write a user's name to it using PrintWriter or BufferedWriter. 2) Write a program to check if data.txt exists, and if not, display a message. If it does, prompt the user to delete it. 3) Create an ArrayList, add 3 Strings, and use ObjectOutputStream to write it to data.bin. 4) Use ObjectInputStream to load the ArrayList from data.bin and display the Strings.

Uploaded by

Sirius Frost
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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.

You might also like