CS51 - Spring 2010 - Lecture 23
exercises 19.7.2, 19.7.3
administrative
- TAs for next year
- CS lunch tomorrow
Review of Readers and Writers
- Input
- FileReader
- BufferedReader
- Output
- FileWriter
- PrintWriter
- Readers and writers deal with characters
A simple I/O application
// reads in first and last names
// the first row in the returned array is an array of first names
// the second row in the returned array is an array of last names
// NUM_NAMES has the number of names
String[][] readNames(String filename)
// prints out all the names to filename with the characters reversed in each name
void lastReversed(String filename, String[] last)
// prints out all the names to filename with a space in between each character
// e.g. "dave" -> "d a v e" (or "d a v e " if that makes life easier)
void spacedFirstNames(String filename, String[] first)
// prints out the names to a file in the form of "last, first"
void reversedNames(String filename, String[] first, String[] last)
Streams
- A stream is a sequence of data
- Streams deal with bytes (instead of characters)
- What is a byte?
- 8 bits
- What is a bit?
- a 0 or a 1
- A byte is a common unit of storage in computer (kilobyte (KB), megabyte (MB), ...)
- Streams are a way for a program in java to communicate to external components (i.e. outside of processor/memory)
- What external components might we want to talk to?
- files (disk drives)
- internet/network (i.e. other computers)
- memory
- devices (e.g. cdrom, disk drive, ipod)
- Streams are directional
- input streams bring data into the program (ancestor of class InputStream)
- output streams send data out from the program (ancestor of class OutputStream)
- Like with Readers and Writers, we can read/write bytes/data from files:
- FileInputStream(String filename)
- FileOutputStream(String filename)
- and like FileReader and FileWriter, we generally won't use this class by itself.
- the key methods read/write a byte at a time!
- instead, we will use these as the base, to build up other Stream handlers that can process larger entities
- Different types of streams depending on the data that you want to read or write. What type of data might we want to read or write?
- built in data types
- DataInputStream(InputStream in)
- readInt()
- readDouble()
- ...
- DataOutputStream(OutputStream out)
- writeInt(int i)
- writeDouble(double d)
- ...
- Objects
- ObjectInputStream(InputStream in)
- ObjectOutputStream(OutputStream out)
- FileInputStream and FileOutputStream vs. FileReader and FileWriter
- FileReader and FileWriter can be thought of as character streams
- what is the difference between using a DataOutputStream to print an int vs. a PrintWriter
- DataOutputStream will send the bytes representing the int
- PrintWriter will send the data as characters
- Buffered streams
- Most streams are unbuffered, that is when you read or write a character, it reads or writes a character
- What's the problems with this?
- can be very inefficient, e.g.
- To alleviate this, we can do buffered I/O
- BufferedInputStream(InputStream in)
- BufferdOutputStream(OutputStream out)
- Note also: InputStreamReader and OutputStreamReader allow us to go from our Reader/Writer classes to Streams
show
DrawingPanelNetwork demo
- Remember our
DrawingPanel demo
...
- How were we storing/keeping track of these objects?
- DrawableInterface[]
- we've added some new ones here, like Line and Text, but the basic idea is the same
- save and load file functionality
- save some file and look at the data as a text file
- can see some data we might, but it is a "binary" file... it has bytes written representing the data
- can also transmit data over the network
- What is a server and a client?
- servers and clients communicate over the network
- the main difference is that servers are started first and wait for clients to collect
- also, there is usually a many to one relationship, with many clients for a single client, for example:
- web (http) servers
- mail (smtp) servers
- ftp servers
- file servers (e.g. nfs)
Map out the key functionalities
- write objects to a stream (writeToStream)
- take as parameter an OutputStream!
- read objects from a stream (readFromStream)
- take as parameter and InputStream!
- save a file
- create a new FileOutputStream
- call writeToStream
- load a file
- create a new FileInputStream
- call readFromStream
- send a file
- wait for the client to connect
- when it does, open an OutputStream to the client
- call writeToStream
- receive a file
- connect to the server
- open an InputStream to read from the server
- call readFromStream
look at
DrawingPanelNetwork code
- top code is basically the
DrawingPanelWithShapeArray code
- To read and write this data, what type of Stream will we need?
- ObjectInputStream/ObjectOutputStream (we're sending Objects!)
- writeToStream
- Takes as input an OutputStream (similar to what we did for SantasList)
- creates a new ObjectOutputStream
- what do you think the flush() method does?
- don't actually need it here since close() will flush the data
- don't forget to close()
- readFromStream
- Similarly, takes as input an InputStream
- creates a new ObjectInputStream
- note that we need to type cast it, why?
- Why do you think we wrote these methods to take as input a Stream?
- we can use the same methods to do file I/O as well as network I/O
- saveFile
- get a filename from the user using JFileChooser
- create a FileInputStream using the file selected by the user
- use our readFromStream method
- loadFIle
- again, get the filename
- create a FileOutputStream
- use our writeToStream method