第5章Java例外处理 H File Out put Stream fos-new File Out put Stream("file out. txt") while(i=fis reado)!=-1) fos. write(1) fis. closed fos. closed;)) 例5,2只是一个简单的文件输入输出程序。该程序 中定义了一个文件输入流和文件输出流,然后通过它 们进行文件的拷贝
第5章 Java例外处理 File Out put Stream fos=new File Out put Stream("file_out.txt"); while(i=fis.read())!=-1) fos.write(i); fis.close(); fos.close(); } } 例5.2只是一个简单的文件输入/输出程序。该程序 中定义了一个文件输入流和文件输出流,然后通过它 们进行文件的拷贝
A第5章Ja例外处理 上述程序进行编译的时候,结果如下: C: UBUILD bin>javac Exception Demo1. java Exception Demol. java: 5 Exception java1o. FileNotFoundException must be caught or it must be declared in the throws clause of this method FileInputStream fisnew FileInputStream("file in. txt); Exception Demol java: 7: Exception java. io IOException must be caught, or it must be declared in the throws clause of this method while((i=fis reado)!=-1) errors
第5章 Java例外处理 当上述程序进行编译的时候,结果如下: c:\JBUILD~1\java\bin>javac ExceptionDemo1.java ExceptionDemo1.java:5: Exception java.io.FileNotFoundException must be caught, or it must be declared in the throws clause of this method. FileInputStream fis=new FileInputStream("file_in.txt"); ^ ExceptionDemo1.java:7: Exception java.io.IOException must be caught, or it must be declared in the throws clause of this method. while((i=fis.read())!=-1) 2 errors
第5章Java例外处理 H 系统异常信息告诉我们,两种异常程序员必须捕 获,即 File not foundexcetion和 IOException。对例外进 行处理时,用户往往想知道异常的具体信息,我们可 利用异常父类 Throwable中提供的方法 get Message得 到有关异常事件的信息。方法 print Stack Trace可用来 跟踪异常事件发生时执行堆栈的内容。这样,例52可 改写为
第5章 Java例外处理 系统异常信息告诉我们,两种异常程序员必须捕 获,即File Not FoundExcetion和IOException。对例外进 行处理时,用户往往想知道异常的具体信息,我们可 利用异常父类Throwable中提供的方法get Message()得 到有关异常事件的信息。方法print Stack Trace()可用来 跟踪异常事件发生时执行堆栈的内容。这样,例5.2可 改写为
第5章Java例外处理 H 例53 import java. 1o. public class Exception Demol i public static void main(String argsi FileInputstream fisnew FileInputStream("file in. txt") FileOutputStreamfos-new FileOutputStream("file out tx t while((ifis reado)!=-1) fos. write(i)
第5章 Java例外处理 例5.3 import java.io.*; public class ExceptionDemo1 { public static void main(String args[]) { int i; try { FileInputStream fis=new FileInputStream("file_in.txt"); FileOutputStreamfos=new FileOutputStream("file_out.tx t"); while((i=fis.read())!=-1) fos.write(i);
第5章Java例外处理 H fis.close; fos. closed )catch(FileNotFoundException e) System. out. println(" The error IS +e.getMessageo) e printstackTrace(System.ou catch(IOException e) System. out. println ("The error is: +e)
第5章 Java例外处理 fis.close(); fos.close(); }catch (FileNotFoundException e){ System.out.println("The error is: "+e.getMessage()); e.printStackTrace(System.out); }catch(IOException e) { System.out.println("The error is: "+e); } } }