try(InputStream in=new FileInputStream("article.txt"))
{
while((word=(char) in.read())!=-1)
{
System.out.print(word);
}
}catch(EOFException e){
System.out.println("ending...");
}
(word=(char) in.read())!=-1 这个循环条件对么?为什么要用char类型去判断!=-1,不用强制转换,word声明为int;改为:(word= in.read())!=-1,同时异常捕捉改为Exception,易判断出错原因。流最后要加finnaly子句关闭流。
把EOFException 这个换成 Exception 试试看呢。
而且要记得关闭文件流的句柄 ,添加 finally {
in.close();
}
http://cuisuqiang.iteye.com/blog/1434416
参考这个。
在文件的读写过程中不仅仅只有EOFException,还可能会有IOException,这里捕获不到异常是因为抛出的异常不能覆盖读写过程中的全部异常类型,修改意见是将EOFException修改成Exception,这样才可以覆盖全部的异常类型,同时添加finally子句,关闭输入流
try个全的 把EOFException修改成Exception 应该就可以了
捕获Exception试试