Close inpout stream before opening output stream

On windows a lock is hold on the file as soon as we open it for reading.
Close the input stream before we can open the output stream with no
problem.