例1:
const fs = require('fs')
//用来把文件/目录的相对路径异步的解析为真实的绝对路径
fs.realpath('./', (err, resolvedPath) => {
if(err) {
console.error(err)
return
}
console.log('a.text的绝对路径是:')
console.log(resolvedPath)
})
执行结果:
![](https://img.haomeiwen.com/i22765904/d5688bb0a24eaaaa.png)
例2:
const fs = require('fs')
//用来把文件/目录的相对路径异步的解析为真实的绝对路径
fs.realpath('./', 'buffer', (err, resolvedPath) => {
if(err) {
console.error(err)
return
}
console.log('a.text的绝对路径是:')
console.log(resolvedPath)
})
执行结果:
![](https://img.haomeiwen.com/i22765904/e42bfd1a87ef0eb3.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)
})
执行结果:
![](https://img.haomeiwen.com/i22765904/e42bfd1a87ef0eb3.png)
例4:
const fs = require('fs')
//用来把文件/目录的相对路径异步的解析为真实的绝对路径
fs.realpath.native('./foo', (err, resolvedPath) => {
if(err) {
console.error(err)
return
}
console.log('foo的绝对路径是:')
console.log(resolvedPath)
})
执行结果:
![](https://img.haomeiwen.com/i22765904/edc057d66388b233.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)
})
执行结果:
![](https://img.haomeiwen.com/i22765904/251b43768d2dec23.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)
执行结果:
![](https://img.haomeiwen.com/i22765904/3266dff4ed81671a.png)
网友评论