CS111: PROGRAMMING LANGUAGE II...7 Reading text files dr. Amal Khalifa, 2014 8 Reading text files...

Post on 01-Jan-2021

0 views 0 download

Transcript of CS111: PROGRAMMING LANGUAGE II...7 Reading text files dr. Amal Khalifa, 2014 8 Reading text files...

CS111: PROGRAMMING

LANGUAGE II

Lecture 13(b): Stream & Files Computer Science

Department

Lecture Contents

dr. Amal Khalifa, 2014

Reading text files

Scanner class

Sequential Access files:

problems

Methods

objects

2

How to store this data in a file?

Case study 3

dr. Amal Khalifa, 2014

The Student class

dr. Amal Khalifa, 2014

4

Data

Constructor

set methods

The Student class (cont.)

dr. Amal Khalifa, 2014

5

get methods

And…

toString

dr. Amal Khalifa, 2014 6

Reading text files

read data

sequentially

from a text

file using a

Scanner.

dr. Amal Khalifa, 2014

7

Reading text files

dr. Amal Khalifa, 2014

8

Reading text files

dr. Amal Khalifa, 2014

9

File processing

dr. Amal Khalifa, 2014

10

Open Read Close

Main!!

dr. Amal Khalifa, 2014

11

Run!!

dr. Amal Khalifa, 2014

12

Sequential Access!!

dr. Amal Khalifa, 2014

13

It might be necessary to process the file sequentially

several times (from the beginning of the file) during

the execution of a program.

Class Scanner does not provide the ability to

reposition to the beginning of the file.

If it is necessary to read the file again, the

program must close the file and reopen it.

Updating Sequential-Access Files

dr. Amal Khalifa, 2014

14

The data in many sequential files cannot be modified without the

risk of destroying other data in the file.

If the name “White” needed to be changed to “Worthington,”

the old name cannot simply be overwritten, because the new name

requires more space.

Fields in a text file—and hence records—can vary in size.

Records in a sequential-access file are not usually updated in place.

Instead, the entire file is usually rewritten.

Rewriting the entire file is uneconomical to update just one record,

but reasonable if a substantial number of records need to be

updated.

Refer to the book fig. 17.20 & 17.21 for more information!!

JFileChooser 15

Working on chapter 17…

That’s all for today…..

dr. Amal Khalifa, 2014

16