在书上看到一段代码,前面的代码都能理解,但finally{}内代码的作用不是太清楚,哪位同学能解答一下?
且将该段代码注释掉后运行没遇到什么影响。。。
- import java.io.*;
- public class SequenceStream{
- public static void main(String[] args)throws Exception{
- FileInputStream in1=null,in2=null;
- SequenceInputStream sequence=null;
- FileOutputStream out=null;
- try{
- File file1=new File("e:\\Java\\demofolder\\file1.txt");
- File file2=new File("e:\\Java\\demofolder\\file2.txt");
- File file3=new File("e:\\Java\\demofolder\\file3.txt");
- in1=new FileInputStream(file1);
- in2=new FileInputStream(file2);
- sequence=new SequenceInputStream(in1,in2);
- out=new FileOutputStream(file3);
- int c;
- while((c=sequence.read())!=-1){
- out.write(c);
- }
- in1.close();
- in2.close();
- out.close();
- sequence.close();
- System.out.println("OK。。。。。。");
- }catch(IOException e){
- e.printStackTrace();
- }
- finally{
- if(in1!=null)
- try{
- in1.close();
- }catch(IOException e){}
- if(in2!=null)
- try{
- in2.close();
- }catch(IOException e){}
- if(out!=null)
- try{
- out.close();
- }catch(IOException e){}
- if(sequence!=null)
- try{
- sequence.close();
- }catch(IOException e){}
- }
- }
- }
复制代码 |