Adobe Integrated Runtime (AIR) for JavaScript Developers Pocket Reference

(nextflipdebug5) #1

82 | Chapter 4: AIR Mini-Cookbook


function doLoad( )
{
var data = null;

file = air.File.applicationStorageDirectory.resolve(
"myFile.txt" );

stream = new air.FileStream( );
stream.open( file, air.FileMode.READ );
data = stream.readMultiByte( stream.bytesAvailable,
air.File.systemCharset );
stream.close( );

document.getElementById( "editor" ).value = data;
}
</script>

</head>
<body onload="doLoad( )">

<textarea id="editor"></textarea>

</body>
</html>

Asynchronously Reading Text from a File


Problem


You want to read a large amount of text into your application.


Solution


Use asynchronous file APIs to load the text, ensuring applica-
tion execution is not blocked while the file is being loaded.


Discussion


Files containing a large amount of data should be read using
theFileStream.openAsync( )method. This method opens a
file asynchronously for reading or writing and won’t block
additional user input. Asynchronous file operations achieve
this goal by raising events during processing. The result is

Free download pdf