简体   繁体   English

在node.js中的大文件中删除最后N个字节

[英]Delete last N bytes in a large file in node.js

I am looking for a way to delete the last few bytes in a large file in node.js without buffering the entire file into the memory and saving it. 我正在寻找一种方法来删除node.js中大文件中的最后几个字节,而不将整个文件缓冲到内存中并保存。 Is there any way to do this? 有什么办法吗?

Edit: By "last few bytes" I mean characters. 编辑:“最后几个字节”是指字符。 I have tried fs.truncate but it just deletes the entire file's contents. 我已经尝试过fs.truncate,但是它只是删除了整个文件的内容。

Truncate not working example: 截断无效示例:

var fs = require('fs');

fs.truncate('C:\\NODEAPP\\totruncate.txt', 0, function(){
    console.log('File got cleared... Is 0 supposed to delete the last character?');
})

I figured it out. 我想到了。 The 0 means how long the file should be afterwards. 0表示文件应保留多长时间。

var fs = require('fs');

fs.truncate('C:\\NODEAPP\\totruncate.txt', 5, function(){
    console.log('Kept first 5 bytes, deleted the rest.');
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM