[Preview] Fix leaked file descriptor during writing

`File.open(path, 'r').read` leaks the file descriptor that is opened. Simplified logic to use `File.write` and `File.read` to stop leaking this file descriptor.