如果找到,DirectoryEntry 的 getFile 方法是否可以删除现有文件

Can the getFile Method of the DirectoryEntry delete an existing file if found?

本文关键字:删除 文件 是否 方法 DirectoryEntry getFile 如果      更新时间:2023-09-26

我目前正在使用类似于以下内容的代码创建文本文件。这将很好地创建一个文件,但它不会删除现有文件。

fs.root.getFile('log.txt', {create: true, exclusive: false},
   function(fileentry) {
      fileentry.createWriter(function (filewriter) {
         filewriter.write('Hello world!');
      });
   }
);

有什么方法可以在没有太多额外代码或专用删除代码的情况下干净地删除现有文件?

我一直在使用本指南,但没有找到有效的方法来做到这一点。

http://www.html5rocks.com/en/tutorials/file/filesystem/

这是我所能得到的尽可能接近的。它会检查文件,如果文件存在,则删除该文件。如果无法删除错误,它还会捕获错误。如果在所有这些结束时,新文件有空间,则会像以前一样创建该文件。

fs.root.getFile('log.txt', {create: false},
   function(fileentry) {
      fileentry.remove(createfile, errorhandler);
   },
   createfile
);
function createfile(){
   //either the file was never there or has been removed successfully
   fs.root.getFile('log.txt', {create: true, exclusive: false},
      function(fileentry) {
         fileentry.createWriter(function (filewriter) {
            filewriter.write('Hello world!');
         });
      }
   );
}
function errorhandler(error){
   //file already exists and couldn't be removed
}

它不像我想要的那么紧凑和漂亮,但它是我目前能想到的最好的。

这应该像你的作家一样工作:

fs.root.getFile('log.txt', {create: false}, function(fileEntry) {
    fileEntry.remove(function() {
      console.log('File deleted.');
    }, errorHandler);
  }, errorHandler);