Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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({

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
     resources.list[counter].stuff = label.text;


This is myList.txt
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!



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).

— asked November 13th 2010 by Marijana Teljega
  • file
  • mobile
  • object
  • text
  • write

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.

    — answered November 13th 2010 by Doug Handy
    1 Comment
    • Oh sorry,I forgot to tell what my app should do. I want to save stuff in myList.txt ). In application there is a textField, where you enter some stuff to buy, and this will be saved in myList.txt. There is also a tableview (in same window as textField), which updates(read) from myList.txt every time you enter a new stuff . It should be possible to read list after a device is restarted. That is why I have to both read and write to file.
      I have myList.txt saved in application folder.

      — commented November 14th 2010 by Marijana Teljega
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.