當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js fs.filehandle.truncate()用法及代碼示例

fs.filehandle.truncate()方法是文件係統模塊中fs.filehandle類的內置應用程序編程接口,用於截斷特定文件對象,並且僅保留那麽多的字節,該字節作為整數在truncate()方法中傳遞。

用法:

const filehandle.truncate(len)

參數:此方法占用此文件必須被截斷的字節長度。

返回值:此方法返回未包含的承諾。

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



範例1: 文件名:index.js

// Node.js program to demonstrate the  
// filehandle.truncate() method 
const fs = require('fs'); 
const fsPromises = fs.promises; 
  
// Initiating asyncrionise function 
async function funct() { 
  
    // Initializng following variables 
    let filehandle = null; 
    let prom = null; 
  
    try { 
  
        // Data before operation 
        console.log("data before operation:" + 
            fs.readFileSync('example.txt', 'utf8')); 
  
        // Creating and initiating  filehandle 
        filehandle = await  
            fsPromises.open('example.txt', 'r+'); 
  
        // Truncating the file 
        // by using truncate() method 
        prom = filehandle.truncate(5); 
  
    } finally { 
  
        if (filehandle) { 
  
            // Data after operation 
            console.log("data after operation:" + 
                fs.readFileSync('example.txt', 'utf8')); 
  
            // Close the file if it is opened. 
            await filehandle.close(); 
        } 
    } 
} 
  
funct().catch(console.error);

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

node index.js

輸出:

data before operation:ABCDEFGHIJK
data after operation:ABCDE

範例2: 文件名:index.js

// Node.js program to demonstrate the  
// filehandle.truncate() 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; 
  
    try { 
      
        // Creating and initiating filehandle 
        filehandle = await fsPromises.open(file, 'r+'); 
  
        // Truncating the file 
        // by using truncate() method 
        prom = filehandle.truncate(4); 
  
    } finally { 
  
        if (filehandle) { 
  
            // Close the file if it is opened. 
            console.log( 
                "content of file after operation:"
                + (fs.readFileSync(file))); 
  
            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.
content of file after operation:This

注意:以上程序無法在在線JavaScript和腳本編輯器上運行。

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




相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js fs.filehandle.truncate() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。