8. a. Store
text and binary data in files
b. Read
text and binary data from files
c.
Exercise -1
Store Text in Files
Solution
var textDir = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'FileManipulations');
if (! textDir.exists()) {
textDir.createDirectory();
var textFile = Ti.Filesystem.getFile(textDir.resolve(), 'trainingdetails.txt');
//Create a Buffer
//var buffer = Ti.createBuffer({ length: 1024 });
//var buffer = Ti.createBuffer({ value: "Training progressing" });
textFile.write("forgot last contents", false);
textFile=null;
textDir =null;
}
Exercise - 2
Read Text in Files
Solution
function(){
Ti.API.info('reading text');
//reading the file
var textDir = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'FileManipulations');
if (textDir.exists()) {
var textFile = Ti.Filesystem.getFile(textDir.resolve(), 'trainingdetails.txt');
var blob = textFile.read();
var readText = blob.text;
Ti.API.info('Text from file is '+ readText);
}
};
Exercise -3
Store Binary data
Solution
function(){
var imgVw= Ti.UI.createImageView({
image:'/appicon.png'
});
Ti.API.info('start storing binary data');
var textDir = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'FileManipulations');
if ( textDir.exists()) {
var imgFile = Ti.Filesystem.getFile(textDir.resolve(), 'appicon7.jpeg');
imgFile.write(imgVw.toBlob(), false);
Ti.API.info('directory exists');
}
Ti.API.info('stop storing binary data');
};
Exercise -4
Identify the accessible storage locations on the filesystem
Identify the appropriate locations to store data on the filesystem
Determine when the filesystem is the most suitable storage location for your app’s data
No comments:
Post a Comment