Which module in nodejs provides file operation API

青灯夜游
Release: 2021-11-10 11:59:43
Original
2622 people have browsed it

In nodejs, the file system module provides file operation API. The file system module (referred to as the fs module) allows users to access and interact with the file system on the computer; using the fs module, the creation, writing, deletion and other operations of files and directories can be achieved.

Which module in nodejs provides file operation API

The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, DELL G3 computer.

1: How to read the entire file content?

Note: Reading files and writing files may be boring to understand, but every time I study such an article, I am ready for the next article, because my next article The demo of the article requires the use of this knowledge, so I will record all the knowledge that needs to be understood.

In Node.js, use the fs module to create, write and delete all files and directories.

1. Method of reading files:

When performing a file reading operation, you can use the readFile method and readFileSync method to read the file. Those with the words Sync and other words are synchronous methods, and those without this word are asynchronous method reading. The difference between synchronous methods and asynchronous methods is: the synchronous method returns the result of the operation immediately. Let’s take a look at the relevant API first:

1) readFileSync reads files synchronously API

The usage method is as follows:

fs.readFileSync(filename, [options]);
Copy after login

filename parameter: used to specify the complete file path and file name of the read file.

options parameter: This value is an object, which can specify the action to be taken on the file using the flag attribute when reading the file. The default is 'r'; the values ​​that can be specified are as follows:

  • 'r': Read the file. If the file does not exist, an exception will be thrown.

  • 'r ': Read and write the file, throwing an exception if the file does not exist.

  • 'rs': Read the file synchronously and notify the operating system to ignore the cache of the local file system. If the file does not exist, an exception will be thrown.

  • 'w': Write to the file. If the file does not exist, create the file. If the file already exists, clear the file content.

  • 'w ': Read and write files, other functions are the same as 'w'.

  • 'a': Append to the file, create the file if it does not exist.

  • 'a ': Read and append to the file, create the file if it does not exist.

In the options parameter, we can use the encoding attribute to specify which encoding format to use to read the file. The attribute values ​​include 'utf-8', 'ascii', 'base64' , let’s take a look at the simple demo first. As shown below:

First look at the directory structure as follows:

### 目录结构如下:
demo1                                       # 工程名
|   |--- readFile                           # 读取文件的文件夹            
|   | |-- index.html                        # html文件
|   | |-- index.js                          # js文件
|   | |-- package.json          
|   | |-- node_modules
Copy after login

The following code reads the file code:

const fs = require('fs');
try {
  const data = fs.readFileSync('./index.html', 'utf-8');
  // 等待操作结果返回,然后打印结果
  console.log(data);
} catch(e) {
  console.log('读取文件发生错误');
}
Copy after login

Execute node index.js in the command line and print the result As follows:

#2) The API usage method for asynchronously reading files is as follows:

fs.readFile(filename, [options], callback);
Copy after login

filename and options are the same as the parameters of the synchronous method.

The callback parameter is the callback function used to be executed after the file is read. As shown in the following code:

const fs = require('fs');
fs.readFile('./index.html', 'utf-8', (err, data) => {
  if (err) {
    console.log('读取文件时发生错误');
  } else {
    console.log(data);
  }
});
Copy after login

The execution result is the same as above. No screenshots here.

2: How to write the entire file content?

To write a file, we use the writeFile method or wirteFileSync method in the fs module. The method of using writeFile is as follows (the wirteFileSync method is similar, except that there is no callback parameter):

fs.writeFile(filename, data, [options], callback);
Copy after login

filename parameter specifies the full path and file name of the file that needs to be written.

data parameter To specify the content to be written, the value can be a string or a Buffer object. The content in the string or buffer will be completely written to the file.

The options value is an object (optional). The option name is flag. It has the following option values:

  • 'flag': used to specify how the file should be used. An operation, the default value is 'w', which means writing to a file. All values ​​of this specified item are the same as the values ​​of readFile above.
    (Create the file if it does not exist, and overwrite the file if it exists).

  • 'mode': This attribute is used to specify the read and write permissions for the file when the file is opened. The default value is 0666 (readable and writable). There is a lot of value that you can learn about on Baidu.

  • 'encoding': Used to specify which encoding format to use to write the file. The attribute values ​​that can be specified are 'utf8', 'ascii', and 'base64'.

  • callback: This parameter is used for the callback function executed when the file reading is completed.

