I am taking a distance learning class on JAVA. I am stuck with the
following. My code appears to work correclty, until it comes to the
DataInputStream. I am unable to figure out how to print the stream to my
console. I am reading a simple txt file of words (one per line). The Problem
could also be in the DataOutputStream, but I think I got that part correct.
Any help would be most appriciated.
import java.util.*;
import java.io.*;
public class PipeDemo{
public static void main(String args[]){
try{
PipedOutputStream out = new PipedOutputStream();
PipedInputStream in = new PipedInputStream(out);
Producer prod = new Producer(out);
Consumer cons = new Consumer(in);
prod.start();
cons.start();
}
catch (IOException e){System.out.println("Error: " + e);}
}
}
class Producer extends Thread{
private DataOutputStream out;
public Producer(OutputStream os){
out = new DataOutputStream(os);
}
public void run(){
try{
FileInputStream inputFile = new FileInputStream( "words.txt" );
InputStreamReader in = new InputStreamReader( inputFile );
BufferedReader br = new BufferedReader(in);
String input;
while ((input = br.readLine()) != null){
out.writeChars(input);
}
br.close();
}
catch(Exception e){System.out.println("Error: " + e);}
}
}
class Consumer extends Thread{
private DataInputStream in;
public Consumer(InputStream is)
{
in = new DataInputStream(is);
}
public void run(){
System.out.println(in.readLine);
}
}
Rhino - 04 Mar 2006 04:52 GMT
>I am taking a distance learning class on JAVA. I am stuck with the
>following. My code appears to work correclty, until it comes to the
[quoted text clipped - 90 lines]
>
> }
Have you looked at the I/O chapter in the Java Tutorial? It might answer
your questions. The URL is
http://java.sun.com/docs/books/tutorial/essential/io/index.html.
--
Rhino
Roedy Green - 04 Mar 2006 10:17 GMT
>The Problem
>could also be in the DataOutputStream, but I think I got that part correct.
>Any help would be most appriciated.
DataOutputStreams write binary, incomprehensible to humans, but
beloved of computers. If you want to make something for humans see
http://mindprod.com/applets/fileio.html
Tell it you want to write to the console and it will generate you
sample code.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.