文件係統模塊或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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
