New topic: Example code to write a file (new approach, not deprecated)
<http://forums.realsoftware.com/viewtopic.php?t=34402> Page 1 of 1 [ 1 post ] Previous topic | Next topic Author Message basil.bourque Post subject: Example code to write a file (new approach, not deprecated)Posted: Fri Jun 25, 2010 4:46 pm Joined: Thu Apr 15, 2010 10:41 pm Posts: 35 Location: Seattle-area It took me a while to determine that calling "CreateXXX" methods on FolderItem is now deprecated. I wrote the following example code to use as a template for future use. This code creates a text file by calling on the TextOutputStream class rather than on the FolderItem object. I'm sharing this code in case it might help others understand the new approach. And it might save some work typing. ---------------- Code:// Example code to write a file. © 2010 Basil Bourque. // This source code may be used freely by anyone taking full responsibility for doing so. // Purpose: Example showing how to use the newer way to create a file from a Stream class // rather than using the deprecated "CreateXXX" methods on a FolderItem object. // Creating a binary file is similar, using the BinaryStream class rather than TextOutputStream or TextInputStream. dim now as Date = new Date dim f As FolderItem dim t as TextOutputStream f = GetFolderItem("Sample_"+now.SQLDate+".txt") Select Case f.LastErrorCode Case FolderItem.NoError // Normal. No code needed here. Case FolderItem.FileNotFound // Normal. No code needed here. // Not really an error if we are going to create a new file, as we do in code below. // You would test for this error if you absolutely expected a file to be (a) new or (b) pre-existing. Case FolderItem.AccessDenied MsgBox "ERROR Access denied for FolderItem: " + f.ShellPath Case FolderItem.NotEnoughMemory MsgBox "ERROR Out of memory for FolderItem: " + f.ShellPath Case FolderItem.FileInUse MsgBox "ERROR File in use for FolderItem: " + f.ShellPath Case FolderItem.InvalidName MsgBox "ERROR Invalid name for FolderItem: " + f.ShellPath Case FolderItem.DestDoesNotExistError // Only happens on CopyFileTo and MoveFileTo. So should not happen. MsgBox "ERROR Should not have been able to get error code FolderItem.DestDoesNotExistError." Else // Catch-all for any other errors presented by host OS' filesystem, or for errors added to REALbasic in the future. MsgBox "ERROR Unexpected error code # " + Str(f.LastErrorCode) + " for FolderItem: " + f.ShellPath End Select t = TextOutputStream.Create(f) // Creates (or overwrites) the file at this point. dim packet as String = "At the tone the time will be: " + now.SQLDateTime t.Write packet // Write data to file. // t.Write (ConvertEncoding(packet, Encodings.MacRoman)) // To encode the text in other than UTF-8. t.close // Close the file to cleanup and free resources. Exception err as IOException MsgBox "ERROR - IOException occurred when creating, opening, or writing to file." // fin. _________________ --Basil Bourque (Using REAL Studio 2010 release 2 with REAL Server 2009r1.2 on Mac OS X 10.6.2 & .3) Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 1 post ]
-- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
