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 using require() 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 for File System. This module is also known as IO or FileSystem or Stream module.

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

NodeJS FS模块发布简介

  1. Node FS模块简介
  2. 节点JS创建文件
  3. 节点JS写入文件
  4. 节点JS读取文件

Node FS模块简介

(NodeJS FS Module Post Brief

  1. Introduction to Node FS Module
  2. Node JS Create File
  3. Node JS Write to File
  4. Node 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 to D:\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创建新文件。

  1. Create a Node JS Project in Eclipse IDE.

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

  2. Copy package.json file 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文件,并更新所需的内容。

  3. 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()调用结束或关闭打开的流。

  4. Before executing fs-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”文件不可用。
  5. Open command prompt at ${Eclipse_Workspace}/filesystem and run node commend to execute fs-create-file.js file 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创建一个新文件,并将一些数据写入其中。 它是我们先前示例的延续。

  1. Remove previously created “JournalDEV.txt” from ${Eclipse_Workspace}/filesystem folder从$ {Eclipse_Workspace} / filesystem文件夹中删除先前创建的“ JournalDEV.txt”
  2. 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)调用用于将一些文本写入文件。

  3. Open command prompt at ${Eclipse_Workspace}/filesystem and run node commend to execute fs-write-file.js file as shown below.

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

  4. 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函数。 通过这个例子来理解这一点。

  1. 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;

    1. 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.
    2. 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;

    1. file name
    2. file data format to read
    3. A 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函数,带有两个参数;

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

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

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

    1. 文档名称
    2. 文件数据格式读取
    3. JavaScript函数或JavaScript匿名函数

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

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

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

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

  2. 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:

代码说明

Here readFile() 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 use Enide 2014 Studio and know all available functions and usage as shown below.

温馨 提示 :要深入学习Node JS FS API,请使用Enide 2014 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

参考: 官方文档

翻译自: https://www.journaldev.com/7821/node-fs-js-create-file-read-write

nodejs fs创建文件

nodejs fs创建文件_节点FS – NodeJS创建文件,读取文件,写入文件相关推荐

  1. nodejs:fs (内置模块)读取和写入文件

    node fs (内置模块)读取和写入文件 const fs = require("fs"); //异步读取文件 fs.readFile("data.txt", ...

  2. 数据库创建函数_达梦数据库创建UUID函数

    数据库创建函数_达梦数据库创建UUID函数 接触达梦数据库有一段时间了,整理了一些资料,今天分享一下达梦数据UUID自定义函数 UUID函数定义 很多数据库都有提供UUID函数,可是接触达梦数据库后, ...

  3. go语言逐行读取和写入文件

    前言 前面一篇博客讲到nodejs使用readline逐行读取和写入文件 今天使用go语言实现从输入文件中读取每行数据,然后将每行字段组合成SQL插入脚本,然后逐行写入另外一个空白文件中. tb_pa ...

  4. 读取和写入文件的最简单方法

    本文翻译自:Easiest way to read from and write to files There are a lot of different ways to read and writ ...

  5. HttpWebRequest FileStream分块读取和写入文件WebClient

    //HttpWebRequest  下载文件 private void DownloadFile(string filePath)           {               string[] ...

  6. Flutter进阶—读取与写入文件

    Flutter使用path_provider插件读取与写入文件,path_provider插件提供了一种平台无关的方法来访问设备文件系统上常用的位置.该类目前支持访问两个文件系统位置: 临时目录:系统 ...

  7. python读取写入文件_Python读取和写入文件

    1 从文件中读取数据 1.1 读取整个文件 创建名为test的txt文本文件,添加内容如下所示: 1234567890 2345678901 3456789012 实现代码: with open('t ...

  8. sublime python3中读取和写入文件时如何解决编码问题

    sublime python3中读取和写入文件时如何解决编码问题 参考文章: (1)sublime python3中读取和写入文件时如何解决编码问题 (2)https://www.cnblogs.co ...

  9. java写入文件编码格式为ansi_Java读取、写入文件如何解决乱码问题

    读取文件流时,经常会遇到乱码的现象,造成乱码的原因当然不可能是一个,这里主要介绍因为文件编码格式而导致的乱码的问题.首先,明确一点,文本文件与二进制文件的概念与差异. 文本文件是基于字符编码的文件,常 ...

最新文章

  1. tomcat 修改默认字符集
  2. Ubuntu 迁移 /tmp 到别的硬盘
  3. pip 和 conda 源更改
  4. 五、Hive数据类型和简单使用
  5. mysql generaton_Mysql 集成随机唯一id mysql unique number generation
  6. Datawhale MySQL 训练营 Task2 查询语句
  7. mysql 横向扩展 中间件_mysql-proxy数据库中间件架构 | 架构师之路
  8. NullPointerException的处理新方式,Java14真的太香了
  9. ftp服务器生成文件,ftp服务器自动生成文件
  10. cent mysql 配置,centos下MySQL安装配置
  11. string.Format字符串格式化说明
  12. 基于局域网快速传输文件
  13. 软件安装包制作工具installshield 2020 R1的安装教程
  14. 南航理论计算机科学答案,专业认证理念下的计算机专业本科生培养管理模式研究——以南航计算机科学与技术专业为例...
  15. POI复制Excel模板并填充数据
  16. CPT CPC CPA CPM 广告投放了解
  17. 引入 ECharts和Bootstrap的bootcdn样式表路径
  18. 生成划掉的字_哪种备忘录划删除线,能划掉文字在字中间划线的便签
  19. STC89C51单片机
  20. 如何成为有效学习的高手 学习笔记

热门文章

  1. 回调函数 EnumFontFamProc
  2. eclipse 下安装插件
  3. 不能不说的C#特性-表达式树
  4. [转载] Python中关于字符串的使用演示
  5. [转载] 学Python的笔记(在网上自学的总结)
  6. 初次使用uwsgi:no python application found, check your startup logs for errors
  7. HDU4686——Arc of Dream矩阵快速幂
  8. [ZT]CCNA课堂精简笔记 2
  9. Linux安装卸载Mysql数据库
  10. Automatic Brain Tumor Segmentation using Cascaded Anisotropic Convolutional Neural Networks