如何读取目录和文件

时间:2012-06-21 14:52:48

标签: android jquery cordova

我需要阅读目录中的许多文件 我可以获取目录的文件名,但不会读取文件内容

以下是我的代码

$("#import-offline").click( function(){
            var localFolder = "ico/recebe/importa/";
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
                  fs.root.getDirectory(localFolder, {}, function(dirEntry){
                  var dirReader = dirEntry.createReader();
                  dirReader.readEntries(function(entries) {
                        for(var i = 0; i < entries.length; i++) {
                            var entry = entries[i];
                            if (entry.isFile){  // here works
                                var arquivoPath = entry.fullPath;
                                var reader = new FileReader();
                                reader.onloadend = function(evt) {
                                    $("#msgSync").html(evt.target.result);
                                };
                                reader.readAsText(arquivoPath);

                            }
                        }
                  }, fail);
                }, fail);
            });

        });    

    });

1 个答案:

答案 0 :(得分:0)

解决

$("#importOffline").click(function(){
    $('#msgSync').html("Iniciando importação aguarde...");
    var localFolder = "ico/recebe/importa";
    console.log("Chamou a função");
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
          console.log("Abriu File System");  
          // rodar log no console
          // adb logcat CordovaLog:V *:S 
          fs.root.getDirectory(localFolder, {create:false}, function(dirEntry){
          console.log(">>>>> Abriu a pasta");
          var dirReader = dirEntry.createReader();
          dirReader.readEntries(function(entries) {
                console.log("Lendo diretórios");
                for(var i = 0; i < entries.length; i++) {
                    console.log("Arquivos "  + i);
                    var entry = entries[i];
                    if (entry.isFile){
                        var arquivoPath = entry.fullPath;
                        var n = entry.name.split(".");
                        tabela = n[0];
                        console.log("Lendo " + arquivoPath);
                        var reader = new FileReader();

                        reader.onloadend = function(evt) {
                            $('#msgSync').html("");
                            $('#msgSync').html("Importando "+tabela+", aguarde..." + i);
                            var arq = evt.target.result.split( /\r?\n/g );
                            console.log("=====================================");
                            importaTxt(tabela, arq);

                        };
                        reader.readAsText(arquivoPath);
                    }
                }

                $('#msgSync').html("");
                navigator.notification.alert("Importação finalizada", null, "mSAC");
          }, fail);
        }, fail);
    });