BS-11472, use stream closure before manipulating files