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


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


fs.filehandle.chmod()方法是“文件系统”模块中fs.filehandle类的内置应用程序编程接口,用于更改特定文件的权限。

用法:

const filehandle.chmod( mode )

参数:此方法接受包含整数值的单参数模式。

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

下面的程序演示了fs.filehandle.chmod()方法的使用。



范例1: 文件名:index.js

// Node.js program to demonstrate the  
// fs.filehandle.chmod() method 
const fs = require('fs'); 
const fsPromises = fs.promises; 
  
console.log("content of file beofre "
        + "operation:- " + 
        (fs.readFileSync('example.txt'))); 
  
// Initiating asyncrionise function 
async function funct() { 
  
    // Initializng filehandle 
    let filehandle = null; 
  
    try { 
  
        // Creating and initiating filehandle 
        filehandle = await fsPromises 
                .open('example.txt', 'r+'); 
  
        // Modyifying the file permission 
        // by using chmod() method 
        const prom = filehandle.chmod(1); 
  
    } finally { 
  
        if (filehandle) { 
  
            // Close the file if it is opened. 
            console.log("permission is changed"
                        + " to read only"); 
  
            console.log("content of file beofre"
                    + " operation:- " + 
                (fs.readFileSync('example.txt'))); 
  
            await filehandle.close(); 
        } 
    } 
} 
  
funct().catch(console.error);

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

运行程序后的目录结构:

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

node index.js

输出:

content of file before operation:- Content of example.txt file
permission is changed to read only
content of file after operation:- Content of example.txt file

范例2: 文件名:index.js

// Node.js program to demonstrate the  
// fs.filehandle.chmod() 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+'); 
  
        // Modyifying the file permission 
        // by using chmod() method 
        const prom = filehandle.chmod(1); 
  
    } finally { 
  
        if (filehandle) { 
  
            // Close the file if it is opened. 
            console.log("permission is "
                + "changed to read only"); 
                  
            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.
permission is changed to read only
content of file after operation:- This is a file containing a collection of books.

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




相关用法


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