700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > nodejs fs创建文件_节点FS – NodeJS创建文件 读取文件 写入文件

nodejs fs创建文件_节点FS – NodeJS创建文件 读取文件 写入文件

时间:2020-09-05 12:08:11

相关推荐

nodejs fs创建文件_节点FS – NodeJS创建文件 读取文件 写入文件

nodejs fs创建文件

Node FS stands for NodeJS File System module. In my previous post, we have already discussed about how to import a Node JS module usingrequire()call. Before reading this post, please go through this post “Node JS Export and Import Modules” to know require() call usage.

Node FS代表NodeJS File System模块。 在我之前的文章中,我们已经讨论了如何使用require()调用导入Node JS模块。 在阅读这篇文章之前,请仔细阅读这篇文章“ Node JS Export and Import Modules ”,以了解require()调用的用法。

节点FS (Node FS)

In this post, we are going to discuss about Node JS Platform “fs” module. FS Stands forFile System. This module is also known asIO or FileSystem or Stream module.

在本文中,我们将讨论Node JS平台的“ fs”模块。 FS代表文件系统。 此模块也称为IO或FileSystem或Stream模块

NodeJS FS模块发布简介 Node FS模块简介 节点JS创建文件 节点JS写入文件 节点JS读取文件

Node FS模块简介

(NodeJS FS Module Post BriefIntroduction to Node FS ModuleNode JS Create FileNode JS Write to FileNode JS Read File

Introduction to Node FS Module

)

Node FS Module provides an API to interact with File System and to perform some IO Operations like create file, read File, delete file, update file etc.

节点FS模块提供了一个API与文件系统交互并执行一些IO操作,例如创建文件,读取文件,删除文件,更新文件等。

Like some Node modules for example “npm”, “http” etc, Node JS “fs” also comes with basic Node JS Platform. We don’t need to do anything to setup Node JS FS module.

像某些节点模块(例如“ npm”,“ http”等)一样,节点JS“ fs”也带有基本的节点JS平台。 我们无需执行任何操作即可设置Node JS FS模块。

节点FS模块导入 (Node FS Module import)

We just need to import node fs module into our code and start writing IO Operations code.

我们只需要将节点fs模块导入我们的代码中,然后开始编写IO Operations代码即可。

To import a node fs module;

导入节点fs模块;

var fs = require("fs");

This require() call imports Node JS “fs” module into cache and creates an object of type Node FS module. Once it’s done, we can perform any IO Operation using node fs object.

此require()调用将Node JS“ fs”模块导入缓存,并创建Node FS模块类型的对象。 完成后,我们可以使用节点fs对象执行任何IO操作。

Let us assume that our${Eclipse_Workspace}refers toD:\RamWorkspaces\NodeWorkSpace.

让我们假设我们的${Eclipse_Workspace}引用D:\RamWorkspaces\NodeWorkSpace

Now onwards, I’m going to use this variable to refer my Eclipse workspace.

现在开始,我将使用此变量来引用我的Eclipse工作区。

As a Java or DOT NET or C/C++ developers, we have already learned and wrote some IO Programs.

作为Java或DOT NET或C / C ++开发人员,我们已经学习并编写了一些IO程序。

IO or Streams are two types:

IO或流是两种类型:

Write Stream – To write data to a Stream.写入流–将数据写入流。 Read Stream – To read data from a Stream.读取流–从流中读取数据。

节点JS创建文件 (Node JS Create File)

Now we will discuss about how to create a new file using Node JS FS API.

现在,我们将讨论如何使用Node JS FS API创建新文件。

Create a Node JS Project in Eclipse IDE.

在Eclipse IDE中创建一个Node JS项目。

Copypackage.jsonfile from previous examples and update the required things.

{"name": "filesystem","version": "1.0.0","description": "File System Example","main": "filesystem","author": "JournalDEV","engines":{"node":"*"}}

复制先前示例中的package.json文件,并更新所需的内容。 Create a JavaScript file with the following content;

