PhoneGap读写SD卡——文本文件方式

2023-05-16

1、从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>



本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PhoneGap读写SD卡——文本文件方式 的相关文章

  • Python获取当前路径

    Refs https blog csdn net qq 15188017 article details 53991216 假设py文件路径为 F SEG myResearch myProject 2 test py Method 1 sy
  • vm虚拟机下载安装、iso镜像下载

    VMware官网下载 VMware Customer Connect The All In One VMware Product Support Portal iso镜像下载地址 1 centos xff08 推荐下载DVD IOS类型 x
  • Codeforces--501B--Misha and Changing Handles

    题目描述 xff1a Misha hacked the Codeforces site Then he decided to let all the users change their handles A user can now cha
  • Codeforces--1165A--Remainder

    题目描述 xff1a You are given a huge decimal number consisting of n digits It is guaranteed that this number has no leading z
  • Flutter web app三端跨平台双向桥接dart web开发

    Flutter web app三端跨平台双向桥接dart web开发 环境及项目运行步骤 xff1a 1 安装开发工具webstorm 2 官网下载flutter stable 1 5 4 xff0c 安装好并配置环境变量 3 执行cmd命
  • 正则表达式 linux shell

    正则表达式 热身 正则表达式 regular expression 描述了一种字符串匹配的模式 xff0c 可以用来检查一个串是否含有某种子串 将匹配的子串做替换或者从某个串中取出符合某个条件的子串等 例如 grep expr sed aw
  • 数据库中top的用法简述

    转自 xff1a 微点阅读 https www weidianyuedu com 数据库中top的用法的用法你知道吗 xff1f 下面小编就跟你们详细介绍下数据库中top的用法的用法 xff0c 希望对你们有用 数据库中top的用法的用法如

随机推荐