Let’s simply make a demo, write code in the index.js file in our directory, create a message.txt file in the directory and write two lines of text. The following code:

const fs = require('fs');
const str = '这是第一行。\r\n这是第二行';
fs.writeFile('./message.txt', str, (err) => {
  if (err) {
    console.log('写入文件操作失败');
  } else {
    console.log('写入文件操作成功');
  }
});
Copy after login

Execute the command as shown below:

The message.txt file will be generated in the directory, as shown below

然后我们打开文件,会看到其写入的内容。

2)写入buffer对象,如下代码:

const fs = require('fs');
const str = new Buffer('我喜爱编程');
fs.writeFile('./message.txt', str, (err) => {  if (err) {
    console.log('写入文件操作失败');
  } else {
    console.log('写入文件操作成功');
  }
});
Copy after login

在message.txt中可以看到 '我喜爱编程' 几个字。

3)设置options参数flag属性选项,比如追加数据这些,如下代码:

const fs = require('fs');
const options = {
  flag: 'a'};
fs.writeFile('./message.txt', '这是追加的数据', options, (err) => {  if (err) {
    console.log('写入文件操作失败');
  } else {
    console.log('写入文件操作成功');
  }
});
Copy after login

message.txt内容编程如下了:我喜爱编程这是追加的数据

4)复制图片文件

我们可以通过readFile方法读取应用程序对应目录下的一个图片文件,在读取文件时使用base64编码,在该回调函数中使用writeFile方法,在该方法中使用base64编码将读取到的图片文件数据复制到另一个图片文件中。如下代码:

const fs = require('fs');

fs.readFile('./images/1.png', 'base64', (err, data) => {
  fs.writeFile('./images/xx.png', data.toString(), 'base64', (err) => {    if (err) {
      console.log('写文件操作失败');
    } else {
      console.log('写文件操作成功');
    }
  });
});
Copy after login

在image下有一个 1.png, 它会先复制,然后写入 xx.png中(该文件之前不存在的,会创建该图片文件)。

三:如何在文件中的指定位置处读入内容?

3.1)打开文件open方法或openSync方法

要实现在文件中的指定位置处读入内容,首先我们需要打开该文件,fs模块中的open方法或openSync方法打开文件。open方法使用方式如下所示:

fs.open(filename, flags, [mode], callback);
Copy after login

前三个参数和readFile的参数是一个意思的。最后一个参数是执行完成后的回调函数。该回调函数有两个参数,第一个是打开文件失败时的错误对象,第二个参数为一个整数值,代表打开文件时返回的文件描述符。

下面我们来打开我们刚刚创建的 message.txt文件,如下代码:

const fs = require('fs');

fs.open('./message.txt', 'r', (err, fd) => {
  console.log(fd);
});
Copy after login

执行结果如下所示:

3.2)从指定的位置读取文件

如上面打开文件之后,我们可以在其回调函数使用fs模块中的read方法或readSync方法从文件的指定位置处读取文件,也可以使用fs模块中的write方法或writeSync方法从文件中指定处写入数据。

fs.read(fd, buffer, offset, length, position, callback);
Copy after login

fd参数是 上面open打开文件返回的文件描述符。

buffer参数 是一个buffer对象,用于指定将文件数据读取到那个缓存区中。

offset:用于指定向缓冲区中写入数据的开始位置。

length:用于指定从文件中读取的字节数。

position: 用于指定读取文件时的开始位置。

callback:用于指定文件操作完毕时的回调函数。如下所示:

function(err, bytesRead, buffer) {}
Copy after login
  • err: 为操作失败时的错误对象。

  • bytesRead: 代表实际读取的字节数。

  • buffer:为被读取的缓冲区对象。

下面我们来看个demo代码如下:

