0% found this document useful (0 votes)
20 views

CS2 Week4

Uploaded by

Dinal Savaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

CS2 Week4

Uploaded by

Dinal Savaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Input and Output Streams

(Byte) Streams
– Represented in memory as a sequence of bytes (low
level file); no “real” notion of type of data represented
Computer Science II – Used to store data read from or to be written to system
devices (e.g. physical file on disc, monitor, keyboard,
4003-232-08 (Winter 2007-2008) pen/stylus input)
– In Java: represented and utilized through objects (most
found in java.io package)
Week 4: Files Continued
Default Streams for Programs in Most OS’s
Richard Zanibbi Standard input (System.in) – usually from keyboard.
Rochester Institute of Technology Standard output (System.out) – usually sent to terminal.
Standard error (System.err) – usually sent to terminal also

-2-

e.g. Scanner or objects used to abstract


FileReader or
FileInputStream or
requests for OS operations
that manipulate streams
“Low Level Files” = Byte Stream Files
DataInputStream

(file in memory) Name: Test (ASCII)


(Java)
Byte Value
File Pointer
On file open:
0 0100 0001 A
(default) pointer set to 0 1 0100 0010 B
Read/Write K bytes: 2 0100 0011 C
Advance pointer by K
3 0100 0100 D
Setting File Pointer:
(file in memory)
e.g. PrintWriter or skip(),mark(),reset()(Java) 4 0100 0101 E
FileWriter or lseek() (POSIX)
FileOutputStream or SetFilePointer() (Win) 5 0100 0110 F
DataOutputStream

-3- No “Type” for Bytes: 6 0100 0111 G -4-


Treated as “raw” bytes

Java Byte Stream Classes


Text vs. Binary I/O
(Binary I/O)
(Conversion to ASCII)
See Fig 18.3 (p. 608) for inheritance
hierarchy
main (abstract) classes:
(No Conversion)
InputStream, OutputStream
(summary: pp. 608-609)
0x : prefix for hexadecimal
values (base 16) ** All methods declared to throw java.io.IOException (IOException)
(4 bits per symbol) or one of its subclasses (e.g. FileNotFoundException)
-5- -6-

1
FileInputStream, FileOutputStream ‘FileStream’ Example
FileInputStream (see Fig. 18.6): TestFileStream.java (p. 610 in text)
Produces an input stream from a file on disk. Constructors
take a File object or a string giving the path to the file. Unix: to view file contents, can use od
(octal dump)
e.g. od –Ad –tx1 temp.dat
FileOutputStream (see Fig. 18.7): • Shows file contents, one byte at a time in hexadecimal (with
decimal numbering for bytes in the file)
Produce output stream (file) that will be stored on disk.
Constructors also take a File object or path string, but od –Ad –a temp.dat
• Shows file contents, interpreted as (named) characters (ASCII)
have an alternate form that takes a boolean flag indicating
whether to overwrite (‘delete’) or append data to the end of od –Ad –t x1 –a –c temp.dat
• Show file contents in hexadecimal, as named characters, and as
the file. ASCII characters (including escape sequences)
-7- -8-

“Filtered” Streams
‘DataStream’ Example
(FilterInputStream, FilterOutputStream)
Purpose TestDataStream.java (p. 613, text)
Converting bytes to other data types in java (for use with streams)
– Syntax: done through “wrapping” another class around an existing
stream

FilterInputStream, FilterOutputStream
– Used to convert primitive types and Strings to and from bytes
– Subclasses of interest: DataInputStream (see Fig 18.9),
DataOutputStream (see Fig 18.10)
• These classes write Java variables directly to a byte stream (quietly
making the necessary conversion), or convert byte stream data to Java
variable primitive (e.g. int, double) or String types (e.g. to store in a
variable).
– Methods for the conversions are defined by the DataInput interface.

-9- - 10 -

Buffered Streams in Java


(also Filtered Streams) Example of Buffering
Buffering
– Using additional intermediate storage (a ‘buffer’) to allow Copy.java (p. 615)
reading ahead and writing behind
– Reduces the number of (actual) read and write operations
needed, improves performance
– Default buffer size is 512 bytes (can be changed using a
constructor)

Java Syntax (Example; mod. TestDataStream.java)


DataOutputStream output = new DataOutputStream(new
BufferedOutputStream(new FileOutputStream(“temp.dat”)));
DataInputStream input = new DataInputStream(new
BufferedInputStream(new FileInputStream(“temp.dat”)));
- 11 - - 12 -

2
Reader and Writer Classes (abstract):
Standard Streams in Java Alternate Classes to Read, Write Text Files in the
java.io Package (see Java API)
System.out PrintWriter
Is a subclass of Writer
Is a PrintStream reference (subclass of Has same interface as PrintStream (e.g. as used by System.out)
FilterOutputStream) Can be used to write to a

FileReader
System.err Is a subclass of Reader used to read text files
Also a PrintStream reference
BufferedFileReader
– Subclass of Reader
System.in – Use a buffer for read/write operations for concrete Reader instances
(e.g. FileReader)
Is an InputStream reference • BufferedFileReader in = new BufferedReader(new FileReader(“foo.in”))

- 13 - - 14 -

Exercise: Files Part B


1. What is stored in a stream? Where do
Part A
streams exist?
1. Can the File class be used for I/O? If not, what is it
used for? 2. What three streams exist for all programs in a
2. What is the (parent) type of checked exception modern OS?
thrown by classes in the java.io package?
3. How must this be handled within a method using
3. What type of data do FileInputStream and
these operations? (Hint: two possibilities) FileOutputStream support?
4. Write java statements to create a PrintWriter object 4. What types of data do DataInputStream and
for a file “foo.txt” in the current directory, write “hello” DataOutputStream support?
to the file, and then close the file.
5. What sources can a Scanner object read data from? 5. What is buffering? Why is it used?
How does this differ for a FileInputStream? 6. Write Java statements to construct a buffered
6. What kind of data ‘unit’ are Scanners normally used FileInputStream for “foo.txt” (current dir.), read
to recover?
a byte, and close the stream.
- 15 - - 16 -

You might also like