Handle file io with proper error handling.