CS 18000 Lecture 28: CS 180 - File IO

31 views5 pages
Generic File Operations (1)
1. Open:
a. Files must be opened before they can be used
b. Open method indicates “for reading”, “for writing”, or “both”
c. May also indicate “append” mode
d. Allows operating system to establish “buffers” and other state information about
the file being read or written
2. Read:
a. Transfers data from the file (or input stream) to the user process
b. Specific method signature indicate the type of data being transferred (byte, int,
String, Tree, etc.)
3. Write
a. Transfers data from the user process to the file
b. Specific method signatures indicate the type of data being transferred
Generic File Operations (2)
1. File position:
a. Sets the “current input position” to a specific byte address in the file
b. Can be used to skip over data in the file; or backup to read data again
c. Can be used to “rewind” the file to start reading from the beginning again
2. Close
a. Ensures that any “queued data” is “flushed” from the operating system buffers
b. Frees any operating system resources being dedicated to managing the file
Importance of Buffering
1. Without buffering, each read or write may generate physical disk access
2. Can be extremely slow for large volumes of data
3. Buffering has OS create internal array
a. OS reads “more than needed” on input, keeps rest for next call to read method
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in
b. OS doesn’t send output “right away” to disk drive, waits a while in case another
write comes along
c. Important to close file (or flush buffers) when done
File I/O layers in Java
1. Low-level
a. “Raw” data transfer: byte-oriented
b. Classes: FileOutputStream, FileInputStream
2. High-level
a. Java primitive types
b. Classes: DataOutputStream, DataInputStream
3. Object I/O
a. Java object types
b. Classes: ObjectOutputStream, ObjectInputStream
Tricky Bits
1. Must keep track of what you’re doing
2. Data values must be read in the same order in which they were written
a. Write int, long, long, boolean, double, float, char
b. Read int, long, long, boolea, double, float, char
3. If you try to read an int, but a double is next in the stream, PFFFT
Object I/O: Notes
1. Fields declared static or transient aren’t written out
a. Static: logical, since doesn’t apply to specific object
b. Transient: Useful for fields that need to be regenerated anyways
2. Graphs of objects can be written
a. Writing one object also writes all serializable object connected to it
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in

Document Summary

String, tree, etc. : write, transfers data from the user process to the file, specific method signatures indicate the type of data being transferred. Important to close file (or flush buffers) when done c. File i/o layers in java: low-level, raw data transfer: byte-oriented, classes: fileoutputstream, fileinputstream, high-level. Java primitive types a: classes: dataoutputstream, datainputstream, object i/o. Java object types a: classes: objectoutputstream, objectinputstream. If you try to read an int, but a double is next in the stream, pffft. Writing and reading text: java handles translation from internal primitive format to human-readable text, writing, class: printwriter (favored, more platform independent, class: printstream for system. out. (but out of favor, reading, classes: filereader and bufferedreader and scanner. Identifies hosts (servers, laptops, etc. ) with a unique address: domain name system (dns), maps domain names to ip address, transmission control protocol (tcp): a. Identifies ports on hosts for a network connection. 1: socket: ip address plus tcp port, think: street name plus house number.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents