cis 1068 program design and abstraction
DESCRIPTION
CIS 1068 Program Design and Abstraction. Zhen Jiang CIS Dept. Temple University 1050 Wachman Hall, Main Campus Email: [email protected]. Table of Contents. Introduction to data storage File Trial of storing data to a text file Exceptions Reading data from a text file - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/1.jpg)
CIS 1068 Program Design and Abstraction
Zhen Jiang
CIS Dept.
Temple University
1050 Wachman Hall, Main Campus
Email: [email protected]/19/23
![Page 2: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/2.jpg)
Table of Contents Introduction to data storage File Trial of storing data to a text file Exceptions Reading data from a text file Appending to a text file Reading a file name from keyboard Reading a file name from GUI Binary file (related work) Summary and acknowledgement
204/19/23
![Page 3: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/3.jpg)
3
Storage
Modern computers have many different kinds of storage components:- memory (aka RAM or DRAM)- disks (aka “hard disks” or “hard drives”)- caches- external media (USB sticks, DVD, CD-RW, etc.)
Why so many?Because they behave differently!
04/19/23
![Page 4: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/4.jpg)
4
Tradeoff: Disks have greater capacity (more GB) and offer permanent
storage;Memory is much faster.
Typical Properties Memory Disk
Capacity: 1-4 Gb >100 Gb
When power goes off:
Data is lost Data is safe
When program ends:
Data is lost Data is safe
Sequential access speed:
1.2 GB / second 50 MB / second
Random access speed:
1.2 GB / second??
~ 66.7 seeks / second
04/19/23
![Page 5: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/5.jpg)
File Files are similar abstractions, but
for the disk: They have names and a location on the disk You can put (lots of) data inside them You can later retrieve that data and use it
04/19/23 5
![Page 6: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/6.jpg)
Read Move data from a file on the disk into a
variable (or variables) in memory Write
Move data from a variable (or variables) in memory to a file on the disk
Two tricky details to these operations – Data types Checked Exceptions
04/19/23 6
![Page 7: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/7.jpg)
7
Storing data to text files
Creating a stream variable for a file, general syntax:PrintWriter <name> = new PrintWriter (<file name>);
The PrintWriter class is in the java.io package. To use it, include the import declaration:import java.io.*;
This PrintWriter object let you print data to a destination file in the current folder (directory).
04/19/23
![Page 8: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/8.jpg)
http://www.cis.temple.edu/~jiang/TextFileOutputTry.pdf
04/19/23 8
![Page 9: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/9.jpg)
04/19/23 9
The program does not compile:
The compiler reports:TextFileOutputTry.java:11: error: unreported exception
FileNotFoundException; must be caught or declared to be thrown
PrintWriter outputStream = new PrintWriter(fileName); ^
1 error
![Page 10: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/10.jpg)
10
Exceptions
exception: An object representing a program error. Programs with invalid logic will cause ("throw")
exceptions.
Examples: Trying to read a file that does not exist. Dividing by 0. Using charAt(10) on a string of length 5.
04/19/23
![Page 11: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/11.jpg)
11
checked exception: An exception that must be explicitly handled (otherwise the program will not compile). We must either:
handle ("catch") the exception, or explicitly state that we choose not to handle
the exception (and accept that the program will crash if the exception occurs)
04/19/23
![Page 12: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/12.jpg)
12
throws clause: Tells the compiler that a method may throw an exception.
Like a waiver of liability:"I hereby agree that this method might throw an exception, and I accept the consequences (crashing) if this happens."
throws clause, general syntax:public static <type> <name>(<params>) throws
<type> {
Example:public static void main(String[] args) throws FileNotFoundException {
04/19/23
![Page 13: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/13.jpg)
http://www.cis.temple.edu/~jiang/TextFileOutputDemo.pdf
Page 732
04/19/23 13
![Page 14: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/14.jpg)
Your program should close what it has opened! Use the close() method to close a file after your
done with it. Example:
PrintWriter out = new PrintWriter(“output.txt”);
out.println(“Hello, output file!”);
out.close();
Also works on Scanner objects close() releases system resources associated
with an open file.
04/19/23 14
![Page 15: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/15.jpg)
15
Reading data from text files
Creating a Scanner for a file, general syntax:Scanner <name> = new Scanner(new File("<file
name>"));
Example:Scanner input = new Scanner(new File("numbers.txt"));
Instead of getting data from the keyboard via System.in, this Scanner object gets data from the file numbers.txt in the current folder (directory).04/19/23
![Page 16: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/16.jpg)
16
The preceding program is assumes you know how many values you want to read.
How could we read in ALL of the numbers in the file, without knowing beforehand how many the file contains?
04/19/23
![Page 17: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/17.jpg)
17
The Scanner has useful methods for testing to see what the next input token will be, see P740.
whether the next token can be interpreted as type int
hasNextInt()
whether any more lines remainhasNextLine()
whether the next token can be interpreted as type double
hasNextDouble()
whether any more tokens remainhasNext()
DescriptionMethod Name
04/19/23
![Page 18: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/18.jpg)
Figure 2.7, p98
![Page 19: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/19.jpg)
![Page 20: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/20.jpg)
20
Each call to next, nextInt, nextDouble, etc. advances the cursor to the end of the current token, skipping over any whitespace. Each call consumes the input.
input.nextDouble();
308.2\n 14.9 7.4 2.8\n\n\n3.9 4.7 -15.4\n2.8\n
^
input.nextDouble();
308.2\n 14.9 7.4 2.8\n\n\n3.9 4.7 -15.4\n2.8\n
^04/19/23
![Page 21: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/21.jpg)
File: sequential data storage http://www.cis.temple.edu/~jiang/
TextFileInputDemo.pdf Page 738
04/19/23 21
![Page 22: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/22.jpg)
Appending to a text file
PrintWriter outputStream = new PrintWriter (new FileOutputStream (<file name>, true));
For examplePrintWriter outputStream = new PrintWriter (new FileOutputStream (“out.txt”, true));
04/19/23 22
![Page 23: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/23.jpg)
Reading a file name from keyboard http://www.cis.temple.edu/~jiang/
TextFileInputDemo2.pdf Page 742. Using path names, p743.
New File (“c:\\homework\\hw1\\data.txt”)
04/19/23 23
![Page 24: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/24.jpg)
Reading a file name from GUI http://www.cis.temple.edu/~jiang/
TextFileInputDemo3.pdf fileChooser import
04/19/23 24
![Page 25: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/25.jpg)
Binary file (related materials) Images, videos, mp3s, oh my! Let’s say we wanted to write a
program that makes a copy of an mp3 –
Q: What’s wrong with using Scanner and PrintWriter?
A: If the file contains no `\n’ characters, the nextLine() method will try to read the entire file into memory all at once!
04/19/23 25
![Page 26: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/26.jpg)
FileInputStream: a class in the java.io package that lets you read binary data in chunks whose size you specify.
Setting up an input file, general syntax:FileInputStream <name> =
new FileInputStream(new File("<file name>"));
Example:FileInputStream input = new FileInputStream(new
File(“input.mp3"));
int nextByte = input.read();
byte [] buffer = new byte[100];
int numBytesRead = input.read(buffer);
04/19/23 26
![Page 27: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/27.jpg)
PrintStream: Another class in the java.io package that lets you print output to a destination such as a file.
Writer: text output, Stream: binary output
System.out is a PrintStream object! Any methods you have used on System.out
(such as print, println) will work on every PrintStream object.
04/19/23 27
![Page 28: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/28.jpg)
http://www.cis.temple.edu/~jiang/BinaryInputDemo.pdf
http://www.cis.temple.edu/~jiang/BinaryOutputDemo.pdf
04/19/23 28
![Page 29: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/29.jpg)
Writing, p732 Reading, p94 and p738 Appending text to a file, p736 Reading the file name, p742 – 743, fileChooser
Summary of Concepts
2904/19/23
![Page 30: CIS 1068 Program Design and Abstraction](https://reader035.vdocuments.mx/reader035/viewer/2022062408/56813047550346895d95efc4/html5/thumbnails/30.jpg)
Acknowledgement
3004/19/23