zoukankan      html  css  js  c++  java
  • nodejs fs 模块的用途

    /**
    * New node file
    fs 操作
    */
    var fs = require(“fs”);

    /*
    创建文件

    var fileName = “anps_hsj”;
    fs.mkdir(fileName,function(err){
    if(err) throw err
    console.log(“创建文件成功”);
    fs.exists( fileName ,function(exists){
    console.log(exists ? “文件存在了,不能重新创建了” : “文件不存在,文件创建失败了”);
    });
    });
    */

    /*
    写入内容到文件中,不会追加,如果文件没有即会立即创建

    var fileName = “anps.txt”;
    var content = “hello word ”;
    fs.writeFile(fileName,content,function(err){
    if(err) throw err
    console.log(“success”);
    });
    */

    /*
    写入内容到文件中,会追加,如果文件没有即会立即创建
    var fileName = “anps.txt”;
    var content = “hello word ”;
    fs.appendFile(fileName,content,function(err){
    if(err) throw err
    console.log(“success”);
    });
    */

    /*
    文件删除
    fs.unlink(“Copy of anps.txt”, function(err){
    fs.exists(“Copy of anps.txt”, function(exists){
    console.log(exists ? “删除失败” : “删除成功”);
    });
    });
    */

    /*
    文件读取
    fs.readFile(“anps.txt”, function(err,data){
    if(err) throw err
    console.log(data.toString());
    });
    */

    /*
    读取目录

    fs.readdir(“E:\2015_WorkSpace\nodejsPro\anps_hsj”, function(err,files){
    for(var i = 0 ; i < files.length; i ++){ console.log(files[i]); // 文件名称 var fileName = files[i]; fs.readFile(“E:\2015_WorkSpace\nodejsPro\anps_hsj\”+files[i], function(err,data){ if(err) throw err console.log(fileName+”文本内容是:”+data.toString()); }); } }); */

  • 相关阅读:
    左值与右值引用 详解
    MFC---导出 Excel 方法
    Linux怎么读? Linux读音考古一日游
    nginx url自动加斜杠问题
    FileBeats配置应用详解
    nginx配置选项try_files详解
    mongodb副本集集群构建
    平凡主丛上的Yang-Mills理论
    Kneser猜想与相关推广
    Lorenzini:Laplacian与图上的黎曼-罗赫定理
  • 原文地址:https://www.cnblogs.com/adjk/p/4936753.html
Copyright © 2011-2022 走看看