Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用内置模块处理文件 #9

Open
luoway opened this issue Sep 13, 2023 · 0 comments
Open

使用内置模块处理文件 #9

luoway opened this issue Sep 13, 2023 · 0 comments

Comments

@luoway
Copy link
Owner

luoway commented Sep 13, 2023

process.cwd()、__dirname

// path/to/src/cwd.cjs

// 进程运行目录
console.log('process.cwd():', process.cwd())

// 文件所在目录
console.log('__dirname:', __dirname) // path/to/src
// 文件绝对路径
console.log('__filename:', __filename) // path/to/src/cwd.cjs

path 模块

在 Windows 与其他系统执行结果有差异,可通过 path.win32path.posix 指定系统返回结果

__dirname: H:\git\nodejs-demo\files\src 
__filename: H:\git\nodejs-demo\files\src\paths.js
path.basename(__filename): paths.js
path.basename(__filename, '.js'): paths
path.basename(__dirname): src
path.basename(__dirname, 'c'): sr // 字符串处理
path.delimiter: ;
path.win32.delimiter: ; // 指定Windows平台
path.posix.delimiter: :
path.sep: \
path.win32.sep: \
path.posix.sep: /
path.dirname(__filename): H:\git\nodejs-demo\files\src
path.dirname(__dirname): H:\git\nodejs-demo\files // 返回输入路径的所在目录
path.extname(__filename): .js // 返回最后.及之后的字符串
path.parse(__filename): {
  root: 'H:\\',
  dir: 'H:\\git\\nodejs-demo\\files\\src',
  base: 'paths.js',
  ext: '.js',
  name: 'paths'
}
path.format({root: '/', name: 'file', ext: '.js'}): /file.js
path.format({root: '/', base: 'file.js', ext: 'anything, ignored'}): /file.js
path.isAbsolute('/baz/..'): true
path.isAbsolute('C:/foo/..'): true
path.isAbsolute('qux/'): false
path.isAbsolute('.'): false
path.join('/foo', 'bar', 'baz/asdf/', './abc') \foo\bar\baz\asdf\abc // 将输入字符拼接为有效的路径格式
path.join('/foo', 'bar', 'baz/asdf/', './abc', '..') \foo\bar\baz\asdf
path.normalize('/foo/./asdf//////quux/../..') \foo // 将输入字符序列为有效的路径格式
path.relative('/data/test/from', '/data/impl/to') ..\..\impl\to
path.resolve('./', 'a/b') H:\git\nodejs-demo\files\a\b // 类似path.join,返回绝对路径
path.toNamespacedPath('./abc') \\?\H:\git\nodejs-demo\files\abc //仅Windows有效
path.win32.toNamespacedPath('./abc') \\?\H:\git\nodejs-demo\files\abc
path.posix.toNamespacedPath('./abc') ./abc

fs 模块

CURD

Create

创建文件有三种方法

  • fs.appendFile( filePath, data[, options], callback )
    将指定的内容追加到文件中。文件不存在则创建
  • fs.open( filePath, flag[, mode], callback )
    按标志打开文件。当标志为 aa+ww+ 时,文件不存在则创建
  • fs.writeFile( filePath, data[, options] )
    替换或创建指定的文件和内容
  • fs.createWriteStream(path[, options])

Update

修改文件有两种方法

  • fs.appendFile()
  • fs.writeFile()

Read

读取文件有两种方法

  • fs.open()
  • fs.readFile(path[, options], callback)
  • fs.createReadStream(path[, options])

Delete

删除文件有两种方法

  • fs.unlink(path, callback)
    删除文件或软链接
  • fs.rm(path[, options], callback)
    删除文件或目录
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant