![]() |
![]() Lecture Notes Eight: Basic Java Networking
|
Before I forget here's the URL for the JMF2.1 main page. Now let's get started.
So far we know how to generate data for the user, graphically or
textually. We are also able to get input from the user from the
command line, as a sequence of
String
s, or from the
mouse in a graphical, or a graphical user interface, context.
Now we want to be able to read text from a file, and from the terminal, and also to write text to a file.
If you want to be able to make phone calls you need to buy a telephone and use it according to the manufacturer's instructions. Same thing with I/O in Java: we just need to know what type of objects we need to work with (and how) in order to read from or write to a file, and read from the terminal.
FileInputStream
InputStreamReader
BufferedReader
StreamTokenizer
System.out
System.in
FileOutputStream
PrintWriter
1. FileInputStream
Find the documentation for it here.
So now you know how you can get a connection, and what happens if you're asking for an inconsistent or incorrect kind of connection. Let's now read the character one by one and put parens around them when we print them back to the user to see if we can get inside the contents of the file.oldschool.cs.indiana.edu%vi Example.java oldschool.cs.indiana.edu%vi file oldschool.cs.indiana.edu%ls -l total 2 -rw------- 1 dgerman 289 Jul 19 12:49 Example.java -rw------- 1 dgerman 59 Jul 19 12:45 file oldschool.cs.indiana.edu%cat file This is a sample file that has three lines of text in it. oldschool.cs.indiana.edu%cat Example.java import java.io.*; class Example { public static void main(String[] args) { try { FileInputStream stream = new FileInputStream(args[0]); stream.close(); } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } } } oldschool.cs.indiana.edu%javac Example.java oldschool.cs.indiana.edu%ls -l total 3 -rw------- 1 dgerman 793 Jul 19 12:50 Example.class -rw------- 1 dgerman 289 Jul 19 12:50 Example.java -rw------- 1 dgerman 59 Jul 19 12:50 file oldschool.cs.indiana.edu%java Example file oldschool.cs.indiana.edu%java Example A.java Something went wrong: java.io.FileNotFoundException: A.java oldschool.cs.indiana.edu%java Example Something went wrong: java.lang.ArrayIndexOutOfBoundsException: 0 oldschool.cs.indiana.edu%
2. InputStreamReader
Find documentation for this kind of objects here.
So you see how you can get to the characters, by first getting their integer codes and then converting them intooldschool.cs.indiana.edu%vi Example.java oldschool.cs.indiana.edu%cat Example.java import java.io.*; class Example { public static void main(String[] args) { try { FileInputStream stream = new FileInputStream(args[0]); InputStreamReader reader = new InputStreamReader(stream); int data = reader.read(); while (data != -1) { System.out.print("(" + data + ")"); data = reader.read(); } stream.close(); } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } } } oldschool.cs.indiana.edu%javac Example.java oldschool.cs.indiana.edu%java Example file (84)(104)(105)(115)(32)(105)(115)(32)(97)(32)(115)(97)(109)(112)(108)(101)(10\ )(102)(105)(108)(101)(32)(116)(104)(97)(116)(32)(104)(97)(115)(32)(116)(104)(\ 114)(101)(101)(10)(108)(105)(110)(101)(115)(32)(111)(102)(32)(116)(101)(120)(\ 116)(32)(105)(110)(32)(105)(116)(46)(32)(10) oldschool.cs.indiana.edu%vi Example.java oldschool.cs.indiana.edu%cat Example.java import java.io.*; class Example { public static void main(String[] args) { try { FileInputStream stream = new FileInputStream(args[0]); InputStreamReader reader = new InputStreamReader(stream); int data = reader.read(); while (data != -1) { System.out.print("(" + (char)data + ")"); data = reader.read(); } stream.close(); } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } } } oldschool.cs.indiana.edu%javac Example.java oldschool.cs.indiana.edu%java Example file (T)(h)(i)(s)( )(i)(s)( )(a)( )(s)(a)(m)(p)(l)(e)( )(f)(i)(l)(e)( )(t)(h)(a)(t)( )(h)(a)(s)( )(t)(h)(r)(e)(e)( )(l)(i)(n)(e)(s)( )(o)(f)( )(t)(e)(x)(t)( )(i)(n)( )(i)(t)(.)( )( )oldschool.cs.indiana.edu%
char
data. Now let's see how
we can get a whole line of characters at a time.
3. BufferedReader
Find documentation about this kind of objects here.
This was easy and you knew all about it already.oldschool.cs.indiana.edu%ls -l total 2 -rw------- 1 dgerman 408 Jul 19 13:12 Example.java -rw------- 1 dgerman 59 Jul 19 12:50 file oldschool.cs.indiana.edu%vi Example.java oldschool.cs.indiana.edu%cat Example.java import java.io.*; class Example { public static void main(String[] args) { try { FileInputStream stream = new FileInputStream(args[0]); InputStreamReader reader = new InputStreamReader(stream); BufferedReader b = new BufferedReader(reader); String line = b.readLine(); while (line != null) { System.out.println("(" + line + ")"); line = b.readLine(); } stream.close(); } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } } } oldschool.cs.indiana.edu%javac Example.java oldschool.cs.indiana.edu%java Example file (This is a sample) (file that has three) (lines of text in it. ) oldschool.cs.indiana.edu%
4. StreamTokenizer
Find documentation about these kind of objects here.
oldschool.cs.indiana.edu%vi Example.java oldschool.cs.indiana.edu%ls -l total 2 -rw------- 1 dgerman 763 Jul 19 13:33 Example.java -rw------- 1 dgerman 59 Jul 19 12:50 file oldschool.cs.indiana.edu%cat Example.java import java.io.*; class Example { public static void main(String[] args) { try { FileInputStream stream = new FileInputStream(args[0]); InputStreamReader reader = new InputStreamReader(stream); BufferedReader b = new BufferedReader(reader); StreamTokenizer s = new StreamTokenizer(b); s.nextToken(); while (s.ttype != StreamTokenizer.TT_EOF) { if (s.ttype == StreamTokenizer.TT_WORD) System.out.println("(" + s.sval + ")"); else if (s.ttype == StreamTokenizer.TT_NUMBER) System.out.println("(" + s.nval + ")"); s.nextToken(); } stream.close(); } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } } } oldschool.cs.indiana.edu%javac Example.java oldschool.cs.indiana.edu%java Example file (This) (is) (a) (sample) (file) (that) (has) (three) (lines) (of) (text) (in) (it.) oldschool.cs.indiana.edu%
StringTokenizer
on lines returned from a BufferedReader
to achieve same result.
System.out
You can read about this kind of objects here. You have used this kind of objects a lot.
6. System.in
You can read about this kind of objects here.
6.oldschool.cs.indiana.edu%vi Example.java oldschool.cs.indiana.edu%cat Example.java import java.io.*; class Example { public static void main(String[] args) { try { InputStreamReader reader = new InputStreamReader(System.in); BufferedReader b = new BufferedReader(reader); System.out.println("Welcome to the Echo program!"); System.out.print("Type: "); String line = b.readLine(); while (! line.equals("quit")) { System.out.println("You typed: " + line); System.out.print("Type: "); line = b.readLine(); } } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } finally { System.out.println("Good-bye."); } } } oldschool.cs.indiana.edu%javac Example.java oldschool.cs.indiana.edu%java Example Welcome to the Echo program! Type: Test You typed: Test Type: Hello! You typed: Hello! Type: I am talking to you. You typed: I am talking to you. Type: Hi there. You typed: Hi there. Type: quit Good-bye. oldschool.cs.indiana.edu%
FileOutputStream
You can read the documentation about this kind of objects here.
7.oldschool.cs.indiana.edu%ls -l total 2 -rw------- 1 dgerman 667 Jul 19 13:51 Example.java -rw------- 1 dgerman 59 Jul 19 12:50 file oldschool.cs.indiana.edu%vi Example.java oldschool.cs.indiana.edu%pico Example.java oldschool.cs.indiana.edu%vi Example.java oldschool.cs.indiana.edu%ls -l total 2 -rw------- 1 dgerman 350 Jul 19 14:00 Example.java -rw------- 1 dgerman 59 Jul 19 12:50 file oldschool.cs.indiana.edu%cat Example.java import java.io.*; class Example { public static void main(String[] args) { try { FileOutputStream stream = new FileOutputStream(args[0]); stream.close(); } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } finally { System.out.println("Good-bye."); } } } oldschool.cs.indiana.edu%javac Example.java oldschool.cs.indiana.edu%java Example Something went wrong: java.lang.ArrayIndexOutOfBoundsException: 0 Good-bye. oldschool.cs.indiana.edu%java Example file.out Good-bye. oldschool.cs.indiana.edu%ls -l total 3 -rw------- 1 dgerman 850 Jul 19 14:00 Example.class -rw------- 1 dgerman 350 Jul 19 14:00 Example.java -rw------- 1 dgerman 59 Jul 19 12:50 file -rw------- 1 dgerman 0 Jul 19 14:01 file.out oldschool.cs.indiana.edu%
PrintWriter
Read the documentation about this kind of objects here.
That's basically it, as far as text goes.oldschool.cs.indiana.eduls -l total 2 -rw------- 1 dgerman 701 Jul 19 14:09 Example.java -rw------- 1 dgerman 59 Jul 19 12:50 file oldschool.cs.indiana.edu%vi Example.java oldschool.cs.indiana.edu%cat Example.java import java.io.*; class Example { public static void main(String[] args) { try { FileOutputStream stream = new FileOutputStream(args[0]); PrintWriter p = new PrintWriter(stream, true); BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Hello, please type: "); String line = b.readLine(); while (! line.equals("quit")) { p.println(line); System.out.print("Thanks, please go on: "); line = b.readLine(); } stream.close(); } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } finally { System.out.println("Good-bye."); } } } oldschool.cs.indiana.edu%javac Example.java oldschool.cs.indiana.edu%java Example file.out Hello, please type: I am typing lines that Thanks, please go on: my program should write Thanks, please go on: to the file that I have Thanks, please go on: indicated on the command Thanks, please go on: line, that is: file.out Thanks, please go on: quit Good-bye. oldschool.cs.indiana.edu%ls -l total 5 -rw------- 1 dgerman 1362 Jul 19 14:09 Example.class -rw------- 1 dgerman 701 Jul 19 14:09 Example.java -rw------- 1 dgerman 59 Jul 19 12:50 file -rw------- 1 dgerman 120 Jul 19 14:10 file.out oldschool.cs.indiana.edu%cat file.out I am typing lines that my program should write to the file that I have indicated on the command line, that is: file.out oldschool.cs.indiana.edu
We don't know it yet, but we have almost everything worked out.
Our next stop: Socket
and ServerSocket
for client/server.
I am providing this as a separate document, that we may actually cover (finish covering) next time.
NC009