java的IO问题······求大神们指教
class Averagecount extends Thread{
private DataInputStream Din;
double total=0;
long count=0;
public Averagecount(InputStream consumer){
Din=new DataInputStream(consumer);
}
public void run(){
while(true){
double num;
try {
num = Din.readDouble();
total+=num;
count++;
System.out.println("The average is "+ total/count);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Randomnumber extends Thread{
private DataOutputStream outs;
private Random ran=new Random();
private final long Term= 1000;
public Randomnumber(OutputStream out){
outs=new DataOutputStream(out);
}
public void run(){
while(true){
double num=ran.nextDouble()*Term;
try {
outs.writeDouble(num);
outs.flush();
sleep(500);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
}
}
}
}
public class FileTest{
public static void main(String[] args){
try {
PipedInputStream producter=new PipedInputStream();
PipedOutputStream consumer=new PipedOutputStream(producter);
Averagecount avc=new Averagecount(consumer);
Randomnumber ranu=new Randomnumber(producter);
avc.start();
ranu.start();
try {
Thread.sleep(10000);
} catch (Exception e) {
// TODO: handle exception
}
avc.stop();
ranu.stop();
producter.close();
consumer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
大神求指教。为什么这两个:
Averagecount avc=new Averagecount(consumer);
Randomnumber ranu=new Randomnumber(producter);
无法进行实例化。pipe是inputstream的子类,然后filter是inputstream的子类,data是filter的子类。然后是不是data的构造器只能够接受input类和他的父类,不能够是其他input的子类么? java io filter inputstream datainputstream
[解决办法]
public Averagecount(InputStream consumer){
Din=new DataInputStream(consumer);
}
要求传入InputStream的,然后你
PipedOutputStream consumer=new PipedOutputStream(producter);
Averagecount avc=new Averagecount(consumer);
给人家传入OutputStream的。
写程序的时候细心点吧。