NodeJS - fs

NodeJs 中提供了很多File System(文件系统的API), 又是跨平台的, 以下实例完成一些基本的文件操作:比如创建,读(使用Stream),写,移动,删除文件,各位同学可以作为参考。

/*
	1. Create a file
	2. Write some data, 5000 lines
	3. Rename it
	4. Move it to a non-existing sub folder.
	5. Read whole file and print
	6. Read file by stream and print each chunk's content and size.
	7. Clear all conent.
	8. Write your name in it 100 times and print it.
	9. Delete it
*/
var fs = require('fs');

var content = "Hello world.";
var folderName = "exam";
var fileName = "exam.log";
var badFileName = "examBad.log";
var encoding = "utf-8";
var linesOfData = 10000;


// 1. Create a file
console.log('1. Create a file');
fs.writeFileSync(fileName, `${1}: ${content} \r\n`, encoding);

// 2. Write some data, 5000 lines
console.log('\r\n2. Write some data, 5000 lines');
for(var i = 2 ; i <= linesOfData; i++){
	fs.appendFileSync(fileName, `${i}: ${content} \r\n`, encoding);
}
console.log("Write file done.");

// 3. Rename it
console.log('\r\n3. Rename it');
fs.renameSync(fileName, "helloWorld.log");
console.log('Rename file done.');
fs.renameSync("helloWorld.log", fileName);

// 4. Move it to a non-existing sub folder.
console.log('\r\n4. Move it to a non-existing sub folder.');
if(fs.existsSync(folderName)){
	console.log(`Folder ${folderName} already exists.`);
}else{
	fs.mkdirSync(folderName);
	console.log(`Folder create done: ${folderName} created.`);
}

fs.renameSync(fileName, folderName + '/' + fileName);
console.log(`Move file done: file '${fileName}' was moved to '${folderName}/${fileName}'`);

fs.renameSync(folderName + '/' + fileName, fileName);
console.log(`Move file back: file '${folderName}/${fileName}' was moved to '${fileName}'`);

// 5. Read whole file and print
console.log('\r\n5. Read whole file and print.');
try{
	var data = fs.readFileSync(fileName, encoding);
	console.log(`File read done: Length:${data.length}`);
}catch(err){
	console.log(err);
}

// 6. Read file by stream and print each chunk's content and size.
console.log('\r\n6. Read file by stream and print each chunk-s content and size.');
var stream = fs.createReadStream(fileName);
var data = [];

// Async call: data may display after all execution.
stream.once('data', function(){
	console.log(`Start reading file: '${fileName}'`);
});

// Async call: data may display after all execution.
stream.on('data', function(chunk){
	data.push(chunk);
	console.log(`Chunk read size: ${chunk.length}`);
})
// Async call: data may display after all execution.
stream.on('end', function(){
	console.log(`Read file '${fileName}' done. Total size:${data.join('').length}`);
})

// 7. Clear all conent.
console.log('\r\n7. Clear all conent.');
fs.writeFileSync(fileName, '');
console.log('File content was cleared.');

// 8. Write your name in it 100 times and print it.
console.log('\r\n8. Write your name in it 100 times and print it.');
for(var i = 0 ; i < 100 ; i++) {
	fs.appendFileSync(fileName, `${i} Cui`);
}
// Async call: data may display after all execution.
fs.readFileSync(fileName, encoding, function(err, data){
	if(err)
		console.log(err);
	else
		console.log(data)
});

// 9. Delete it
console.log('\r\n9. Delete it.');
fs.unlink(fileName, function(err){
	if(err)
		console.log(err);
	else
		console.log(`File '${fileName}' was deleted.`);
})

/*
	Async call will crush
fs.writeFile(badFileName, `${1}: ${content} \r\n`, encoding, function(err){
	if(err){
		console.log(err);
	}
});

for(var i = 2; i < linesOfData; i++){
	var j = i;
	var dataToWrite = `${j}: ${content} \r\n`;
	fs.appendFile(badFileName, dataToWrite, encoding, function(err){
		if(err){
			console.log(err);
		}else{
			console.log(`Write data:${dataToWrite}`);
		}
	});
}
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值