fs-create-file.js

/*** Node FS Example* Node JS Create File*/var fs = require("fs");var createStream = fs.createWriteStream("JournalDEV.txt");createStream.end();

Code Description:

var fs = require("fs")require() call loads specified Node FS module into cache and assign that to an object named as fs.

fs.createWriteStream(filename)call is used to create a Write Stream and file with given filename.

createStream.end()call ends or closes the opened stream.

fs-create-file.js

代码说明

var fs = require("fs")require()调用将指定的Node FS模块加载到缓存中,并将其分配给名为fs的对象。

fs.createWriteStream(filename)调用用于创建具有给定文件名的Write Stream和文件。

createStream.end()调用结束或关闭打开的流。

Before executingfs-create-file.js, first observe the filesystem project content and you will notice that “JournalDEV.txt” file is not available.在执行fs-create-file.js,首先观察文件系统项目的内容,您会注意到“ JournalDEV.txt”文件不可用。 Open command prompt at${Eclipse_Workspace}/filesystemand run node commend to executefs-create-file.jsfile as shown in below image.

Notice your project directory contents now, you will notice an empty file named “JournalDEV.txt”.

${Eclipse_Workspace}/filesystem打开命令提示符,并运行命令${Eclipse_Workspace}/filesystem执行fs-create-file.js文件,如下图所示。

现在注意项目目录的内容,您会注意到一个名为“ JournalDEV.txt”的空文件。

节点JS写入文件 (Node JS Write to File)

We will use Node FS API to create a new file and write some data into that. It is continuation to our previous example.

我们将使用Node FS API创建一个新文件,并将一些数据写入其中。 它是我们先前示例的延续。

Remove previously created “JournalDEV.txt” from ${Eclipse_Workspace}/filesystem folder从$ {Eclipse_Workspace} / filesystem文件夹中删除先前创建的“ JournalDEV.txt” Create a Java Script file with the following content:

fs-write-file.js

/*** Node FS Example* Node JS Write to File*/var fs = require("fs");var writeStream = fs.createWriteStream("JournalDEV.txt");writeStream.write("Hi, JournalDEV Users. ");writeStream.write("Thank You.");writeStream.end();

createStream.write(sometext)call is used to write some text to a file.

fs-write-file.js

createStream.write(sometext)调用用于将一些文本写入文件。

Open command prompt at${Eclipse_Workspace}/filesystemand run node commend to executefs-write-file.jsfile as shown below.

${Eclipse_Workspace}/filesystem处打开命令提示符,并运行命令${Eclipse_Workspace}/filesystem执行fs-write-file.js文件,如下所示。

Go to ${Eclipse_Workspace}/filesystem folder and open “JournalDEV.txt” to verify its content.

Now we have created a new file and write some data into that file.

现在,我们创建了一个新文件,并将一些数据写入该文件。

节点JS读取文件 (Node JS Read File)

We will use Node FS API to open and read an existing file content and write that content to the console. It is continuation to our previous examples.

我们将使用Node FS API打开和读取现有文件内容,并将该内容写入控制台。 它是我们先前示例的延续。

Here we are going to use named JavaScript function. Go through this example to understand this.

在这里,我们将使用命名JavaScript函数。 通过这个例子来理解这一点。

Create a Java Script file with the following content.

fs-read-file1.js

/*** Node FS Read File* Node JS Read File*/var fs = require("fs");function readData(err, data) {console.log(data);}fs.readFile('JournalDEV.txt', 'utf8', readData);

Code Description:

readData()is a JavaScript function which takes two parameters;

err: it’s an error object. When our program fails to open or read data from a file, then FS module writes some error message into this parameter.data: it’s a variable to hold some data.

This function takes data parameter and prints that data to a console.

fs.readFile()is Node JS FS API. It takes three parameters;

file namefile data format to readA JavaScript function or JavaScript anonymous function

