如何在PhoneGap中读写文本文件。

时间:2012-01-06 16:02:26

标签: cordova

我想在PhoneGap Android中读取和写入文件,我尝试了this示例。 但我找不到任何输出。即。我找不到文件的保存位置和写入位置。我们可以手动创建一个文件并在其中写入或在PhoneGap中从中读取。

2 个答案:

答案 0 :(得分:0)

尝试在Eclipse中的DDMS Perspective中找到该文件。

Window -> Show View -> File Explorer. In this one look for the path data -> data -> your.projects.package.name and search there for the file. 

答案 1 :(得分:0)

我已经实施了几个关于phonegap的演示。一切都很好。您无需为操作创建目录或文件,但是phonegap api会为您完成所有这些操作。请显示您的代码,以便更轻松地解决您的问题。

//some code I write
function fail(error) {
       console.log(error.code);
   }

   function getLogPath() {
       var d = new Date();
       return "log"+d.getFullYear()+"-"+d.getMonth()+"-"+d.getDate()+".txt";
   }

   function writeToLogFile(errorMsg) {
       try {
           window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
               function(fileSystem) {
                   fileSystem.root.getDirectory("phonegapLogFile", { create: true, exclusive: false }, function(directoryEntry) {
                       console.log("log folder is created");
                       directoryEntry.getFile(getLogPath(), { create: true, exclusive: false }, function(fileEntry) {
                           fileEntry.createWriter(function(writer) {
                               writer.seek(writer.length);
                               writer.write("\r\n");
                               writer.write("\r\n");
                               writer.write("Log message-" + new Date().toLocaleString());
                               writer.write("\r\n");
                               writer.write(errorMsg);
                           }, fail);
                       }, fail);
                   }, fail);
               },
               fail);
       }
       catch(e) {
           fail(e);
       }
   }