package test;import java.io.*;public class asd {
static FileInputStream in = new FileInputStream("c:\\Example1.xml");
public static void main(String[] args) throws IOException{
int b = 0;
while((b=in.read())!=-1){
System.out.print((char)b);}
}
}这段代码中为什么static FileInputStream in = new FileInputStream("c:\\Example1.xml");这里会报错咧?说我未处理异常类型,可是我在后面加上throws Exception的话 throws还是报错 说“应为,”这我就搞不懂了
static FileInputStream in = new FileInputStream("c:\\Example1.xml");
public static void main(String[] args) throws IOException{
int b = 0;
while((b=in.read())!=-1){
System.out.print((char)b);}
}
}这段代码中为什么static FileInputStream in = new FileInputStream("c:\\Example1.xml");这里会报错咧?说我未处理异常类型,可是我在后面加上throws Exception的话 throws还是报错 说“应为,”这我就搞不懂了
FileInputStream in = new FileInputStream("c:\\Example1.xml");
}catch(Exception e){}
试一试你只是对方法 main()抛出了异常,没有具体对某些会抛出异常的语句做出异常捕获
可以这样修改:public class Test120090402 {
static FileInputStream in; public static void main(String[] args) throws IOException {
in = new FileInputStream("c:\\Example1.xml");
int b = 0;
while ((b = in.read()) != -1) {
System.out.print((char) b);
}
}
}
in 的声明不能放在try/catch中,这样就缩小了他的作用域
在main方法中是不能访问的,应该把in的声明放在外面,
而他的实例化过程应置于一个{}中
public class Test {
static FileInputStream in = null;
{
try {
in = new FileInputStream("c:\\Example1.xml");
} catch (Exception e) {}
} public static void main(String[] args) throws IOException{
int b = 0;
while((b=in.read())!=-1){
System.out.print((char)b);}
}
}