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

16
CS111: PROGRAMMING LANGUAGE II Lecture 13(b): Stream & Files Computer Science Department

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

Page 1: CS111: PROGRAMMING LANGUAGE II...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

CS111: PROGRAMMING

LANGUAGE II

Lecture 13(b): Stream & Files Computer Science

Department

Page 2: CS111: PROGRAMMING LANGUAGE II...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

Lecture Contents

dr. Amal Khalifa, 2014

Reading text files

Scanner class

Sequential Access files:

problems

Methods

objects

2

Page 3: CS111: PROGRAMMING LANGUAGE II...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

How to store this data in a file?

Case study 3

dr. Amal Khalifa, 2014

Page 4: CS111: PROGRAMMING LANGUAGE II...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

The Student class

dr. Amal Khalifa, 2014

4

Data

Constructor

set methods

Page 5: CS111: PROGRAMMING LANGUAGE II...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

The Student class (cont.)

dr. Amal Khalifa, 2014

5

get methods

And…

toString

Page 6: CS111: PROGRAMMING LANGUAGE II...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

dr. Amal Khalifa, 2014 6

Page 7: CS111: PROGRAMMING LANGUAGE II...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

Reading text files

read data

sequentially

from a text

file using a

Scanner.

dr. Amal Khalifa, 2014

7

Page 8: CS111: PROGRAMMING LANGUAGE II...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

Reading text files

dr. Amal Khalifa, 2014

8

Page 9: CS111: PROGRAMMING LANGUAGE II...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

Reading text files

dr. Amal Khalifa, 2014

9

Page 10: CS111: PROGRAMMING LANGUAGE II...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

File processing

dr. Amal Khalifa, 2014

10

Open Read Close

Page 11: CS111: PROGRAMMING LANGUAGE II...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

Page 12: CS111: PROGRAMMING LANGUAGE II...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

Run!!

dr. Amal Khalifa, 2014

12

Page 13: CS111: PROGRAMMING LANGUAGE II...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

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.

Page 14: CS111: PROGRAMMING LANGUAGE II...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

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.

Page 15: CS111: PROGRAMMING LANGUAGE II...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

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

JFileChooser 15

Page 16: CS111: PROGRAMMING LANGUAGE II...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

Working on chapter 17…

That’s all for today…..

dr. Amal Khalifa, 2014

16