nodejs writeSync 的position不起作用呢

import * as fs from 'fs';
import * as path from 'path';
class BreakFileSave{
    constructor(protected option: IOptione) {
        this.init();
    }
    protected init() {
        let filePath =  path.resolve(this.option.path+'/'+this.option.hash+'.'+this.option.subffix.toLowerCase());
        console.log(filePath);
        if(fs.existsSync(filePath)) {
            console.log('文件存在');
        }else{
            console.log('文件不存在');
            let buffer = Buffer.alloc(this.option.size);
            let res = fs.writeFileSync(filePath, buffer);
        }
        this.test(filePath)
    }
    test(filePath: string) {
        let fd = fs.openSync(filePath, 'a+');
        let buf = Buffer.from('dd2', 'utf8');
        fs.writeSync(fd, buf ,0,buf.length,8);
        fs.closeSync(fd);
    }
}

每次写入都是在文件的末尾

阅读 2k
1 个回答

nodejs fs 的文档 里说:

On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.

a+ 模式打卡就只能想末尾写。你得看下文档,换一个你需要的打开方式。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题