0

指定されたディレクトリにテキスト ファイルを作成する方法を教えてください。そのテキスト ファイルに書き込み、そのテキスト ファイルからテキストを読み取る必要があります。このコードを使用してフォルダーを作成できますが、フォルダー内にテキストファイルを追加する必要があります(newDir)。

<!DOCTYPE html>
<html>
  <head>
    <title>Local File System Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
    <script type="text/javascript" charset="utf-8">

      // Wait for Cordova to load
      document.addEventListener("deviceready", onDeviceReady, false);

      // Cordova is ready
      function onDeviceReady() {
          window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onFileSystemFail);
      }

      function onFileSystemSuccess(fileSystem) {
          console.log(fileSystem.name);
          var directoryEntry = fileSystem.root;
          directoryEntry.getDirectory("newDir", {create: true, exclusive: false}, onDirectorySuccess, onDirectoryFail)
      }

      function onDirectorySuccess(parent) {
          console.log(parent);
      }

      function onDirectoryFail(error) {
          alert("Unable to create new directory: " + error.code);
      }

      function onFileSystemFail(evt) {
          console.log(evt.target.error.code);
      }

    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>Local File System</p>
  </body>
</html>
4

1 に答える 1

0

Cordova File APIには、必要なすべての情報が含まれています。

以下のコードは、ディレクトリを作成し、ディレクトリ内にファイルを作成し、ファイルにサンプル テキストを書き込みます。

<script type="text/javascript" charset="utf-8">

    document.addEventListener("deviceready", onDeviceReady, false);

     // Cordova is ready
    function onDeviceReady() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    }

    function gotFS(fileSystem) {
        // create dir
        fileSystem.root.getFile("newDir", {
            create: true,
            exclusive: false
        }, gotDirEntry, fail);
    }

    function () gotDirEntry(dirEntry) {
        // create file
        dirEntry.getFile("newFile.txt", {
            create: true,
            exclusive: false
        }, gotFileEntry, fail);
    }

    function gotFileEntry(fileEntry) {
        fileEntry.createWriter(gotFileWriter, fail);
    }

    function gotFileWriter(writer) {
        writer.onwrite = function (evt) {
            console.log("write completed");
        };
        writer.write("some sample text");
        writer.abort();
    }

    function fail(error) {
        console.log(error.code);
    }
</script>
于 2013-06-19T13:47:16.917 に答える