Closing a Java FileInputStream

前端 未结 9 936
故里飘歌
故里飘歌 2020-12-08 04:29

Alright, I have been doing the following (variable names have been changed):


FileInputStream fis = null;
try
{
    fis = new FileInputStream(file);

    ...         


        
9条回答
  •  醉酒成梦
    2020-12-08 05:08

    Something like the following should do it, up to you whether you throw or swallow the IOException on attempting to close the stream.

    FileInputStream fis = null;
    try
    {
        fis = new FileInputStream(file);
    
        ... process ...
    
    
    }
    catch (IOException e)
    {
        ... blah blah blah ...
    }
    finally
    {
        try
        {
            if (fis != null)
                fis.close();
        }
        catch (IOException e)
        {
        }
    }
    

提交回复
热议问题