Sie sind auf Seite 1von 7

Department of Computing

CS 212: Object Oriented Programming

Class: BESE 8AB


Lab 11: Files & Streams

Date: May 11, 2018


Instructor: Hirra Anwar

CS 212: Object Oriented Programming Page 1


Learning Objectives

The learning objective of this lab is to understand and practice the concept of file handling in Java.

Lab Task #1

Accounts System

Write a program to create a simple sequential-access file that might be used in an accounts
receivable system to keep track of the amounts owed to a company by its credit clients. For each
client, the program obtains from the user an account number and the client’s name and
balance (i.e., the amount the client owes the company for goods and services received). Each
client’s data constitutes a “record” for that client.

Class CreateTextFile
Now as a next step create a class named CreateTextFile which would initially open the stream,
write the account records on the file and then close the stream. Note that in this case each attribute
would be written separately in the text file. Object serialization will not be used.
For writing the records to the file, you can either use Character Based Stream or Byte Based
Stream as discussed in the class. Use respective write methods and write FIVE records (account
no., name, balance) of five customers to the text file.

Exception Handling:
Perform exception handling (try catch, finally blocks) as and when required. Show user appropriate
messages when an error is thrown.
Explore the exception class hierarchy online and cater for FileNotFoundException and
NoSuchElementException types exceptions in your code.

Class CreateTextFileTest
Now create a test program that creates a CreateTextFile object, which is then used to open, add
records to and close the file. Take input of five users for five accounts, then enter end-of-file to
signal that data entry is complete.

Class ReadTextFile
Once you are done with writing the text data to the file, create a ReadTextFile and implement the
logic of reading all the data from the file. Make sure to open the connection and then read and close
it eventually. You can either use Scanner or Character based or Stream based classes for reading
data from the file.

CS 212: Object Oriented Programming Page 2


CS 212: Object Oriented Programming Page 3
Lab Task #2

CS 212: Object Oriented Programming Page 4


Continue improving the Account System developed in Task #1.

In the previous task you have written programs to write the individual fields of an AccountRecord
object into a file as text, and how to read those fields from a file and place their values into an
AccountRecord object in memory. AccountRecord was used to aggregate the information for one
record. When the instance variables for an AccountRecord were output to a disk file, certain
information was lost, such as the type of each value.
For instance, if the value "3" is read from a file, there’s no way to tell whether it came from an int, a
String or a double.We have only data, not type information, on a disk. If the program that’s going to
read this data “knows” what object type the data corresponds to, then the data is simply read into
objects of that type. For example, if we know that we’re inputting an int (the account number),
followed by two Strings (the first and last name) and a double (the balance). We also know that
these values are separated by spaces, with only one record on each line. However Sometimes we’ll
not know exactly how the data is stored in a file.

In such cases, we want to read or write an entire object from a file. Java provides such a mechanism,
called object serialization. A so-called serialized object is an object represented as a sequence of
bytes that includes the object’s data as well as information about the object’s type and the types of
data stored in the object. After a serialized object has been written into a file, it can be read from the
file and deserialized—that is, the type information and bytes that represent the object and its data
can be used to recreate the object in memory

Now your task is to create a copy of the above classes and modify the program & implement the
concept of object serialization. Create the following classes and read and write the accounts info
using the serialized and de-serialized objects.
Class AccountRecordSerializable and rest of the testing classes accordingly.

 Once you complete both the tasks, check whether .ser file is required for object
serialization or not.
 Read and write data from a .png and .jpeg files.

CS 212: Object Oriented Programming Page 5


CS 212: Object Oriented Programming Page 6
Submission

Compile a single word file and upload on LMS.

CS 212: Object Oriented Programming Page 7

Das könnte Ihnen auch gefallen