CS111: PROGRAMMING LANGUAGE II...7 Reading text files dr. Amal Khalifa, 2014 8 Reading text files...
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](https://reader035.fdocuments.net/reader035/viewer/2022071301/609ff2750a1c523e5d60d5dc/html5/thumbnails/1.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022071301/609ff2750a1c523e5d60d5dc/html5/thumbnails/2.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022071301/609ff2750a1c523e5d60d5dc/html5/thumbnails/3.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022071301/609ff2750a1c523e5d60d5dc/html5/thumbnails/4.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022071301/609ff2750a1c523e5d60d5dc/html5/thumbnails/5.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022071301/609ff2750a1c523e5d60d5dc/html5/thumbnails/6.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022071301/609ff2750a1c523e5d60d5dc/html5/thumbnails/7.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022071301/609ff2750a1c523e5d60d5dc/html5/thumbnails/8.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022071301/609ff2750a1c523e5d60d5dc/html5/thumbnails/9.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022071301/609ff2750a1c523e5d60d5dc/html5/thumbnails/10.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022071301/609ff2750a1c523e5d60d5dc/html5/thumbnails/11.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022071301/609ff2750a1c523e5d60d5dc/html5/thumbnails/12.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022071301/609ff2750a1c523e5d60d5dc/html5/thumbnails/13.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022071301/609ff2750a1c523e5d60d5dc/html5/thumbnails/14.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022071301/609ff2750a1c523e5d60d5dc/html5/thumbnails/15.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022071301/609ff2750a1c523e5d60d5dc/html5/thumbnails/16.jpg)
Working on chapter 17…
That’s all for today…..
dr. Amal Khalifa, 2014
16