2019-06-22 fs模塊

一、同步實(shí)現(xiàn)

// 使用fs模塊,創(chuàng)建一個(gè)目錄fs
// 使用fs模塊,在目錄中創(chuàng)建一個(gè)test.txt文件,往里面寫(xiě)入hello world字符串
// 使用fs模塊,復(fù)制之前寫(xiě)的test.txt文件,并粘貼到同一文件夾下改名為hello.txt
// 使用fs模塊,讀取hello.txt文件的大小和創(chuàng)建時(shí)間
// 使用fs模塊,刪除fs目錄

var fs = require('fs');

fs.mkdirSync('fs',function(err){
});

var data = 'hello world';
fs.writeFileSync('fs/test.txt',data);

fs.copyFileSync('fs/test.txt','fs/hello.txt');

console.log('創(chuàng)建大?。?+ fs.statSync('fs/hello.txt').size);
console.log('創(chuàng)建時(shí)間:'+ fs.statSync('fs/hello.txt').ctime);


function deleteall(path) {
    var files = [];
    if(fs.existsSync(path)) {
        files = fs.readdirSync(path);
        files.forEach(function(file, index) {
            var curPath = path + "/" + file;
            if(fs.statSync(curPath).isDirectory()) { // recurse
                deleteall(curPath);
            } else { // delete file
                fs.unlinkSync(curPath);
            }
        });
        fs.rmdirSync(path);
    }
};
deleteall('./fs');

二、同步實(shí)現(xiàn)(不同一個(gè)文件下)

//使用fs模塊,創(chuàng)建一個(gè)目錄fs
var fs = require('fs');

fs.mkdir('fs',function(err){
    if(err) throw err;

    console.log('創(chuàng)建目錄成功!');
})
// 使用fs模塊,在目錄中創(chuàng)建一個(gè)test.txt文件,往里面寫(xiě)入hello world字符串
var fs = require('fs');
var data = 'hello world';
fs.writeFile('fs/test.txt',data,function(err){
    if(err) throw err;
    console.log('寫(xiě)入成功!');
});
// 使用fs模塊,復(fù)制之前寫(xiě)的test.txt文件,并粘貼到同一文件夾下改名為hello.txt
var fs = require('fs');
fs.copyFile('fs/test.txt','fs/hello.txt',function(err){
    if(err) throw err;

    console.log('copy成功!');
});
// 使用fs模塊,讀取hello.txt文件的大小和創(chuàng)建時(shí)間
var fs = require('fs');

fs.readFile('fs/hello.txt','utf8',function(err,data){
    if(err) throw err;
    console.log(data);

    var stat = fs.statSync('fs/hello.txt');

    var createTime =stat.ctime;
    var size = stat.size;
    console.log('創(chuàng)建時(shí)間:'+createTime);
    console.log('文件大小:'+size);

});
// 使用fs模塊,刪除fs目錄
var fs = require('fs');

function deleteall(path) {
    var files = [];
    if(fs.existsSync(path)) {
        files = fs.readdirSync(path);
        files.forEach(function(file, index) {
            var curPath = path + "/" + file;
            if(fs.statSync(curPath).isDirectory()) { // recurse
                deleteall(curPath);
            } else { // delete file
                fs.unlinkSync(curPath);
            }
        });
        fs.rmdirSync(path);
    }
};
deleteall('./fs');


  1. existsSync() :路徑是否存在 ( 同步方法)
var fs =  require('fs');
console.log('file是否存在:'+fs.existsSync('file'));
console.log('Stage_Two是否存在:'+fs.existsSync('Stage_Three/fs_all.js'));
  1. readdir() // readdirSync() 讀取目錄內(nèi)容
var fs =  require('fs');
console.log(fs.readdir('Stage_Two','utf8',function(err,files){
    if(err) throw err;

    console.log('\n異步方法:');
    console.log(files); 
}));
console.log('同步方法:');
console.log(fs.readdirSync('Stage_Two'));
  1. 刪除
    unlinkSync(path) // unlink(path) 刪除文件
    remdir(path)\ rmdirSync(path) 刪除文件夾
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Node.js 常用工具 util 是一個(gè)Node.js 核心模塊,提供常用函數(shù)的集合,用于彌補(bǔ)核心JavaScr...
    FTOLsXD閱讀 615評(píng)論 0 2
  • feisky云計(jì)算、虛擬化與Linux技術(shù)筆記posts - 1014, comments - 298, trac...
    不排版閱讀 4,383評(píng)論 0 5
  • 一、Python簡(jiǎn)介和環(huán)境搭建以及pip的安裝 4課時(shí)實(shí)驗(yàn)課主要內(nèi)容 【Python簡(jiǎn)介】: Python 是一個(gè)...
    _小老虎_閱讀 6,356評(píng)論 0 10
  • 一、核心模塊和對(duì)象 核心模塊的意義 常用內(nèi)置模塊path:處理文件路徑fs:操作文件系統(tǒng)child_process...
    EndEvent閱讀 4,518評(píng)論 0 1
  • 很早以前,當(dāng)微信正在蠶食微博的時(shí)候,我還在玩著QQ。追時(shí)尚,我總是慢人幾步,直到身邊的朋友都玩著微信了,為趕時(shí)髦,...
    海利大人閱讀 873評(píng)論 0 2

友情鏈接更多精彩內(nèi)容