Phonegap - Creating a .txt file on first load

大憨熊 提交于 2019-12-03 16:58:44

You can take a look at the full example here:

http://docs.phonegap.com/en/1.4.1/phonegap_file_file.md.html#FileWriter

This line create the file if it doesn't exist :

fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);

Supported Platforms

Android BlackBerry WebWorks (OS 5.0 and higher) iOS Windows Phone 7 ( Mango )

I don't know about there others but in iOS, the document is created in /var/mobile/Application/YOU_APP/Documents

[CODE]

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

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

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

            function gotFS(fileSystem) {
                fileSystem.root.getFile("readme.txt", {create: true}, gotFileEntry, fail);
            }

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

            function gotFileWriter(writer) {
                writer.onwrite = function(evt) {
                    console.log("write success");
                };

                writer.write("some sample text");
                writer.abort();
                // contents of file now 'some different text'
            }

            function fail(error) {
                console.log("error : "+error.code);
            }

        </script>

Hope it helps

Depending on the devices you are developing for you can look at using their native file creation APIs. For example, iOS uses plists. Android does use .txt files, look at this link for more information.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!