当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js fs.filehandle.write()用法及代码示例


fs.filehandle.write()方法是文件系统模块中fs.filehandle类的内置应用程序编程接口,用于将缓冲区中的数据写入该特定文件。

用法:

const filehandle.write(buffer[, offset[, length[, position]]])

参数:此方法确实采用以下参数:

  • buffer:缓冲区将提供需要存储的数据。
  • offset:缓冲区中的起点。
  • length:要写入的字节数。
  • position:文件的起点。

返回值:此方法返回一个未决的Promise,其中包含buffer和bytesWritten属性,该属性描述写入的字节数。

以下示例程序旨在说明Node.js中fs.filehandle.write()方法的使用:



范例1: 文件名:index.js

// Node.js program to demonstrate the  
// filehandle.write() method 
const fs = require('fs'); 
const fsPromises = fs.promises; 
  
console.log("file before write operation:- "
    + fs.readFileSync('example.txt', 'utf8')); 
  
// Initiating asyncrionise function 
async function funct() { 
  
    // Initializng following variables 
    let filehandle = null; 
    let prom = null; 
    let buffer = Buffer.from('Geeks'); 
  
    try { 
  
        // Creating and initiating filehandle 
        filehandle = await  
            fsPromises.open('example.txt', 'r+'); 
  
        // Writting the file by using 
        // write() method 
        prom = filehandle.write( 
                buffer, 0, buffer.length, 0); 
  
    } finally { 
  
        if (filehandle) { 
  
            // Display the result 
            prom.then(function (result) { 
                console.log( 
                    "file after write operation:- "
                    + (result.buffer).toString()); 
            }) 
  
            // Close the file if it is opened. 
            await filehandle.close(); 
        } 
    } 
} 
  
funct().catch(console.error);

运行程序之前的目录结构:

运行程序后的目录结构:

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

node index.js

输出:

file before write operation:- Geeks for Geeks
file after write operation:- Geeks

范例2: 文件名:index.js

// Node.js program to demonstrate the  
// filehandle.sync() method 
const fs = require('fs'); 
const fsPromises = fs.promises; 
  
// Data for the new file 
let data = "This is a file containing"
        + " a collection of books."; 
  
// Name of the file to be created 
let file = "books.txt"; 
  
// Creating the new file 'books.txt' 
fs.writeFile(file, data, (err) => { 
  
    // Cathing error 
    if (err) { 
        console.log(err); 
    } 
}); 
  
// Using fs.exists() method  
fs.exists(file, (exists) => { 
    if (exists) { 
        console.log( 
            "content of file before operation:- "
            + (fs.readFileSync(file))); 
    } 
}); 
  
// Initiating asyncrionise function 
async function funct() { 
  
    // Initializng filehandle 
    let filehandle = null; 
  
    // Initializng following variables 
    let buffer = Buffer.from('ABCD'); 
  
    try { 
  
        // Creating and initiating  filehandle 
        filehandle = await  
            fsPromises.open(file, 'r+'); 
  
        // Writting the file 
        // by using write() method 
        prom = filehandle.write( 
            buffer, 0, buffer.length, 0); 
  
    } finally { 
  
        if (filehandle) { 
  
            // Display the result 
            prom.then(function (result) { 
                console.log( 
                    "file after write operation:- "
                    + (result.buffer).toString()); 
            }) 
  
            await filehandle.close(); 
        } 
    } 
} 
  
funct().catch(console.error);

运行程序之前的目录结构:

运行程序后的目录结构:

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

node index.js

输出:

content of file before operation:- 
  This is a file containing a collection of books.
file after write operation:- ABCD

参考: https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_filehandle_write_buffer_offset_length_position




相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Node.js fs.filehandle.write() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。