fs.mkdtemp(prefix[, options], callback)
历史
版本 | 变化 |
---|---|
v18.0.0 | 将无效回调传递给 |
v16.5.0、v14.18.0 |
|
v10.0.0 |
|
v7.0.0 |
|
v6.2.1 |
|
v5.10.0 | 添加于:v5.10.0 |
参数
prefix
<string>options
<string>|<Object>encoding
<string> 默认:'utf8'
callback
<Function>
创建一个唯一的临时目录。
生成六个随机字符,附加在所需的prefix
后面,以创建唯一的临时目录。由于平台不一致,请避免在 prefix
中使用尾随 X
字符。一些平台,尤其是 BSD,可以返回六个以上的随机字符,并将 prefix
中的尾随 X
字符替换为随机字符。
创建的目录路径作为字符串传递给回调的第二个参数。
可选的 options
参数可以是指定编码的字符串,也可以是具有指定要使用的字符编码的 encoding
属性的对象。
import { mkdtemp } from 'node:fs';
mkdtemp(path.join(os.tmpdir(), 'foo-'), (err, directory) => {
if (err) throw err;
console.log(directory);
// Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2
});
fs.mkdtemp()
方法会将六个随机选择的字符直接附加到prefix
字符串中。例如,给定一个目录 /tmp
,如果打算在 /tmp
中创建一个临时目录,则 prefix
必须以尾随平台特定的路径分隔符 (require('node:path').sep
) 结尾。
import { tmpdir } from 'node:os';
import { mkdtemp } from 'node:fs';
// The parent directory for the new temporary directory
const tmpDir = tmpdir();
// This method is *INCORRECT*:
mkdtemp(tmpDir, (err, directory) => {
if (err) throw err;
console.log(directory);
// Will print something similar to `/tmpabc123`.
// A new temporary directory is created at the file system root
// rather than *within* the /tmp directory.
});
// This method is *CORRECT*:
import { sep } from 'node:path';
mkdtemp(`${tmpDir}${sep}`, (err, directory) => {
if (err) throw err;
console.log(directory);
// Will print something similar to `/tmp/abc123`.
// A new temporary directory is created within
// the /tmp directory.
});
相关用法
- Node.js fs.mkdtemp()用法及代码示例
- Node.js fs.mkdtempSync()用法及代码示例
- Node.js fs.mkdir()用法及代码示例
- Node.js fs.mkdirSync()用法及代码示例
- Node.js fs.mkdir(path[, options], callback)用法及代码示例
- Node.js fs.filehandle.datasync()用法及代码示例
- Node.js fs.chmod()用法及代码示例
- Node.js fs.read()用法及代码示例
- Node.js fs.Dirent.isFile()用法及代码示例
- Node.js fs.Dir.closeSync()用法及代码示例
- Node.js fs.fchmodSync()用法及代码示例
- Node.js fs.symlink(target, path[, type], callback)用法及代码示例
- Node.js fs.constants用法及代码示例
- Node.js fs.fdatasync()用法及代码示例
- Node.js fs.Dirent.isFIFO()用法及代码示例
- Node.js fs.copyFile()用法及代码示例
- Node.js fs.writeSync()用法及代码示例
- Node.js fs.symlink()用法及代码示例
- Node.js fs.truncate()用法及代码示例
- Node.js fs.openSync()用法及代码示例
- Node.js fs.filehandle.write()用法及代码示例
- Node.js fs.fdatasyncSync()用法及代码示例
- Node.js fs.filehandle.sync()用法及代码示例
- Node.js fs.fsyncSync()用法及代码示例
- Node.js fs.Dirent.isDirectory()用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 fs.mkdtemp(prefix[, options], callback)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。