2019-06-22 fs模块

一、同步实现

// 使用fs模块,创建一个目录fs
// 使用fs模块,在目录中创建一个test.txt文件,往里面写入hello world字符串
// 使用fs模块,复制之前写的test.txt文件,并粘贴到同一文件夹下改名为hello.txt
// 使用fs模块,读取hello.txt文件的大小和创建时间
// 使用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('创建大小:'+ fs.statSync('fs/hello.txt').size);
console.log('创建时间:'+ 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');

二、同步实现(不同一个文件下)

//使用fs模块,创建一个目录fs
var fs = require('fs');

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

    console.log('创建目录成功!');
})
// 使用fs模块,在目录中创建一个test.txt文件,往里面写入hello world字符串
var fs = require('fs');
var data = 'hello world';
fs.writeFile('fs/test.txt',data,function(err){
    if(err) throw err;
    console.log('写入成功!');
});
// 使用fs模块,复制之前写的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文件的大小和创建时间
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('创建时间:'+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() 读取目录内容
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) 删除文件夹
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Node.js 常用工具 util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScr...
    FTOLsXD阅读 544评论 0 2
  • feisky云计算、虚拟化与Linux技术笔记posts - 1014, comments - 298, trac...
    不排版阅读 3,893评论 0 5
  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 5,801评论 0 10
  • 一、核心模块和对象 核心模块的意义 常用内置模块path:处理文件路径fs:操作文件系统child_process...
    EndEvent阅读 4,434评论 0 1
  • 很早以前,当微信正在蚕食微博的时候,我还在玩着QQ。追时尚,我总是慢人几步,直到身边的朋友都玩着微信了,为赶时髦,...
    海利大人阅读 851评论 0 2