• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use PipedReader,pipeWriter, i has error output

 
eric lee
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why i can not output A,B,C? i got output "Error"
so how to change code? Thanks
-------------------------------------------------
import java.io.*;
class test {
public static void main(String args[])
{
PipedWriter out=new PipedWriter();
PipedReader input=new PipedReader();
char buffer[]={'A','B','C'};
int inChar;
try{
inChar=input.read(buffer);
while(inChar!=-1)
out.write(buffer,0,inChar);
}catch(IOException e){
System.out.println("Error");}
}
}
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of error did you get? Was it a compiler error? Was it a runtime error? What did the error message say?
 
eric lee
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code got to excpetion and print "Error"
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to Java IO too, but I see these things:
1) Your PipedReader and PipedWriter have to be connected.
2) You should probably be writing into your PipedWriter before you read from your PipedReader.
3) Pipes are normally used between different threads
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this does what you are expecting:

Thank you for giving me the opportunity to learn about Piped IO and threads.
[ April 02, 2003: Message edited by: Barry Gaunt ]
 
eric lee
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barry Gaunt , i learn too!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic