Node.js 文件系统
来源:互联网 发布:苏州智科讯优网络 编辑:程序博客网 时间:2024/05/21 09:43
无标题
Node.js 提供一组类似 UNIX(POSIX)标准的文件操作API。 Node 导入文件系统模块(fs)语法如下所示:
var fs = require("fs")
异步和同步
Node.js 文件系统(fs 模块)模块中的方法均有异步和同步版本,例如读取文件内容的函数有异步的 fs.readFile() 和同步的 fs.readFileSync()。
异步的方法函数最后一个参数为回调函数,回调函数的第一个参数包含了错误信息(error)。
建议大家是用异步方法,比起同步,异步方法性能更高,速度更快,而且没有阻塞。
实例
创建 input.txt 文件,内容如下:
About Node.js?
As an asynchronous event driven framework, Node.js is designed to build scalable network applications. In the following "hello world" example, many connections can be handled concurrently. Upon each connection the callback is fired, but if there is no work to be done Node is sleeping.
const http = require('http');
创建 file.js 文件, 代码如下:
var fs = require("fs");// 异步读取
fs.readFile('input.txt', function (err, data) {if (err) {return console.error(err);}
console.log("异步读取: " + data.toString());});// 同步读取var data = fs.readFileSync('input.txt');
console.log("同步读取: " + data.toString());
console.log("程序执行完毕。");
0 0
- Node.js 文件系统
- node.js之文件系统
- Node.js 文件系统
- Node.js 文件系统
- Node.js 文件系统
- Node.js 文件系统
- Node.js 文件系统
- Node.js 文件系统
- Node.js文件系统fs
- node.js文件系统
- Node.js 文件系统
- node.js中的文件系统
- Node.js 文件系统 fs
- Node.js中的文件系统(FS)
- Node.js学习(10)----文件系统fs
- Node.js v0.12.0API手册--文件系统
- Node.js文件系统、路径的操作函数
- 在Node.js中操作文件系统
- 新浪微博一处反射型XSS(可截获用户登录名及明文密码、可蠕虫、可刷关注)
- 树、森林和二叉树的转换
- Node.js 常用工具
- scrollView ,无限轮播加手势
- Android ScrollView向上滑动控件顶部悬浮效果实现
- Node.js 文件系统
- poj 1085 Triangle War
- 结对编程就是两个人用同一个设计、同一个算法以及同一段代码,并且两人的角色可以随时互换;
- JMeter核心抽象
- eclipse中添加mybatis generator插件教程
- 什么是准双向口,双向口?
- stl 中 set容器用法
- css hack
- springMvc架构流程图及常用注解总结