CIS 270—Application Development II Chapter 14—Files and Streams.

10
CIS 270—Application Development II Chapter 14—Files and Streams

Transcript of CIS 270—Application Development II Chapter 14—Files and Streams.

Page 1: CIS 270—Application Development II Chapter 14—Files and Streams.

CIS 270—Application Development II

Chapter 14—Files and Streams

Page 2: CIS 270—Application Development II Chapter 14—Files and Streams.

2

14.1 Introduction Data maintained in files are called ___________

and are stored on secondary storage devices. A _________ is ordered data read from or

written to a file. Three forms of file processing with Java

text file (storing text) object serialization (storing objects) random-access (for use with databases)

Page 3: CIS 270—Application Development II Chapter 14—Files and Streams.

3

14.2 Data Hierarchy A single electronic circuit can store a bit (_______

digit) with possible values of 0 or 1 (off or on). Humans use decimal digits, letters, and other symbols.

Data hierarchy Characters in Java are in __________ that uses two bytes

one byte is composed of eight bits ‘A’ = hex 0041 = ASCII 00001011 = Unicode 00000000

00001011 A meaningful group of characters forms a ________. A meaningful group of fields forms a record. A group of related records forms a file.

One or more fields in a record can form a primary key that uniquely identifies a record (e.g. a social security number).

A sequential data file stores records in primary key order. A group of related files forms a ____________.

Page 4: CIS 270—Application Development II Chapter 14—Files and Streams.

4

14.3 Files and Streams 1 Java views a file as a ____________ stream of bytes. An operating system uses an end-of-file ________. A stream can perform ____ in bytes (a byte-based

stream) or characters (a character-based stream). Files created using byte-based streams are called

________ files (which must be converted to text). Files created using character-based streams are

called _____ files (which can be read by text editors).

Three Java stream objects that can be redirected. System.in (standard input stream from the keyboard) System.out (standard output stream to the screen) System.err (standard error stream to the screen)

Page 5: CIS 270—Application Development II Chapter 14—Files and Streams.

5

14.3 Files and Streams 2 File processing classes are in the package _________: FileInputStream and FileOutputStream for ______-

based file I/O FileReader and FileWriter for character-based file

I/O Object I/O is handled by ObjectInputStream

and ObjectOutputStream. Class File provides information about files and

___________. Classes Scanner and Formatter can also

perform character-based input and output, respectively.

Page 6: CIS 270—Application Development II Chapter 14—Files and Streams.

6

14.4 Class File An absolute path is the location of a file or

directory starting with the ______ directory. A _________ path is the location of a file or directory

starting where the application began executing. A URI (Uniform Resource Identifier) can be used to

locate a file, such as with file:/C:/data.txt in Windows. A URL is used to locate web sites, http://www.sun.com.

A ___________ character separates directories and files in a path \ for Windows, / for UNIX—Java interprets each the same

Page 7: CIS 270—Application Development II Chapter 14—Files and Streams.

7

14.5 Sequential-Access Files

Java imposes no structure on a file (such as records). This must be done by the application.

Trying to write to a non-existent file will create the file.

Trying to write to an existing file will _________ the existing file (existing data will be discarded).

By default, all data files are assumed to be in the same directory as the application files.

See Figs. 14.6-14.7 for a writing example. See Figs. 14.11-14.12 for a reading example.

Page 8: CIS 270—Application Development II Chapter 14—Files and Streams.

8

14.6 Object Serialization Data written to a text files loses information

about their ______ (int, String, double, etc.) and the type of object containing these data.

A serialized object is a sequence of bytes that includes this information.

These data can be read from a file and ___________ into an object in memory.

Classes ObjectInputStream and ObjectOutputStream implement the interfaces ObjectInput and ObjectOutput and use file stream classes FileInputStream and FileOutputStream.

Page 9: CIS 270—Application Development II Chapter 14—Files and Streams.

9

14.6.1 Reading / Writing Objects from / to a File

An object-defining class (ODC) must implement Serializable to serialize and deserialize objects.

All _________ variables of the ODC must be serializable (primitives and arrays are by default).

________ occurs when an object-stream object uses a file-stream object to read/write objects to a file.

See Figs. 14.17-14.19 for an example of writing objects to a file.

See Figs. 14.20-14.21 for an example of reading objects from a file.

Page 10: CIS 270—Application Development II Chapter 14—Files and Streams.

10

14.7 Random-Access Files Sequential-access files are suited for _______

applications. Instant-access applications, such as

transaction processing systems, need rapid access to data. These applications need random-access files. Random-access files are also called _______-access

files. See program examples in text.