小弟不明

07-10-28 lang131719
import java.io.*;

public class PipedIO //程序运行后receiverFile文件获得sendFile文件的内容

{

public static void main(String args[]) //注意:在执行该程序之前应该在当前目录下有sendFile文件

{

try

{

PipedInputStream pis=new PipedInputStream();

PipedOutputStream pos=new PipedOutputStream();

pos.connect(pis);

new Sender(pos,"sendFile").start();

new Receiver(pis,"receiverFile").start();

}

catch(IOException e)

{ System.out.println("Pipe Error"+ e);

}

}

}

class Sender extends Thread

{

PipedOutputStream pos;

File file;

Sender(PipedOutputStream pos, String fileName)

{ this.pos=pos;

file=new File(fileName);

}

public void run()

{

try

{ FileInputStream fs=new FileInputStream(file);

int data;

while((data=fs.read())!=-1)

{

pos.write(data);

System.out.println("正在发送数据 !");

}

pos.close();

}catch(IOException e)

{ System.out.println("Sender Error" +e);

}

}

}

class Receiver extends Thread

{

PipedInputStream pis;

File file;

Receiver(PipedInputStream pis, String fileName)

{ this.pis=pis;

file=new File(fileName);

}

public void run()

{

try

{ FileOutputStream fs=new FileOutputStream(file);

int data;

while((data=pis.read())!=-1)

{

fs.write(data);

System.out.println("正在接收数据 !");

}

pis.close();

}catch(IOException e)

{ System.out.println("Receiver Error" +e);

}

}

}

下面这两句怎么解释

new Sender(pos,"sendFile").start();

new Receiver(pis,"receiverFile").start();

qlqsh
2007-10-29 18:13
开始线程。。。

猜你喜欢