CS51 - Spring 2010 - Lecture 24
- TAs for next year
- sr. presentations today and tomorrow
- pre-registration pizza
Review of Readers and Writers
- 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 returend 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)
- 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)
- 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)
- 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)
- DataOutputStream(OutputStream out)
- writeInt(int i)
- writeDouble(double d)
- 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
- 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)
- top code is basically the
- To read and write this data, what type of Stream will we need?
- 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()
- 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
- get a filename from the user using JFileChooser
- create a FileInputStream using the file selected by the user
- use our readFromStream method
- again, get the filename
- create a FileOutputStream
- use our writeToStream method
- a socket is a framework for bidirectional communication
- sockets are specified by an IP address and a port
- every computer has an IP address, think of it like a phone number
- consists of 4 numbers 0 to 255 xxx.xxx.xxx.xxx
- or, you can think of it as 4 bytes
- think of the port, like an extension, it uniquely identifies what program/process on a computer another program is talking to
- Looking up ip addresses
- "nslookup <domain_name>"
- nslookup www.cs.pomona.edu
- the server only need to specify a port, which is the port it is listening on
- "localhost", is a keyword for specifying the current computer
- ServerSocket is used to wait for a connection from a client
- note that we only need to specify the port
- another blocking operation
- waits for the client to connect and gives us the socket for the connection
- uses writeToStream to transmit the data
- creates a new socket using the ip and the port specified
- can leverage readToStream