Cordova files Read and write JavaScript

Source: Internet
Author: User

The following code may help you to use the Cordova file plug-in function, you understand it, oh, say, this is the front-end HTML JavaScript method, non-Java plug-ins


Prerequisite: Configure Cordova Plug-in method, still not on their own niang.

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);
		The 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 fileent
			Ry ");
		Return
			
		else {writefileintenalcreatefile (fileentry, NULL, isappend);

}}, Onerrorcreatefile);
	
	function WriteFile (filePath, data) {dataobj=data;
Window.resolvelocalfilesystemurl (FilePath, Writemain, Onfilesystemfail);
		The 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 writ
			E. ... ");
		Createfilecomplete ();

		}; Filewriter.onerror = function (e) {console.log ("Failed file R")EAD: "+ 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 writ
			E. ... ");
		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);
}); The 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); //Get the MOBOVIP directory, and if not, create the directory function onfilesystemsuccess (filesystem) {newFile = FileSystem.root.getDirectory (
Directory, {create:true, exclusive:false}, Ondirectorysuccess, Onfilesystemfail);  //Get the Stores.txt file under the MOBOVIP directory, and create this file function Ondirectorysuccess (newFile) {newfile.getfile if not present (FileName, {create:
True, Exclusive:false}, Onfilesuccess, Onfilesystemfail); /** * Gets FileWriter object for writing data * @param fileentry/function onfilesuccess (fileentry) {Fileentry.createwriter (Onfilew
Ritersuccess, Onfilesystemfail); /** * Write Datas * @param writer/function onfilewritersuccess (writer) {//log ("Filename=" +writer.filename+); filElength= "+writer.length+";p osition= "+writer.position);
	Writer.onwrite = function (e) {//callback function Console.log ("write success") called after successful completion of the write;
	};
	Writer.onerror = function (e) {//callback functions called after write failure Console.log ("Write error" + Error.code);
	};
	Writer.onabort = function (e) {//write callback functions invoked after being aborted, for example by calling abort () console.log ("Write abort" + Error.code);
	}; 
	Quickly point the file pointer to the end of the file, which can be append//Writer.seek (writer.length); Writer.write (datas)///write data to File//writer.truncate (11);//Truncate File//Writer.abort () by specified length;/Abort Write file} 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 ("read file" +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 ("read file start");
	Fileentry.file (function (file) {
		var reader = new FileReader ();
		Reader.onloadend = function () {
			Console.log ("read successfully");
			$ ("#state"). Text (this.result);
			Setjsonstring (This.result);
		};
		Reader.readastext (file);
	}, fail);

To have a function setjsonstring (STR) to accept read files on the main page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.