美文网首页
2019-06-22 fs模块

2019-06-22 fs模块

作者: 门门_ | 来源:发表于2019-06-21 22:36 被阅读0次

一、同步实现

// 使用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) 删除文件夹

相关文章

  • 2019-06-22 fs模块

    一、同步实现 // 使用fs模块,创建一个目录fs// 使用fs模块,在目录中创建一个test.txt文件,往里面...

  • Node基础备注

    模块 http模块 url模块 fs模块 fs.state:检测是文件还是目录fs.mkdir:创建目录fs.wr...

  • node栈-第三方工具-文件类

    fs-extra fs-extra模块是系统fs模块的扩展,提供了更多便利的 API,并继承了fs模块的 API ...

  • node.js文件操作

    fs 文件操作用的模块为node.js中的fs模块.因此要提前将其引入. var fs = require('fs...

  • fs的核心模块及方法

    fs的核心模块及方法 fs模块(http://nodejs.cn/api/)

  • 周国康-20160809笔记

    HTTP,URL,FS模块 HTTP模块 FS模块 URL模块 作业 构造静态服务Server:解析URL,根据U...

  • 八:Node文件系统管理

    导入:var fs = require("fs") 定义:Node.js 文件系统(fs 模块)模块中的方法均有异...

  • nodejs学习笔记

    参考 模块化 内置模块文件管理(fs)fs.readdirSync() //同步读取文件夹fs.readir() ...

  • node中的内置模块fs

    fs文件系统操作模块 注:fs 模块=>包括文件目录的创建、删除、查询以及文件的读取、写入等; 在 fs 模块中,...

  • 关于node.js一些模块的记录「FS模块」

    目录 Node.JS教程 FS模块 Path模块 FS模块 Path模块 Node.js path 模块提供了一些...

网友评论

      本文标题:2019-06-22 fs模块

      本文链接:https://www.haomeiwen.com/subject/zxyyqctx.html