Created
August 4, 2011 00:35
-
-
Save jonalter/1124244 to your computer and use it in GitHub Desktop.
Test writing files to filesystem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var win = Ti.UI.createWindow({ | |
| backgroundColor: 'blue', | |
| layout: 'vertical' | |
| }); | |
| var testFile; | |
| var label = Ti.UI.createLabel({ | |
| top: 10, | |
| height: 100, | |
| width: 300, | |
| backgroundColor: "green", | |
| text: "Read the text file already!" | |
| }); | |
| win.add(label); | |
| var tfield = Ti.UI.createTextField({ | |
| top: 10, | |
| height: 40, | |
| width: 200, | |
| borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED, | |
| value: "text written via write()" | |
| }); | |
| tfield.addEventListener('return', function(e){ | |
| tfield.blur(); | |
| }); | |
| win.addEventListener('click', function(e){ | |
| tfield.blur(); | |
| }); | |
| win.add(tfield); | |
| var createButton = Ti.UI.createButton({ | |
| top: 20, | |
| height: 50, | |
| width: 200, | |
| title: "Create File" | |
| }); | |
| createButton.addEventListener('click', function(e){ | |
| testFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'text.txt'); | |
| }); | |
| win.add(createButton); | |
| var writeButton = Ti.UI.createButton({ | |
| top: 20, | |
| height: 50, | |
| width: 200, | |
| title: "Write File" | |
| }); | |
| writeButton.addEventListener('click', function(e){ | |
| Ti.API.info('text.txt exists? ' + testFile.exists()); | |
| Ti.API.info('text.txt size: ' + testFile.size + ' bytes'); | |
| if(!testFile.write(tfield.value)) { | |
| Ti.API.info("could not write string to file."); | |
| } | |
| }); | |
| win.add(writeButton); | |
| var readButton = Ti.UI.createButton({ | |
| top: 20, | |
| height: 50, | |
| width: 200, | |
| title: "Read File" | |
| }); | |
| readButton.addEventListener('click', function(e){ | |
| Ti.API.info('text.txt exists? ' + testFile.exists()); | |
| Ti.API.info('text.txt size: ' + testFile.size + ' bytes'); | |
| var text = testFile.read().text; | |
| Ti.API.info('#### Text of testFile: ['+text+']'); | |
| label.text = text; | |
| }); | |
| win.add(readButton); | |
| var deleteButton = Ti.UI.createButton({ | |
| top: 20, | |
| height: 50, | |
| width: 200, | |
| title: "Delete File" | |
| }); | |
| deleteButton.addEventListener('click', function(e){ | |
| testFile.deleteFile(); | |
| }); | |
| win.add(deleteButton); | |
| win.open(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment