cordova文件读写javascript

下面代码也许有助你使用cordova的文件插件功能,自己理解吧,哦,说一下,这是前端Html JavaScript方法,非java 插件


前提:配置cordova的插件方法,还不会就自己度娘。

cordova plugin add phonegap-nfc
cordova plugin add cordova-plugin-device
cordova plugin add cordova-plugin-file
cordova plugin add cordova-plugin-camera
cordova plugin add cordova-plugin-file-transfer
cordova plugin add cordova-plugin-media
cordova plugin add cordova-plugin-media-capture
cordova plugin add org.apache.cordova.device-orientation  

var writedirectory; // default directory
var writefileName; // default file name
var dataObj = null; //datas need write
function getSDCardFile(rootDirEntry, directory, fileName) {
	this.writedirectory = directory;
	this.writefileName = fileName;
	console.log('file system open: ' + rootDirEntry);
	window.resolveLocalFileSystemURL(rootDirEntry, getfs, onErrorGetDir);

}
function getfs(sdcard) {
	if (sdcard == null) {
		console.log("No sdcard");
		return;
	} else {
		dirEntry = createDirectory(sdcard, writedirectory, writefileName);
	}
}
function createDirectory(rootDirEntry, directory, fileName) {
	return rootDirEntry.getDirectory(directory, {
		create : true
	}, function(dirEntry) {

		if (dirEntry == null) {
			console.log("No dirEntry");
			return;
		} else {
			fileEntry = createFile(dirEntry, fileName, false);
		}
	}, onErrorGetDir);
}

function createFile(dirEntry, fileName, isAppend) {
	// Creates a new file or returns the file if it already exists.
	return dirEntry.getFile(fileName, {
		create : true,
		exclusive : false
	}, function(fileEntry) {
		if (fileEntry == null) {
			console.log("No fileEntry");
			return;
		} else {
			writeFileintenalCreateFile(fileEntry, null, isAppend);
			
		}

	}, onErrorCreateFile);

}

function writeFile(filePath, data) {
	dataObj=data;
	
	window.resolveLocalFileSystemURL(filePath, writeMain, onFileSystemFail);
}

function writeMain(fileEntry) {
	if (fileEntry == null) {
		console.log("No writeMain fileEntry");
		return;
	} else {
		var isAppend = false;
		writeFileintenalwriteFile(fileEntry, dataObj, isAppend);
	}
}

function writeFileintenalCreateFile(fileEntry, dataObj, isAppend) {
	// Create a FileWriter object for our FileEntry (log.txt).
	console.log('file system open to write data: ' + dataObj);
	fileEntry.createWriter(function(fileWriter) {

		fileWriter.onwriteend = function() {
			console.log("Successful file write...");
			createFileComplete();
		};

		fileWriter.onerror = function(e) {
			console.log("Failed file read: " + e.toString());
		};

		// If we are appending data to file, go to the end of the file.
		if (isAppend) {
			try {
				fileWriter.seek(fileWriter.length);
			} catch (e) {
				console.log("file doesn't exist!");
			}
		}
		fileWriter.write(dataObj);
	});
}
function writeFileintenalwriteFile(fileEntry, dataObj, isAppend) {
	// Create a FileWriter object for our FileEntry (log.txt).
	console.log('file system open to write data: ' + dataObj);
	fileEntry.createWriter(function(fileWriter) {

		fileWriter.onwriteend = function() {
			console.log("Successful file write...");
			//writeFileComplete();
		};

		fileWriter.onerror = function(e) {
			console.log("Failed file read: " + e.toString());
		};

		// If we are appending data to file, go to the end of the file.
		if (isAppend) {
			try {
				fileWriter.seek(fileWriter.length);
			} catch (e) {
				console.log("file doesn't exist!");
			}
		}
		fileWriter.write(dataObj);
	});
}

function onFileSystemFail(error) {
	console.log("Failed to get file:" + error.code);
}

function onErrorCreateFile(error) {
	console.log("Failed to CreateFile:" + error.code);
}

function onErrorGetDir(error) {
	console.log("Failed to GetDir:" + error.code);
}
var datas=null;//datas need write
var directory="mobovip";//default directory
var fileName="stores.txt";//default file name
function write(data,directory,fileName){
	this.datas=data;
	this.directory=directory;
	this.fileName=fileName;
	window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onFileSystemFail);
}

//获取mobovip目录,如果不存在则创建该目录
function onFileSystemSuccess(fileSystem) {
	newFile = fileSystem.root.getDirectory(directory, {
		create : true,
		exclusive : false
	}, onDirectorySuccess, onFileSystemFail);
}
//获取mobovip目录下面的stores.txt文件,如果不存在则创建此文件
function onDirectorySuccess(newFile) {
	newFile.getFile(fileName, {
		create : true,
		exclusive : false
	}, onFileSuccess, onFileSystemFail);
}
/**
 * 获取FileWriter对象,用于写入数据
 * @param fileEntry
 */
function onFileSuccess(fileEntry) {
	fileEntry.createWriter(onFileWriterSuccess, onFileSystemFail);
}

/**
 * write datas
 * @param writer
 */
function onFileWriterSuccess(writer) {
//	log("fileName="+writer.fileName+";fileLength="+writer.length+";position="+writer.position);
	writer.onwrite = function(e) {//当写入成功完成后调用的回调函数
		console.log("write success");
	};
	writer.onerror = function(e) {//写入失败后调用的回调函数
		console.log("write error"+ error.code);
	};
	writer.onabort = function(e) {//写入被中止后调用的回调函数,例如通过调用abort()
		console.log("write abort"+ error.code);
	};
	// 快速将文件指针指向文件的尾部 ,可以append
//	writer.seek(writer.length); 
	writer.write(datas);//向文件中写入数据
//	writer.truncate(11);//按照指定长度截断文件
//	writer.abort();//中止写入文件
}

function onFileSystemFail(error) {
	console.log("Failed to retrieve file:" + error.code);
}



var directory; //default directory
var fileName; //default file name
var JsonString;
function Readfile(filePath) {
	console.log("读取文件"+filePath);
	this.directory = directory;
	this.fileName = fileName;
	window.resolveLocalFileSystemURL(filePath, readFileMain, fail);
	//window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory + filePath, readFileMain, fail);

}

function fail(e) {
	SetJsonString(null);
	console.log("FileSystem Error");
	console.log(e.code);
}

function readFileMain(fileEntry) {
	console.log("读取文件开始");
	fileEntry.file(function(file) {
		var reader = new FileReader();
		reader.onloadend = function() {
			console.log("成功读取");
			//$("#state").text(this.result);
			SetJsonString(this.result);
		};
		reader.readAsText(file);
	}, fail);
}

主页面上要有个function SetJsonString(Str)接受读文件的方法

你可能感兴趣的:(自己软件开源代码,自己的工具)