Titanium Community Questions & Answer Archive

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

iPad simulator crashes when trying to create a File

Hello I've run into this problem which is most probably my fault but any help/advice would be great.
I've got this piece of code which I run after user pushes the button, it does take text from the Textarea and it should create a text file inside appsDir for later usage.

//listen to tap
btn1.addEventListener("click",function(){
    var text = txtfd.value;

    if(text !== null)
    {
        var f = Titanium.Filesystem.createFile("test.txt");
             f.createFile();
            f.write(text);
            if(f.exists())
            {
                 alertDialog = Titanium.UI.createAlertDialog({
                    title: 'Be Happy!',
                    message: 'File exists!',
                    buttonNames: ['Yeah!']
                });
                alertDialog.show();
            }
            else
            {
                  alertDialog = Titanium.UI.createAlertDialog({
                    title: 'Be Worried!',
                    message: 'File does not exist!',
                    buttonNames: ['Doh!']
                });
                alertDialog.show();
            }
    }
});
— asked October 7th 2010 by Lukas Skrabak
  • file
  • filesystem
  • ipad
0 Comments

1 Answer

  • Accepted Answer

    Try this

    //listen to tap
    btn1.addEventListener("click",function(){
        var text = txtfd.value;
    
        if(text !== null)
        {
                var f = Titanium.Filesystem.getFile('test.txt');
                f.write(text);
    
                if(f.exists())
                {
                     alertDialog = Titanium.UI.createAlertDialog({
                        title: 'Be Happy!',
                        message: 'File exists!',
                        buttonNames: ['Yeah!']
                    });
                    alertDialog.show();
                }
                else
                {
                      alertDialog = Titanium.UI.createAlertDialog({
                        title: 'Be Worried!',
                        message: 'File does not exist!',
                        buttonNames: ['Doh!']
                    });
                    alertDialog.show();
                }
        }
    });
    
    — answered October 7th 2010 by John McKnight
    permalink
    0 Comments
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.