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

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>



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值