Streams Cheat Sheet
FileReader reader = new FileReader("filename");
InputStreamReader reader = new InputStreamReader(System.in);
URL url = new URL("http://www.cs.williams.edu/~index.html");
InputStreamReader reader = new InputStreamReader(url.openStream());
InputStreamReader reader = new InputStreamReader(socket.getInputStream());
int ch = reader.read();
The variable ch is set to a character value between 0 and
255, or -1 when there are no more characters left in the stream (i.e.,
at the end of a file).
To read whole lines at a time, use a BufferedReader:
BufferedReader buffer = new BufferedReader(reader);
The reader variable is any reader made above. The following
example shows how to read all lines in a file using a buffered reader:
String s;
BufferedReader buffer = new BufferedReader(new FileReader("filename"));
s = buffer.readLine();
while (s != null) {
System.out.println(s);
s = buffer.readLine();
}
The method readLine() returns null when the reader
reaches the end of the stream.
reader.close();
PrintWriter writer = new PrintWriter(new FileWriter("filename"));
PrintWriter writer =
new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
For sockets, we need to use one extra parameter for the
PrintWriter constructor to indicate that Java should
treat the PrintWriter as something that writes to a
network.
writer.println("some text");
writer.print("some text");
Unlike println, the print method does not start a
new line after printing the text.
writer.close();