readFile() reads data from a filename (first parameter) in given format (second parameter) and uses function given at third parameter to do some operation.

Here we are using a plain JavaScript function as the third Parameter. We will see how to use JavaScript anonymous function in next example.

In our example, readFile() reads data from “JournalDEV.txt” file and writes to console.

If we don’t use ‘utf8’ format, then we will get binary data. We will verify this in few moments.

fs-read-file1.js

代码说明

readData()是一个JavaScript函数,带有两个参数;

err:这是一个错误对象。 当我们的程序无法打开或无法从文件读取数据时,FS模块将一些错误消息写入此参数。data:它是保存一些数据的变量。

此函数采用data参数并将该数据打印到控制台。

fs.readFile()是Node JS FS API。 它包含三个参数;

文档名称 文件数据格式读取 JavaScript函数或JavaScript匿名函数

readFile()从文件名(第一个参数)以给定格式(第二个参数)读取数据,并使用第三个参数给定的函数执行某些操作。

在这里,我们使用普通JavaScript函数作为第三个参数。 在下一个示例中,我们将看到如何使用JavaScript匿名函数。

在我们的示例中,readFile()从“ JournalDEV.txt”文件读取数据并写入控制台。

如果我们不使用'utf8'格式,那么我们将获得二进制数据。 我们将在稍后验证。

Open command prompt at ${Eclipse_Workspace}/filesystem and run node commend to execute fs-read-file1.js file.

Now we have observed that our code has successfully open a file, reads its content and writes its content to the console.

现在,我们已经观察到我们的代码已成功打开文件,读取其内容并将其内容写入控制台。

Node.js以二进制形式读取文件 (Node.js read file as binary)

Now we are going to use JavaScript anonymous function to read data from a file. Go through this example to understand this. It’s similar to previous fs-read-file1.js example only but with anonymous function. If you are not familiar with JavaScript anonymous functions, please go through some JavaScript tutorial and get some idea.

现在,我们将使用JavaScript匿名函数从文件中读取数据。 通过这个例子来理解这一点。 它仅与先前的fs-read-file1.js示例相似,但具有匿名功能。 如果您不熟悉JavaScript匿名函数,请阅读一些JavaScript教程并有所了解。

Create a Java Script file with the following content;

创建具有以下内容的Java脚本文件;

fs-read-file2.js

fs-read-file2.js

/*** Node FS File System Module* Node.js read file example*/var fs = require("fs");fs.readFile('JournalDEV.txt', 'utf8', function(err, data) {console.log(data);});

Code Description:

代码说明

HerereadFile()is using JavaScript anonymous function to read data from a file and write that file content to the console.

在这里,readFile()使用JavaScript匿名函数从文件中读取数据并将该文件内容写入控制台。

When we run this file, we will get same output as fs-read-file2.js example.

运行此文件时,将得到与fs-read-file2.js示例相同的输出。

Now remove “utf8” data format to see binary output.

现在删除“ utf8”数据格式以查看二进制输出。

/*** Node FileSystem Module * Node JS Read File Binary Data*/var fs = require("fs");fs.readFile('JournalDEV.txt', function(err, data) {console.log(data);});

Execute above file and observe the output as shown in below image.

执行以上文件,并观察输出,如下图所示。

Bonus TIP: To learn Node JS FS API in depth, please useEnide Studioand know all available functions and usage as shown below.

温馨提示:要深入学习Node JS FS API,请使用Enide Studio并了解所有可用功能和用法,如下所示。

[anyFSAPIObject] + Press . (dot) + After dot Press (CTRL + Space Bar)

Now we are familiar with Node FS module. We are going to use this knowledge in next posts, especially in HTTP Module post.

现在我们熟悉了Node FS模块。 我们将在下一篇文章中,特别是在HTTP Module文章中使用这些知识。

Reference: Official Documentation

参考: 官方文档

翻译自: /7821/node-fs-js-create-file-read-write

nodejs fs创建文件

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。