例1:
const fs = require('fs')
//用来把文件/目录的相对路径异步的解析为真实的绝对路径
fs.realpath('./', (err, resolvedPath) => {
if(err) {
console.error(err)
return
}
console.log('a.text的绝对路径是:')
console.log(resolvedPath)
})
执行结果:
image.png
例2:
const fs = require('fs')
//用来把文件/目录的相对路径异步的解析为真实的绝对路径
fs.realpath('./', 'buffer', (err, resolvedPath) => {
if(err) {
console.error(err)
return
}
console.log('a.text的绝对路径是:')
console.log(resolvedPath)
})
执行结果:
image.png
例3:
const fs = require('fs')
//用来把文件/目录的相对路径异步的解析为真实的绝对路径
fs.realpath('./', {encoding: 'buffer'}, (err, resolvedPath) => {
if(err) {
console.error(err)
return
}
console.log('a.text的绝对路径是:')
console.log(resolvedPath)
})
执行结果:
image.png
例4:
const fs = require('fs')
//用来把文件/目录的相对路径异步的解析为真实的绝对路径
fs.realpath.native('./foo', (err, resolvedPath) => {
if(err) {
console.error(err)
return
}
console.log('foo的绝对路径是:')
console.log(resolvedPath)
})
执行结果:
image.png
例5:
const fs = require('fs')
//用来把文件/目录的相对路径异步的解析为真实的绝对路径
fs.realpath.native('./foo', 'buffer', (err, resolvedPath) => {
if(err) {
console.error(err)
return
}
console.log('foo的绝对路径是:')
console.log(resolvedPath)
})
执行结果:
image.png
例6:
const fs = require('fs')
//用来把文件/目录的相对路径同步的解析为真实的绝对路径
const resolvedPath = fs.realpathSync('./a.text')
console.log(resolvedPath)
const resolvedPath1 = fs.realpathSync('./a.text', 'buffer')
console.log(resolvedPath1)
const resolvedPath2 = fs.realpathSync.native('./foo')
console.log(resolvedPath2)
const resolvedPath3 = fs.realpathSync.native('./foo', 'buffer')
console.log(resolvedPath3)
执行结果:
image.png
网友评论