PhoneGap读写SD卡——文本文件方式
2014-11-13 11:24
218 查看
1、从SD卡中的文本文件读取数据
2、向SD卡中写数据,以文本文件格式存储
<script type="text/javascript" charset="utf-8"> //等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完毕 function onDeviceReady() { window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); } //获取newFile目录,如果不存在则创建该目录 function gotFS(fileSystem) { newFile = fileSystem.root.getDirectory("newFile", {create : true,exclusive : false}, readerFile, fail); } //获取newFile目录下面的dataFile.txt文件,如果不存在则创建此文件 function readerFile(newFile) { newFile.getFile("dataFile.txt", {create : true,exclusive : false}, gotFileEntry, fail); } function gotFileEntry(fileEntry) { fileEntry.file(gotFile, fail); } function gotFile(file){ readAsText(file); } function readAsText(file) { var reader = new FileReader(); reader.onloadend = function(evt) { console.log("Read as text"); console.log(evt.target.result); var resultString = evt.target.result; while(resultString.length > 0) { var cellString = resultString.substring(0,resultString.indexOf("::::")); var resultString = resultString.substring(resultString.indexOf("::::") + 4, resultString.length); var ad=document.getElementById('myBody'); var adiv=document.createElement('div'); adiv.textContent = cellString; adiv.id="kkk"; ad.appendChild(adiv); var hr = document.createElement('hr'); ad.appendChild(hr); } }; reader.readAsText(file); } function fail(evt) { console.log(evt.target.error.code); } </script>
2、向SD卡中写数据,以文本文件格式存储
<script type="text/javascript" charset="utf-8"> //等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完毕 function onDeviceReady() { window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); } //获取newFile目录,如果不存在则创建该目录 function gotFS(fileSystem) { newFile = fileSystem.root.getDirectory("newFile", {create : true,exclusive : false}, writerFile, fail); } //获取newFile目录下面的dataFile.txt文件,如果不存在则创建此文件 function writerFile(newFile) { newFile.getFile("dataFile.txt", {create : true,exclusive : false}, gotFileEntry, fail); } function gotFileEntry(fileEntry) { fileEntry.createWriter(gotFileWriter, fail); } function gotFileWriter(writer) { //onwrite:当写入成功完成后调用的回调函数 writer.onwrite = function(evt) { navigator.notification.alert("添加成功!", null, "提示", "知道了"); }; writer.seek(writer.length); //相当于文件光标 writer.write("the text you want to write into the file"); //参数为要写入文件的内容 } function fail(error) { alert("Failed to retrieve file:" + error.code); } </script>
相关文章推荐
- Android——数据存储(四种方式之二)读写SD卡
- STM32的简单的SD卡读写(不带文件系统,SPI方式)
- 黑马程序员_第一天_学会用IO流读写方式拷贝文本文件
- STM32的简单的SD卡读写(不带文件系统,SPI方式)
- android 文本文件(含SD卡)读写
- Android——数据存储(四种方式之二)读写SD卡——练习
- C#高级编程 多种方式读写文本文件
- 文件读写的文本方式与二进制方式、文本文件与二进制文件之区别
- C#高级编程 多种方式读写文本文件
- Fileatream表示文件流,它能够打开和关闭文件,并对文件进行单字节的读写操作。 StreamReader和StreamWriter以文本方式对流进行读写操作。建立一个文本文件,分别使用上面两种方
- PHP 简易聊天室实例(读写文本文件的方式)
- C++读写txt文件方式以及基于opencv的Mat数据类型读写txt文件类型
- 文本文件读写-QTextStream类
- OleContainer操作Excel以二进制方式读写数据库
- cocos2d-x 横屏、竖屏、读写sd卡
- json之java方式的读写
- C++(3):文件读写操作(含int型和char型变量的存储方式)
- 读写文本文件和二进制文件——二进制模式
- 解决SD卡频繁读写问题 Anything-sync-daemon 映射linux目录到tmpfs并定时同步
- Epoll在LT和ET模式下的读写方式