📜  Node.js fs.writeSync() 方法

📅  最后修改于: 2022-05-13 01:56:44.110000             🧑  作者: Mango

Node.js fs.writeSync() 方法

文件系统模块或 fs 模块是 Node js 中的一个内置模块,用于处理计算机上的文件。模块的功能可以通过导入 fs 模块来使用。 fs 模块可以通过使用文件系统模块的 fs.writeSync()函数包含在程序中,是 write() 方法的同步版本。它可用于将文本和二进制数据写入文件。

句法:

fs.writeSync( fd, string, position, encoding )

要么

fs.writeSync( fd, buffer, offset, length, position )

参数:

  • fd:它代表文件描述符,它是一个标识文件的数字。我们可以使用 fs.openSync() 方法并在其中传递一个描述文件位置的字符串,它将返回一个整数,即文件描述符。
  • 字符串:这是一个将写入文件的字符串。
  • 位置:它指定文件中将写入文本的位置。如果没有在方法中传递位置或者没有使用整数值来指定位置,那么它将从第 0位置开始写入。如果字符串已经写入该位置,则该方法将覆盖在该位置传递的新字符串。
  • encoding:它是一个指定字符编码的字符串。默认为 utf8。
  • 缓冲区:它包含缓冲区类型值,如 Buffer、Typed Array、Data View。
  • offset:它是一个整数值,指定要写入文件的缓冲区部分。
  • 长度:它是一个整数值,指定要写入文件的字节数。

返回值:返回写入的字节数。

例子:

index.js
// Importe fs module
const fs = require("fs");
  
// Create a file input.txt and open it
// using openSync
// The second parameter is the flag 
// which is r+ used for reading and 
// writing onto the file
// An exception occurs if the file does
// not exist.
// The method returns an integer which
// is the file descriptor fd
const fd = fs.openSync("input.txt", "r+");
  
// This text will be written on file input.text
const text = "Welcome to GeeksforGeeks";
  
// Starting position in file
const position = 0;
  
// writeSync returns number of bytes written
// on file which is stores in this variable
const numberOfBytesWritten = 
    fs.writeSync(fd, text, position, 'utf8');
  
console.log('File written successfully using writeSync()');
  
console.log(`Text written on file: ${text},
        starting from position: ${position}`);
  
console.log(`Number of Bytes written: 
        ${numberOfBytesWritten}`);


index.js
// Importing fs module
const fs = require("fs");
  
// open file using openSync in writing mode
// The file is created if it does not exist
// or truncated if it exists
// The method returns an integer which is
// the file descriptor fd
const fd = fs.openSync("binaryFile", "w");
  
  
// Create a buffer which will be written
// onto the file
const buffer = new Buffer.from(
    'GeeksforGeeks: A computer science portal for geeks');
  
// Starting position in file
const position = 0;
  
// writeSync returns number of bytes written
// on file which is stores in this variable
const numberOfBytesWritten = 
    fs.writeSync(fd, buffer, position, 50);
  
console.log('File written successfully using writeSync()');
  
console.log(`Buffer written on file: ${buffer},
            starting from position: ${position}`);
  
console.log(`Number of Bytes written: 
            ${numberOfBytesWritten}`);


使用以下命令运行index.js文件:

node index.js

控制台输出:

File written successfully using writeSync()
Text written on file: Welcome to GeeksforGeeks,starting from position: 0
Number of Bytes written: 24

例子:

index.js

// Importing fs module
const fs = require("fs");
  
// open file using openSync in writing mode
// The file is created if it does not exist
// or truncated if it exists
// The method returns an integer which is
// the file descriptor fd
const fd = fs.openSync("binaryFile", "w");
  
  
// Create a buffer which will be written
// onto the file
const buffer = new Buffer.from(
    'GeeksforGeeks: A computer science portal for geeks');
  
// Starting position in file
const position = 0;
  
// writeSync returns number of bytes written
// on file which is stores in this variable
const numberOfBytesWritten = 
    fs.writeSync(fd, buffer, position, 50);
  
console.log('File written successfully using writeSync()');
  
console.log(`Buffer written on file: ${buffer},
            starting from position: ${position}`);
  
console.log(`Number of Bytes written: 
            ${numberOfBytesWritten}`);

使用以下命令运行index.js文件:

node index.js

控制台输出:

File written successfully using writeSync()
Buffer written on file: GeeksforGeeks: A computer science portal for geeks
starting from position: 0
Number of Bytes written: 50

参考:

  • https://nodejs.org/api/fs.html#fs_fs_writesync_fd_string_position_encoding
  • https://nodejs.org/api/fs.html#fs_fs_writesync_fd_buffer_offset_length_position