【问题标题】:How to test if a file exists in Tabris?如何测试 Tabris 中是否存在文件?
【发布时间】:2018-12-25 07:47:07
【问题描述】:

这是一个在 Tabris 中读取和写入文件的示例(在操场上工作)。 (这可能是一个很好的 sn-p 帮助理解 iOS/Android/Windows 上的“路径”)

如果您尝试读取不存在的文件,则会报告一般错误。

如何测试文件是否存在?

我已经尝试了一些无效的 Nodejs 方法。

谢谢

代码

const {fs, Button, TextView, TextInput, ui} = require('tabris')

const FILENAME = 'hello.txt'
const FILEPATH = fs.filesDir + '/' + FILENAME

console.log(FILEPATH)

let btnReadFile = new Button({
  centerX: 0,  top: 'prev() 10', width: 200,
  text: 'Read File: ' + FILENAME
}).appendTo(ui.contentView)

btnReadFile.on('select', () => {
  fs.readFile(FILEPATH, 'utf-8')
    .then(text => txiFile.text = text)
    .catch(err => console.error(err))
})

let btnWriteFile = new Button({
  centerX: 0,  top: 'prev() 10', width: 200,
  text: 'Write File: ' + FILENAME
}).appendTo(ui.contentView)

let btnRemoveFile = new Button({
  centerX: 0,  top: 'prev() 10', width: 200,
  text: 'Remove File: ' + FILENAME
}).appendTo(ui.contentView)

btnWriteFile.on('select', () => {
  fs.writeFile(FILEPATH, txiFile.text, 'utf-8')
    .then(() => console.log('file written:', FILEPATH))
    .catch(err => console.error(err))
})

btnRemoveFile.on('select', () => {
  txiFile.text = ''
  fs.removeFile(FILEPATH)
    .then(() => console.log('file removed:', FILEPATH))
    .catch(err => console.error(err))
})

let txiFile = new TextInput({
  top: 'prev() 20', left: '20%', right: '20%', height: 100,
  type: 'multiline'
}).appendTo(ui.contentView)

屏幕截图


使用 fs.readDir 解决方案更新代码不起作用...

函数总是返回 false - 但在 async function() 内部我看到它正在工作,并且 filesDir 正确列出了文件。

const FILENAME = 'helloxxppp.txt'
const FILEPATH = fs.filesDir
const FULLFILEPATH = FILEPATH + '/' + FILENAME

console.log('FILENAME: \n ' + FILENAME)
console.log('FILEPATH: \n ' + FILEPATH)
console.log('FULLFILEPATH \n: ' + FULLFILEPATH)

// this ALWAYS is false
if (fileExist(FILEPATH, FILENAME)) {
  console.log('File NOT exists\n')
} else {
  console.log('File exists\n')
}

async function fileExist (path, file) {
  let files
  try {
    files = await fs.readDir(path)
    console.log(files)
  } catch (err) {
    return false
  }
  return files.indexOf(file) > -1
}

【问题讨论】:

  • 你尝试了哪些 Nodejs 方法?
  • 当您尝试它时,您是否为 require('fs') 创建了一个不同的变量,因为您已经将 fs 用于 tabris?
  • @JosephWebber Tabris.js 环境不使用与 Node 相同的 fs。这是一个单独的,方法不同:docs.tabris.com/latest/api/fs.html
  • 我知道,我在问 OP 在尝试使用 Node 的 fs 时是否使用了不同的变量名,因为 var fs 会与 const {fs, ...} 冲突。

标签: javascript file-exists tabris tabris-js


【解决方案1】:

异步/等待版本:

const {fs} = require('tabris')

async function fileExist(path, file) {
  let files
  try {
    files = await fs.readDir(path)
  } catch (err) {
    return false
  }
  return files.indexOf(file) > -1
}

承诺版本:

const {fs} = require('tabris')

function fileExist(path, file) {
  return new Promise((resolve, reject) => {
    fs.readDir(path)
      .then(files => resolve(files.indexOf(file) > -1))
      .catch(err => resolve(false)) // Error is ignored intentionally
  })
}

用法:

1) 使用then:

const FILENAME = 'helloxxppp.txt'
const FILEPATH = fs.filesDir

fileExist(FILEPATH, FILENAME).then((exist) => {
  if (exist) {
    console.log('File NOT exists\n')
  } else {
    console.log('File exists\n')
  }
})

2) 使用async/await:

async myFunction() {
  // code here
  let exist = await fileExist(FILEPATH, FILENAME)
    if (exist) {
    console.log('File NOT exists\n')
  } else {
    console.log('File exists\n')
  }
}

【讨论】:

  • 我同意这个答案。 Tabris.js 还没有确定文件是否存在的方法,因此您必须自己实现它。这是一个很好的功能请求候选者。
  • 谢谢,我明白了,获取文件列表,如果 indexOf > -1,文件存在。我试图把它放在我的例子中,但函数总是返回 false - 你能看到我哪里出错了吗,(在 Playground 中工作)
  • 我将函数添加到我的代码中,请参阅上面的“使用 fs.readDir 解决方案更新代码不起作用...”我编辑的 POST 我一定遗漏了一些东西...
  • 我没有安装开发工具。你能用调试器进入函数并检查会发生什么吗?
  • 不确定“开发工具” - 我的代码将在 tabrisjs.com/playground 中运行 - 这是作为 GIST 的完整代码 gist.github.com/mrmccormack/fdef86b4a710db08b019ab93f44d5de5 - 希望对您有所帮助...
猜你喜欢
  • 1970-01-01
  • 2011-02-16
  • 2010-09-14
  • 2011-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-16
相关资源
最近更新 更多