Ch-13.pdffgfdgfdgfdgfdgrtrfdgregregergreyh

zmulani8 6 views 23 slides Oct 26, 2025
Slide 1
Slide 1 of 23
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23

About This Presentation

gdfgdfgdfg


Slide Content

Using Disk Data Files
•Let ’ s concentrate first on those classes that deal with directory
and drive information.
•The DriveInfo Class
–“ Everything you could ever possibly want to know . . . ” about
a disk drive is in the DriveInfo class.

Prepared By: Prof. Pooja Kadam

Directory Class
•Table 13 - 2 presents a partial listing for methods found in the
Directory class.
Prepared By: Prof. Pooja Kadam

DirectoryInfo Class
Prepared By: Prof. Pooja Kadam

File Namespace
Prepared By: Prof. Pooja Kadam

Prepared By: Prof. Pooja Kadam

Prepared By: Prof. Pooja Kadam

Prepared By: Prof. Pooja Kadam

Types of Files
•There are two basic types of files:
–Those that contain textual data and
–Those that contain binary data.
–For example, a file extension of txt is normally a textual data file
while those ending in jpg are binary image files.
–Textual Versus Binary Data Files
•Files that contain textual data are usually built from strings
appended to the file. When a user types text into a textbox, that
data is stored in memory as plain text in a string format.
•In other words, text files contain nothing but strings.
•Binary files are a little different from text files. If a number is
written to a binary file, no conversion of that number to a string
occurs.

Prepared By: Prof. Pooja Kadam

Prepared By: Prof. Pooja Kadam

Prepared By: Prof. Pooja Kadam

Prepared By: Prof. Pooja Kadam

•The StreamWriter Object:
–sw = new StreamWriter(Path.Combine(pathName, fileName),
true);
–calls the StreamWriter constructor to instantiate the sw
StreamWriter object
–The first argument, Path.Combine(pathName, filename) ,
combines the pathname and the file name to form a single
argument.
–The second argument is a Boolean with the value true . If the
second argument is true , all new text data is appended to the
end of the file.
–If the file does not exist, the file is created.
–If the second argument is false and the file exists, any current
data in the file is overwritten.
–If the file does not exist and the second argument is false , the
file is created and the new string data is written to the file.
Prepared By: Prof. Pooja Kadam

Sequential Versus Random
Access Files
•Sequential Files:
–The process of continually adding new data to the end of an existing
file creates what is called a sequential file .
–With sequential files, new data is added on to the end of the file.
There are no gaps in the data.
–Indeed, one of the advantages of sequential files is that they are
dense. That is, every byte of storage space in a sequential file is filled
with a piece of information.
–Sequential files are like the old cassette music tapes.
–Advantages and Disadvantages of Sequential Files
•sequential files, the good news is that the files are dense: they waste no
disk space.
•The bad news is that you are forced to read through unwanted data to
get to the data you actually want to use. Also, editing the information in a
sequential file is awkward at best.
Prepared By: Prof. Pooja Kadam

Prepared By: Prof. Pooja Kadam

•Random Access Files
–Random access files are based upon the concept of a fixed
record size.
–You can visualize a random access record as a line of bricks laid
end to end, in which each brick represents the information about
one customer. Because each brick has the same size, it ’ s easy
to pick up brick number five.
–Fixed Record Sizes
•The advantage of random access files is that we can pick up
the File Pointer and skip over the four bricks (that is, records)
you don ’ t want to read and drop the File Pointer down at the
exact beginning of brick five.
•Seek((desiredRecord - 1) * RECORDSIZE, SeekOrigin.Begin);
•Seek((5 - 1) * 300, SeekOrigin.Begin);
•Seek(4 * 300, BOF);
•Seek(1200, BOF);
Prepared By: Prof. Pooja Kadam

•The first argument in the Seek() method is the byte offset that the File
Pointer is to be moved expressed as a long data type.
•The second argument for Seek() is the relative position from which the
File Pointer is to move.
•The second argument of the Seek() method provides three relative
points for moving the File Pointer.
–The first, SeekOrigin.Begin , positions the File Pointer with the byte offset relative to
BOF
–Second, SeekOrigin.Current moves the File Pointer relative to the current location of the
File Pointer.
–For example, if you have read record number five, the File Pointer ’ s current position
would be at the start of record number six.
–If you wish to now read record number four, you could use
Seek(-600, SeekOrigin.Current);
–Because the byte offset is negative, the File Pointer is moved 600 bytes back from its
current position, which would be at the start of record number four.
–Seek(0, SeekOrigin.End); would place the File Pointer at the end of file (EOF).


Prepared By: Prof. Pooja Kadam

•Advantages and Disadvantages of Random Access Files:

–The biggest advantage of random access files is that
you can get to a particular record much faster than
you can with sequential files.
–In fact, it ’ s common to tie a customer ’ s record
location in the file to some unique piece of
information associated with the customer.
–The process of using one piece of information (an S)
to derive a second piece of information (record 83) is
called hashing .
–Using a hash code algorithm for record positions
within a random access file makes locating an
individual record very fast.

Prepared By: Prof. Pooja Kadam

–if a second Smith is added to the file, this creates what ’
s called a hash collision and the hash algorithm has to
accommodate such things.
–Record updating is much faster with random access files
because you have to rewrite only the record that has
been changed.
–The bad news is that you must design the record size
in terms of a worst - case scenario. Even though you
know that most peoples ’ first names do not exceed 10
characters, you might have to design the number of
bytes devoted to the customer ’ s first name to be 15
bytes.
–Because of this worst - case design philosophy, random
access files are not dense like sequential files.

Prepared By: Prof. Pooja Kadam

Serialization and Deserialization
•serialization is the act of saving, or persisting, an
object ’ s state to disk.

•Deserialization is the act of reconstructing an
object ’ s state by reading the information stored
on disk back into an object of that class.

•Because the primitive data types ( byte, char, int,
long, double, string, and so on) are not serializable
by default, you must explicitly state that the object
can be serialized using the [Serializable] attribute
at the top of the class source file of the object to
be serialized.
Prepared By: Prof. Pooja Kadam

Prepared By: Prof. Pooja Kadam

•Filestream object ( myStream ) is used to open the
test file named Test.bin . If the file does not exist, it
is created automatically. (If you change
FileMode.Create to FileMode.Append , you can
append more than one object ’ s state to the file.)
•The program then uses the Serialize() method of
the BinaryFormatter class to store myFriend ’ s
state to disk.
•The Deserialize() method of the BinaryFormatter
class reads the data from the disk file and formats
into the clsSerial object.
•The explicit cast (clsSerial) is required because
Deserialize() returns a plain object type.
Prepared By: Prof. Pooja Kadam

To Serialize or Not to Serialize
•After all, the random access program is also saving the
state of an object to disk, but it requires quite a bit more
code to do so.
•First, the random access program is not specifically
designed to simply serialize an object. Rather, it ’ s
intended to serve as a transactions - based structure
where the object is permanently stored.
•Serialization of an object is more often used to
temporarily store the state of an object so it can
be restored later.
•you may not want to have all the information in a
class serialized. There could be sensitive
information in the class that you don ’ t want others
to see.

Prepared By: Prof. Pooja Kadam

•If you want to exclude a specific member of a class from
being serialized as part of the object ’ s state, you can use
the following statement in the class definition:
[NonSerialized] string cellPhone;
Prepared By: Prof. Pooja Kadam