const fs = require('fs');
// message.txt 内容为:我喜爱编程这是追加的数据
fs.open('./message.txt', 'r', (err, fd) => {
  const buf = new Buffer(255);
  // 一个汉字的utf编码为三个字节数据
  fs.read(fd, buf, 0, 9, 3, (err, bytesRead, buffer) => {
    console.log(buffer.slice(0, bytesRead).toString()); // 喜爱编  });
});
Copy after login

如上代码message.txt内容为:我喜爱编程这是追加的数据,position为3,从第三个字节开始读取文件,然后长度为9个字节,一个汉字3个字节,因此结果为 '喜爱编';

四:如何在文件中的指定位置处写入内容?

在文件被打开后,可以使用fs模块中的write方法或writeSync方法从一个缓存区中读取数据并从文件的指定位置处写入这些数据。使用方法如下所示:

fs.write(fd, buffer, offset, length, position, callback);
Copy after login

fd参数为描述符。

buffer参数为一个Buffer对象,用于指定从哪个缓存区中读取数据。

offset参数用于指定从缓存区中读取数据时的开始读取位置。

length参数用于指定从缓存区中读取的字节数。

position参数值用于指定写入文件时的开始位置。

callback参数用于指定文件写入操作执行完毕时的回调函数。该回调函数如下:

function(err, written, buffer) {}
Copy after login
  • err参数为写入失败时的错误对象。

  • written: 代表被写入的字节数。

  • buffer: 代表被读取的缓冲区对象。

下面是一个简单的demo,如下所示:

const fs = require('fs');

const buf = new Buffer('我喜爱编程');

fs.open('./message.txt', 'w', (err, fd) => {
  fs.write(fd, buf, 3, 9, 0, (err, written, buffer) => {    if (err) {
      console.log('写文件操作失败');
    } else {
      console.log('写文件操作成功');
    }
  });
});
Copy after login

在message.txt中显示为 喜爱编 这三个字。

五:如何创建与读取目录?

5.1) 创建目录mkdir和mkdirSync方法

在fs模块中,可以使用mkdir方法创建目录,该方法使用方式如下所示:

fs.mkdir(path, [mode], callback);
Copy after login
  • path参数用于指定需要被创建的目录完整路径及目录名。

  • mode参数用于指定该目录的权限,默认值为0777(表示任何人可读写该目录)。

  • callback是回调函数。

下面是一个简单的demo,如下所示:

const fs = require('fs');

fs.mkdir('./test', (err) => {  if (err) {
    console.log('创建目录操作失败');
  } else {
    console.log('创建目录操作成功');
  }
});
Copy after login

如下所示:

5.2)读取目录readdir和readdirSync方法

在fs模块中,使用readdir方法读取目录,该方法使用如下所示

fs.readdir(path, callback);
Copy after login

path参数用于指定需要被读取的目录的完整路径及目录名。

callback参数用于指定读取目录操作的回调函数。该回调函数如下所示:

function(err, file) {}
Copy after login
  • err 为读取目录操作失败的回调函数。

  • file参数值为一个数组,读取到的文件中的所有文件名。

如下demo所示:

const fs = require('fs');

fs.readdir('./', (err, files) => {  if (err) {
    console.log('读取目录操作失败');
  } else {
    console.log(files);
  }
});
Copy after login

如下图所示:

六:如何查看与修改文件或目录的信息?

6.1)查看文件或目录的信息

在fs模块中,可以使用stat方法或lstat方法查看一个文件或目录的信息。这两个方法唯一的区别是当查看符号链接文件的信息
时,必须使用lstat方法。使用方法如下所示:

fs.stat(path, callback);
fs.lstat(path, callback);
Copy after login

path参数用于被查看的文件或目录的完整路径及文件名或目录名。

callback是回调函数。如下回调函数

function(err, stats) {}
Copy after login
  • err 参数值为查看文件或目录信息操作失败时触发的错误对象。

  • stats参数值为一个 fs.Stats对象,该对象有如下一些方法,在这些方法中不包含任何参数。

isFile: 用于判断被查看的对象是否为一个文件,如果是则返回true,否则的话返回false。

isDirectory: 用于判断被查看的对象是否为一个目录,如果是的话返回true,否则的话返回false。

还有很多很多其他的方法,这里不一一介绍,用的比较少,有需要的话,可以自己百度一下看看。

下面是使用 stat的使用demo,查看应用程序根目录下的 message.txt文件并且在回调函数中的第二个参数值 fs.Stats对象在控制台中的输出有哪些?

const fs = require('fs');

