Only open the file for writing if a replacement is needed.