import java.io.*;
public class PipeStreamTest
{
public static void main(String args[])
{
try
{
Thread e1 = new Sender();
Thread e2 = new Receiver();
PipedOutputStream out = e1.getOutputStream();
PipedInputStream in = e2.getInputStream();
out.connect(in);
e1.start();
e2.start();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
class Sender extends Thread
{
private PipedOutputStream out = new PipedOutputStream();
public PipedOutputStream getOutputStream() //PipedOutputStream为返回类型
{
return out;
}
public void run()
{
String s = new String("My name is YuanPing");
try
{
out.write(s.getBytes());
out.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
class Receiver extends Thread
{
private PipedInputStream in = new PipedInputStream();
public PipedInputStream getInputStream()
{
return in;
}
public void run()
{
String s = null;
byte bt[] = new byte[1024];
int len;
try
{
len = in.read(bt);
s = new String(bt,0,len);
System.out.println("The following message comes from Sender:\n" + s);
in.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
编译报的错:
D:\PipeStreamTest.java:10: 找不到符号
符号: 方法 getOutputStream()
位置: 类 java.lang.Thread
PipedOutputStream out = e1.getOutputStream();
^
D:\PipeStreamTest.java:11: 找不到符号
符号: 方法 getInputStream()
位置: 类 java.lang.Thread
PipedInputStream in = e2.getInputStream();
^
2 错误
getOutputStream()和getInputStream()明明在Sender和Receiver中自己定义了啊!
为什么还这报错?高手们,帮帮忙吧!