Input and Output Streams
The abstract classes InputStream and OutputStream contain
the basic I/O interface.
Note: Use the ones from java.io, not Corba.
These have abstract methods to read and write a single byte and implemented
methods to close and handle arrays of bytes.
Look at the Java documentation for these.
There are lots of extension to each of these.
Here are the tables from the Core Java book.
This does not even count the Reader and Writer classes.
Files
The simplest ways to get these streams from file is with the FileInputStream and FileOutputStream.
First create a File.
The constructor takes a String parameter.
File f = new File(filename);
If a file exists, you can find its size with the length method.
Then create a stream:
FileInputStream fis = new FileInputStream(f);
FileOutputStream fis = new FileOutputStream(f);
Note that write will write all you ask, but read may not read all you ask it to.
If you have a DataInputStream you can use readFully(byte[] b).
General way of figuring out which type of stream to use:
First determine the functionality you need.
Exceptions
Most of the I/O methods and some constructors can throw an exception.
Handle exceptions with try ... catch ... finally.
It is important to close all streams when you are done.
This can be tricky when an error occurs, especially since close can
also throw an error.
Note that the finally clause is executed whether an exception occurs
or not.
Look at the code for readEntireFile.
Network communication
We will look only at connection-oriented communication.
Server: waits for a connection from a remote client using a given port