write to txt file- object problem
There is 2 files, tab1.js and myList.txt.
My only problem is that I can't write to myList.txt.
`
//this one to read the file
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory, 'myList.txt');
//this one to write to file
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'myList.txt');
var resources = JSON.parse(f.read().text);
var contents = f.read();
** test **
Ti.API.info('file = ' + f);
Ti.API.info('exists = ' + f.exists());
Ti.API.info('size = ' + f.size);
Ti.API.info('readonly = ' + f.readonly);
Ti.API.info('writeable = ' + f.writeable);
Ti.API.info('name = ' + f.name);
Ti.API.info("contents blob object = "+contents);
Ti.API.info('contents = ' + contents.text);
* end test **
function loadValues(){
for (var i=0; i<resources.list.length; i++){
var row = Ti.UI.createTableViewRow({
title:resources.list[i].stuff,
className:'newRowClass'});
data.push(row);
}
}
addButton.addEventListener('click', function(e)
{
var row = Ti.UI.createTableViewRow({});
var label = Ti.UI.createLabel({});
label.text = tf1.value;
while(resources.list[counter].stuff != "")//if there is something, loop until empty
{
counter++;
}
resources.list[counter].stuff = label.text;
file.write(JSON.stringify(resources));
row.add(label);
tableView.insertRowBefore(0,row);
});This is myList.txt
{"list":[
{"Row":"1","stuff":""},
{"Row":"2","stuff":""},
{"Row":"3","stuff":""},
{"Row":"4","stuff":""},
{"Row":"5","stuff":""}
]}
`
If I only use …resourcesDirectory, 'myList.txt');, it is ok on iPhone emulator, but not on android emulator or Android device.
But if I use …applicationDataDirectory,'myList.txt');,it happens something strange-I see that file is existing, but I get error nullPointerException, just like there is not an object..??? I get this error on both emulators and android device. I dont understand.. Can someone please try to explain what is with my code when read,and when write and tell me how to write to file? Should I specify what to write to file more times? If so, how is it possible that code is working if using …resourcesDirectory, 'myList.txt'); Please help!
Thanks!
INFO:
INFO] file = [object TiFilesystemFile]
[INFO] exists = true
[INFO] size = 1502
[INFO] readonly = true
[INFO] writeable = false
[INFO] name = myList.txt
[INFO] contents blob object = [object TiBlob]
[INFO] contents = null
[ERROR] Script Error = Result of expression 'resources' [null] is not an object. at myList.js (line 45).
1 Answer
-
The first issue is that the resources directory is read-only, so while you should be to perform a f.read() to grab the contents of a file, you will never be able to perform a f.write() when f points to a file in the resources folder.
It isn't clear to me why you need myList.txt but any copy in the resources folder will not be changeable via your code. You need to create it in the temp folder (if you don't need it to persist or get backed up), or in the application data folder if you want it to persist between sessions or get backed up during sync operations.