这个代码怎么改就正确了?

朗晴 2012-06-18 04:28:40

import java.io.*;

public class pipedIo
{
public static void main(String [] args) throws Exception
{
try
{
//构造读写的管道流对象
PipedInputStream pis=new PipedInputStream();
PipedOutputStream pos=new PipedOutputStream();
//实现关联
pos.connect(pis);
//构造两个线程,并启动
new Sender(pos,"d:\\HelloWorld\\test1.txt").start();
new Receiver(pis,"d:\\HelloWorld\\test2.txt").start();
// new Sender().start();
// new Receiver().start();
}catch(Exception 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);
}
pos.close();
}catch(Exception 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);
}
pis.close();
}catch(Exception e)
{
System.out.println("Receiver Error"+e);
}
}
}
}
}
...全文
124 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
朗晴 2012-06-18
  • 打赏
  • 举报
回复
谢谢楼上的楼上的主,你辛苦了,分全给你。
zmywhhit 2012-06-18
  • 打赏
  • 举报
回复
能运行没有问题呀
LamarChen 2012-06-18
  • 打赏
  • 举报
回复
修改了下。。。


import java.io.*;

public class PipedIo
{
//构造读写的管道流对象
public static PipedInputStream pis=new PipedInputStream();
public static PipedOutputStream pos=new PipedOutputStream();

public static void main(String [] args) throws Exception
{
try
{
//实现关联
pos.connect(pis);
//构造两个线程,并启动
PipedIo pi = new PipedIo();
Thread t1 = new Thread(pi.new Sender(pos,"d:\\HelloWorld\\test1.txt"));
Thread t2 = new Thread(pi.new Receiver(pis,"d:\\HelloWorld\\test2.txt"));
t1.start();
t2.start();
}catch(Exception e)
{
System.out.println("Pipe Error"+e);
}

}

//线程发送
class Sender implements Runnable
{
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);
}
pos.close();
}catch(Exception e)
{
System.out.println("Sender Error"+e);
}

}
}

//线程读
class Receiver implements Runnable
{
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);
}
pis.close();
}catch(Exception e)
{
System.out.println("Receiver Error"+e);
}
}
}
}
朗晴 2012-06-18
  • 打赏
  • 举报
回复
这个程序在我这儿刷不通过。谁那里能刷通过!
我也不知道是哪里写错了。所以请教~
我照着例子写了一整天,不知道这个程输出结果是什么,发现一点儿都不明白,只是明白一点是管道流。
所以这个程序需要改动哪里才能编译,然后结果是怎样的?
淡定的峰哥 2012-06-18
  • 打赏
  • 举报
回复
有什么问题吗

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