pipedDemo.java:146:unreported exception java.io.FileNotFoundException:must b e caught or declared to be thrown FileInputStream fin = new FileInputStream(fFile); ^
FileInputStream fin = new FileInputStream(fFile); 更改成 try { FileInputStream fin = new FileInputStream(fFile); } catch(Exception e) { }
FileInputStream fin = new FileInputStream(fFile);出错
谢谢你同时对两个问题的指导和关注
中调用
System.out.println(file)看是什么东西?
e caught or declared to be thrown
FileInputStream fin = new FileInputStream(fFile);
^
更改成
try
{
FileInputStream fin = new FileInputStream(fFile);
}
catch(Exception e)
{
}
现在显示
variable:fin
location:class pipesender
while((n=fin.read())!=-1)
^
class PipedIODemo{ public static void main(String args[]) { try { PipedInputStream pin = new PipedInputStream(); PipedOutputStream pout = new PipedOutputStream(); pout.connect( pin); new PipeSender(pout,"sendString").start(); new PipeReciver(pin,"receiveString").start(); } catch(IOException e) { e.printStackTrace(); } }}class PipeSender extends Thread{ PipedOutputStream pout; File fFile; PipeSender(PipedOutputStream out,String file) { fFile = new File(file);
System.out.println(file);
pout = out; } public void run() {
try
{
FileInputStream fin = new FileInputStream(fFile); }
catch (Exception e)
{}
int n; try { while((n=fin.read())!=-1) { pout.write(n); } pout.close(); } catch(IOException e) { e.printStackTrace(); } }}class PipeReciver extends Thread{ PipedInputStream pin; File fFile; PipeReciver (PipedInputStream in,String file) { fFile =new File(file);
System.out.println(file);
pin = in; } public void run() { FileOutputStream fout = new FileOutputStream(fFile); int n; try { while((n=pin.read())!=-1) { fout.write(n); } } catch(IOException e) { e.printStackTrace(); } }}
运行JAVAC
现在显示以下信息:
PipedIODemo:java:167;can not resolve sysmbol
variable:fin
location:class pipesender
while((n=fin.read())!=-1)
^
import java.io.*;
import java.io.File;class PipedIODemo {
public static void main(String args[]){
try{
PipedInputStream pin = new PipedInputStream();
PipedOutputStream pout = new PipedOutputStream();
pout.connect( pin);
new PipeSender(pout,"sendString").start();
new PipeReciver(pin,"receiveString").start();
}
catch(IOException e){
e.printStackTrace();
}
}
}class PipeSender extends Thread { PipedOutputStream pout;
File fFile; PipeSender(PipedOutputStream out,String file){
fFile = new File(file);//系统显示这里出错
pout = out;
} public void run() {
FileInputStream fin = null;
try {
fin = new FileInputStream(fFile);
}
catch (FileNotFoundException ex) {
}
int n;
try {
while((n=fin.read())!=-1){
pout.write(n);
}
pout.close();
}
catch(IOException e){
e.printStackTrace();
}
}
}class PipeReciver extends Thread { PipedInputStream pin; File fFile;
PipeReciver (PipedInputStream in,String file){
fFile = new File(file);
pin = in;
} public void run(){
FileOutputStream fout = null;
try {
fout = new FileOutputStream(fFile);
}
catch (FileNotFoundException ex) {
} int n; try{
while((n=pin.read())!=-1){
fout.write(n);
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
>我用你的作品,编译顺利通过。
>C:\javacode>java PipedIODemo
>java.lang.NullPointerException
> at PipeSender.run(PipedIODemo.java:43)
>光标就在下面闪烁。
>请问是怎么回事呢
这是因为输入文件(sendString)不存在造成的。你可以自己构造一个。
仍然出现以下结果。
C:\javacode>java PipedIODemo
>java.lang.NullPointerException
> at PipeSender.run(PipedIODemo.java:43)在c:\javacode目录下建立 receiveString文件也如此。请问该怎么办啊?