fs.stat('./message.txt', (err, stats) => {
  console.log(stats);
});
Copy after login

如下图所示显示:

6.2)检查文件或目录是否存在exists和existsSync方法

在fs模块中,可以使用exists方法检查一个文件或目录是否存在,该方法使用方式如下所示:

fs.exists(path, callback);
Copy after login

path参数:用于指定需要被检查的文件或目录的完整路径。
callback: 是回调函数,该回调函数如下所示:

function(exists) {}
Copy after login
  • exists参数,当文件或目录存在时,该参数值为true,否则的话,该参数值为false。

下面是一个简单的demo,如下代码所示:

const fs = require('fs');

// 该目录是存在的
fs.exists('./message.txt', (exists) => {
  console.log(exists); // true
});

// 该目录是不存在的
fs.exists('./message2.txt', (exists) => {
  console.log(exists); // false
});
Copy after login

6.3) 获取文件或目录的绝对路径realpath和realpathSync方法

在fs模块中,可以使用 realpath方法获取一个文件或目录的绝对路径,该方法使用如下所示:

fs.realpath(path, [cache], callback);
Copy after login
  • cache 参数是可选的,path和callback是必须的。

  • path参数为需要查看的文件或目录的完整路径。

  • cache参数为一个对象,其中存放了一些预先指定的路径。具体的百度下,这个参数用的不多。

  • callback是回调函数,该回调函数有2个参数,err参数值为获取目录的绝对路径失败的错误对象。resolvedPath参数值为获取
    到的文件或目录的绝对路径。

下面是一个简单的demo,如下所示:

const fs = require('fs');

fs.realpath('./message.txt', (err, resolvedPath) => {  if (err) {    throw err;
  } else {
    console.log(resolvedPath);
  }
});
Copy after login

执行如下所示:

6.4) 使用ReadStream对象读取文件

在fs模块中,可以使用createReadStream方法创建一个将文件内容读取为流数据的ReadStream对象,该方法使用如下所示:

fs.createReadStream(path, [options]);
Copy after login

path 该参数用于指定需要被读取的文件的完整路径及文件名。

options为一个对象,它有如下属性:

  • flags: 用于指定对该文件采取什么操作,默认值为 'r', 它的用法和readFile方法中的flags属性一样的。

  • encoding: 指定使用什么编码来读取该文件,可指定的值有 'utf8', 'ascii', 'base64'. 默认值为null.

  • start: 指定文件的开始读取位置。

  • end: 指定文件的结束读取位置。

还有很多其他的参数,这里不一一讲解,可以自行百度下即可。

下面简单的使用一个demo来了解下使用:如下代码:

const fs = require('fs');/*
 一个汉字三个字节,message.txt的内容为:我喜爱编写代码
 因此从第三个位置开始,到第十二个位置结束,因此数据应该为 喜爱编写*/const options = {
  start: 3, 
  end: 12};
const file = fs.createReadStream('./message.txt', options);

file.on('open', (fd) => {
  console.log('开始读取文件');
});

file.on('data', (data) => {
  console.log('读取到的数据:' + data);
});

file.on('end', () => {
  console.log('文件已全部读取完毕');
});

file.on('close', () => {
  console.log('文件被关闭');
});

file.on('error', (err) => {
  console.log('读取文件失败');
});
Copy after login

如下图所示:

我们可以使用 ReadStream对象的pause方法暂停data事件的触发,同时也意味着停止文件的读取操作。而已经被读取到的操作系统缓存区中的数据也将被暂时保存在操作系统缓冲区中,在使用了pause方法暂停data事件的触发之后,也可以使用ReadStream对象的resume方法恢复data事件的触发,也就意味着可以继续读取文件的数据。

如下demo:

const fs = require('fs');

const readStream = fs.createReadStream('./message.txt');

readStream.pause();

readStream.on('data', (data) => {
  console.log('获取到的数据为:' +data);
});

setTimeout(() => {
  readStream.resume();
}, 1000);
Copy after login

读取过程中,断听了一秒后,继续把数据读出来了。

注意:写入文件的方法是 WriteStream, 使用方式和读方式类似,这里不多介绍。

【推荐学习:《nodejs 教程》】

The above is the detailed content of Which module in nodejs provides file operation API. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!