本文介绍了节点js:fs.rename覆盖文件,如果已经存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

fs.rename是否覆盖已存在的文件?

  var fs = require('fs'),
oldPath ='firstfile.txt',
newPath ='temp / firstfile.txt';
$ b $ fs.rename(oldPath,newPath,function(err){
console.log('rename callback',err);
});

如果'/newFolder/somefile.txt'存在,会发生什么?

解决方案

简短回答: >

长答案:



我创建了一个脚本来检查它:

  var fs = require('fs'); 

创建两个文件:

 fs.writeFileSync('b.txt',这是另一个文件)

重命名:

  fs.renameSync( 'A.TXT', 'b.txt'); 

检查是否被覆盖:

  var text = fs.readFileSync('b.txt',utf-8); 

console.log(text)//这是一个文件


Does fs.rename overwrite the file if it already exists?

var fs = require('fs'),
    oldPath = 'firstfile.txt',
    newPath = 'temp/firstfile.txt';

fs.rename(oldPath, newPath, function (err) {
    console.log('rename callback ', err);
});

What happens if '/newFolder/somefile.txt' aready exists ?

解决方案

Short answer: yes


Long answer:

I created a script to check it:

var fs = require('fs');

Create two files:

fs.writeFileSync('a.txt',"This is a file")
fs.writeFileSync('b.txt',"This is another file")

Rename:

fs.renameSync('a.txt','b.txt');

Check if it was overriden:

var text = fs.readFileSync('b.txt', "utf-8");

console.log(text) // This is a file

这篇关于节点js:fs.rename覆盖文件,如果已经存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:17
查看更多