且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除文件的前N行

更新时间:2023-12-01 22:14:52

文件系统不支持从文件的任意位置删除,因此您至少需要自己重新编写整个文件.实施所需内容的常用方法是

Filesystems doesn't support deleting from arbitrary locations of a file, so you at least need to re-write the entire file yourself. The common way to implement what you need is to

  • 打开一个临时文件,
  • 从原始文件中读取小片段,例如在您的情况下,每次只能一行一行
  • 仅将要保留在原始文件中的行写到临时文件中
  • 完成后,将临时文件重命名为原始文件名.