Rewrite file inplace