public FileInputStream(String name)
throws FileNotFoundException
throws FileNotFoundException 为什么throws FileNotFoundException 要用try catch语句对啊,throws不是向上一级抛出异常吗?不过他不是runtime Exception 必须要捕获,不然会出错的。
throws FileNotFoundException
throws FileNotFoundException 为什么throws FileNotFoundException 要用try catch语句对啊,throws不是向上一级抛出异常吗?不过他不是runtime Exception 必须要捕获,不然会出错的。
import java.io.FileNotFoundException;public class FileList {
public static void main(String args[]) {
int b = 0;
int num = 0;
FileInputStream fis = null;
try {
fis = new FileInputStream("D:/A/B.txt");
} catch (FileNotFoundException e) {
System.out.println("而文件不错");
System.out.println(-1);
}
try {
while((b = fis.read()) != 0) {
System.out.print((char)b);
num ++;
}
fis.close();
} catch(IOException e) {
System.out.println("而文件不错");
System.out.println(-1);
}finally {
System.out.println("又成功了加油");
System.out.println("输出了" + num + "个字符");
}
}
}
这个finally语句为什么执行不了啊?????
而文件不错
-1
又成功了加油
输出了0个字符
public int read()
throws IOException
Reads a byte of data from this input stream. This method blocks if no input is yet available. Specified by:
read in class InputStream
Returns:
the next byte of data, or -1 if the end of the file is reached.
Throws:
IOException - if an I/O error occurs.
public static void main(String args[])throws FileNotFoundException..这样就不用try catch了