文件系统模块或fs模块是Node js中的内置模块,用于处理计算机上的文件。可以通过导入fs模块来使用该模块的函数。通过使用fs可以将fs模块包括在程序中。文件系统模块的writeSync()函数是write()方法的同步版本。它可以用于将文本以及二进制数据写入文件。
用法:
fs.writeSync( fd, string, position, encoding )
或者
fs.writeSync( fd, buffer, offset, length, position )
参数:
- fd:它代表文件描述符,它是标识文件的数字。我们可以使用fs.openSync()方法,并在其中传递一个描述文件位置的字符串,它将返回一个整数,即文件描述符。
- string:这是一个将写入文件的字符串。
- position:它指定文件在其中要写入文本的位置。如果未在方法中传递位置或未使用整数值指定位置,则它将从0开始写入th位置。如果已经在该位置写入了字符串,则该方法将覆盖在该位置传递的新字符串。
- encoding:它是指定字符编码的字符串。默认情况下是utf8。
- buffer:它包含缓冲区类型值,例如Buffer,Typed Array,Data View。
- offset:它是一个整数值,指定要写入文件的缓冲区部分。
- length:它是一个整数值,用于指定要写入文件的字节数。
返回值:返回写入的字节数。
例:
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文件:
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
相关用法
- Node.js console.timeLog()用法及代码示例
- Node.js fs.fsyncSync()用法及代码示例
- Node.js GM charcoal()用法及代码示例
- Node.js GM blur()用法及代码示例
- Node.js GM sharpen()用法及代码示例
- Node.js GM drawLine()用法及代码示例
- Node.js GM drawArc()用法及代码示例
- Node.js GM drawPolyline()用法及代码示例
注:本文由纯净天空筛选整理自pritishnagpal大神的英文原创作品 Node.js fs.writeSync() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。