复制代码
- //:Cleanup.java
- //Payingattentiontoexceptions
- //inconstructors
- importjava.io.*;
- classInputFile{
- privateBufferedReaderin;
- InputFile(Stringfname)throwsException{
- try{
- in=
- newBufferedReader(
- newFileReader(fname));
- //Othercodethatmightthrowexceptions
- }catch(FileNotFoundExceptione){
- System.out.println(
- "Couldnotopen"+fname);
- //Wasntopen,sodontcloseit
- throwe;
- }catch(Exceptione){
- //Allotherexceptionsmustcloseit
- try{
- in.close();
- }catch(IOExceptione2){
- System.out.println(
- "in.close()unsuccessful");
- }
- throwe;
- }finally{
- //Dontcloseithere!!!
- }
- }
- StringgetLine(){
- Strings;
- try{
- s=in.readLine();
- }catch(IOExceptione){
- System.out.println(
- "readLine()unsuccessful");
- s="failed";
- }
- returns;
- }
- voidcleanup(){
- try{
- in.close();
- }catch(IOExceptione2){
- System.out.println(
- "in.close()unsuccessful");
- }
- }
- }
- publicclassCleanup{
- publicstaticvoidmain(String[]args){
- try{
- InputFilein=
- newInputFile("Cleanup.java");
- Strings;
- inti=1;
- while((s=in.getLine())!=null)
- System.out.println(""+i+++":"+s);
- in.cleanup();
- }catch(Exceptione){
- System.out.println(
- "Caughtinmain,e.printStackTrace()");
- e.printStackTrace();
- }
- }
- }///:~
欢迎光临 仓酷云 (http://ckuyun.com/) | Powered by Discuz! X3.2 |