日期:2014-05-20  浏览次数:20845 次

求助,改下程序
package test3;
import java.io.*;
public class PipeStreamTest
{
  public static void main(String args[])
  {
  try
  {
  Thread t1=new Sender();
  Thread t2=new Receiver();
  PipedOutputStream out = t1.getOutputStream();
PipedInputStream in = t2.getInputStream();
  out.connect(in);
  t1.start();
  t2.start();
  }
  catch(IOException e)
  {
  System.out.println(e.getMessage());
  }
  }
}
class Sender extends Thread
{
  private PipedOutputStream out=new PipedOutputStream();
  public PipedOutputStream getOutputStream()
  {
  return out;
  }
  public void run()
  {
  String s=new String("hello,receiver ,how are you");
  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 [] buf = new byte[1024];
  try
  {
  int len =in.read(buf);
s = new String(buf,0,len);
  System.out.println("the following message comes from sender:\n"+s);
  in.close();
  }
  catch(IOException e)
  {
  System.out.println(e.getMessage());
  }
  }
}
这个程序有语法错误
Exception in thread "main" java.lang.Error: Unresolved compilation problems
The method getOutputStream() is undefined for the type Thread
The method getInputStream() is undefined for the type Thread

at test3.PipeStreamTest.main(PipeStreamTest.java:22)

请问下怎么修改程序呢,首先谢谢了

------解决方案--------------------
Java code

import java.io.*;

public class PipeStreamTest {
    public static void main(String args[]) {
        try {
            Sender t1 = new Sender();
            Receiver t2 = new Receiver();
            PipedOutputStream out = t1.getOutputStream();
            PipedInputStream in = t2.getInputStream();
            out.connect(in);
            t1.start();
            t2.start();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

class Sender extends Thread {
    private PipedOutputStream out = new PipedOutputStream();

    public PipedOutputStream getOutputStream() {
        return out;
    }

    public void run() {
        String s = new String("hello,receiver ,how are you");
        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[] buf = new byte[1024];
        try {
            int len = in.read(buf);
            s = new String(buf, 0, len);
            System.out
                    .println("the following message comes from sender:\n" + s);
            in.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}