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);
}
}
每次写入都是在文件的末尾
nodejs fs 的文档 里说:
用
a+
模式打卡就只能想末尾写。你得看下文档,换一个你需要的打开方式。