[英]write/read contents from a file in Linux FS using java script
I have installed node.js and running a project(with all js files) on Apache Knox. 我已经安装了node.js并在Apache Knox上运行了一个项目(包含所有js文件)。 I need to save some data to a file(HOME/Job_State.txt) in Linux FS and read its contents when needed to do some checksums.
我需要将一些数据保存到Linux FS中的文件(HOME / Job_State.txt)中,并在需要进行一些校验和时读取其内容。 Iam new to js and looking for example code that does this.
我是JS的新手,并正在寻找能做到这一点的示例代码。 Kindly help me by providing some suggestions/pointers.
请提供一些建议/指针,以帮助我。
Have a look at the fs
(File System) module of node.js: 看一下node.js的
fs
(文件系统)模块:
var fs = require('fs');
fs.writeFile('message.txt', 'Hello Node', function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
fs.readFile('message.txt', function (err, data) {
if (err) throw err;
console.log(data);
});
https://nodejs.org/api/fs.html#fs_fs_writefile_filename_data_options_callback https://nodejs.org/api/fs.html#fs_fs_writefile_filename_data_options_callback
https://nodejs.org/api/fs.html#fs_fs_readfile_filename_options_callback https://nodejs.org/api/fs.html#fs_fs_readfile_filename_options_callback
There are synchronous versions too: fs.writeFileSync()
and fs.readFileSync()
也有同步版本:
fs.writeFileSync()
和fs.readFileSync()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.