8 Replies Latest reply on Aug 28, 2015 5:30 PM by shawnynicole87

    fileStream.writeUTFBytes - Text String Line Brake ....

    aktell2007 Level 1

      My Text String in the AIR 'fileStream.writeUTFBytes' needs several 'Line Brakes' ??? I ave been trying everything what I could find, but with NO luck !

           fileStream.open(newFile, FileMode.WRITE);
           fileStream.writeUTFBytes("One; Two; Three; Four; dummy; ");
           // This second line I included for testing only, but does not to be in there as is !
           fileStream.writeUTFBytes("Five; Six; Seven; Eight; dummy; ");
           fileStream.close();
      
      

      This is what I need to come up in a TEXT .txt file !!!

      One; Two; Three; Four; dummy;
      Five; Six; Seven; Eight; dummy;
      
      

      Any help with this would be very much appriciated ! regards aktell 2007

        • 1. Re: fileStream.writeUTFBytes - Text String Line Brake ....
          miguel8312 Level 3

          have you tried using the carriage return and the line feed?"\r", "\n"  or a combination there of?

          Miguel

          • 2. Re: fileStream.writeUTFBytes - Text String Line Brake ....
            aktell2007 Level 1

            Thank for the reply, BUT I stated:

             

            I have been trying everything what I could find, AND I WOULD KNOW OF, but with NO luck !
            And \n etc. would have been the first one not mentioning the others e.g. 60 variations or so I have tried !

             

            I believe that maybe it has something to do with the  ' ; ' semi colon's I need to use ?

            • 3. Re: fileStream.writeUTFBytes - Text String Line Brake ....
              miguel8312 Level 3
              <?xml version="1.0" encoding="utf-8"?>
              <s:Application
              
               xmlns:fx="http://ns.adobe.com/mxml/2009"                   xmlns:s="
              
              library://ns.adobe.com/flex/spark"                   xmlns:mx="
              
              library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
                                applicationComplete="initData();"
              
              >
              
              
              <fx:Declarations>
              
              
              <!-- Place non-visual elements (e.g., services, value objects) here -->
              
              
              </fx:Declarations>
              
              
              
              <fx:Script>
              
                        <![CDATA[
              
              import flash.net.FileReference;
              
              
              
              import flash.events.IOErrorEvent;
              
              import flash.events.Event;
              
              
              private static const DEFAULT_FILE_NAME:String = "example.txt";
              
              
              import mx.collections.ArrayCollection;
              
              
              //Create a new variable and type it as an ArrayCollection
              
              public var itemsAC:ArrayCollection;
              
              
              
              //Create the initData method, invoked on creationComplete from the Application tag
              
              private function initData():void {
                                  Texttosave.text = 
              
              'One; Two; Three; Four; dummy;'+"\n"+"Five; Six; Seven; Eight; dummy;"
              
                             }
              
              
              //FileReference Class well will use to save data
              
              private var fr:FileReference;
              
              
              /********** UI Event Handlers **************/
              
              
              
              //called when the users types in the textarea
              
              //note valueCommit should be used, but is broken in the flex build 
              
              //I am using
              
              
              
              //called when the user clicks the load file button
              
              private function onSaveClick():void
                             {
              
              //create the FileReference instance
                                  fr = 
              new FileReference();
              
              
              //listen for the file has been saved
                                  fr.addEventListener(Event.COMPLETE, onFileSave);
              
              
              //listen for when then cancel out of the save dialog
                                  fr.addEventListener(Event.CANCEL,onCancel);
              
              
              //listen for any errors that occur while writing the file
                                  fr.addEventListener(IOErrorEvent.IO_ERROR, onSaveError);
              
              
              //open a native save file dialog, using the default file name
                                  fr.save(Texttosave.text, DEFAULT_FILE_NAME);
                             }
              
              
              /***** File Save Event Handlers ******/
              
              
              
              //called once the file has been saved
              
              private function onFileSave(e:Event):void
                             {
              
              trace("File Saved");                    fr = 
              
              null;               }
              
              
              
              //called if the user cancels out of the file save dialog
              
              private function onCancel(e:Event):void
                             {
              
              trace("File save select canceled.");                    fr = 
              
              null;
                             }
              
              
              
              //called if an error occurs while saving the file
              
              private function onSaveError(e:IOErrorEvent):void
                             {
              
              trace("Error Saving File : " + e.text);                    fr = 
              
              null;
                             }
                        ]]>
              
              
              </fx:Script>
              
              
              
              
              <s:TextArea id="Texttosave" x="10" y="10">
              
              
              
              </s:TextArea>
              
              
              
              
              <mx:Button label="Save File" right="10" bottom="10" id="saveButton"                  click="onSaveClick()" enabled="
              
              true"/>
              
              
              </s:Application>
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              


              Why dont you try this instead?

              Miguel

              • 4. Re: fileStream.writeUTFBytes - Text String Line Brake ....
                aktell2007 Level 1

                Well, what can I say 'Thanks a lot' for all that work, BUT the Application I'm working this on is still from the Flex 3 Stone Age !

                • 5. Re: fileStream.writeUTFBytes - Text String Line Brake ....
                  miguel8312 Level 3

                  ok well how about this then all you would have to do to your flex  project is right click the project then

                  click "properties" then "flex compiler" on the right hand side you should see somehting that says "use a specific version" click on that button then  type 10.0.0

                   

                  then this could should work

                   

                   

                   

                  look here

                  ?xml version="1.0" encoding="utf-8"?>
                  <mx:Application
                  
                   xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600" applicationComplete="initData();">
                  
                  
                  
                  
                  <mx:Script>
                  
                            <![CDATA[
                  
                  import flash.net.FileReference;
                  
                  
                  import flash.events.IOErrorEvent;
                  
                  import flash.events.Event;
                  
                  private static const DEFAULT_FILE_NAME:String = "example.txt";
                  
                  
                  import mx.collections.ArrayCollection;
                  
                  
                  //Create a new variable and type it as an ArrayCollection
                  
                  public var itemsAC:ArrayCollection;
                  
                  
                  
                  //Create the initData method, invoked on creationComplete from the Application tag
                  
                  private function initData():void {
                            Texttosave.text = 
                  
                  'One; Two; Three; Four; dummy;'+"\n"+"Five; Six; Seven; Eight; dummy;"
                  
                            }
                  
                  
                  //FileReference Class well will use to save data
                  
                  private var fr:FileReference;
                  
                  
                  
                  /********** UI Event Handlers **************/
                  
                  
                  
                  //called when the users types in the textarea
                  
                  //note valueCommit should be used, but is broken in the flex build 
                  
                  //I am using
                  
                  
                  
                  //called when the user clicks the load file button
                  
                  private function onSaveClick():void
                            {
                  
                  //create the FileReference instance
                            fr = 
                  new FileReference();
                  
                  
                  //listen for the file has been saved
                            fr.addEventListener(Event.COMPLETE, onFileSave);
                  
                  
                  //listen for when then cancel out of the save dialog
                            fr.addEventListener(Event.CANCEL,onCancel);
                  
                  
                  //listen for any errors that occur while writing the file
                            fr.addEventListener(IOErrorEvent.IO_ERROR, onSaveError);
                  
                  
                  //open a native save file dialog, using the default file name
                            fr.save(Texttosave.text, DEFAULT_FILE_NAME);
                            }
                  
                  
                  /***** File Save Event Handlers ******/
                  
                  
                  
                  //called once the file has been saved
                  
                  private function onFileSave(e:Event):void
                            {
                  
                  trace("File Saved");
                            fr = 
                  
                  null;
                            }
                  
                  
                  
                  //called if the user cancels out of the file save dialog
                  
                  private function onCancel(e:Event):void
                            {
                  
                  trace("File save select canceled.");          fr = 
                  
                  null;
                            }
                  
                  
                  
                  //called if an error occurs while saving the file
                  
                  private function onSaveError(e:IOErrorEvent):void
                            {
                  
                  trace("Error Saving File : " + e.text);
                            fr = 
                  
                  null;          }
                            ]]>
                  
                  
                  </mx:Script>
                  
                  
                  
                  
                  <mx:TextArea id="Texttosave" x="10" y="10"/>
                  
                  
                  
                  
                  
                  
                  <mx:Button label="Save File" right="10" bottom="10" id="saveButton"                  click="onSaveClick()" enabled="
                  
                  true"/>
                  
                  
                  
                  
                  
                  </mx:Application>
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  • 6. Re: fileStream.writeUTFBytes - Text String Line Brake ....
                    aktell2007 Level 1

                    Well, Thanks again just had to do a little more research so ! It just looked to familiar that code !

                    http://www.mikechambers.com/blog/2008/08/20/reading-and-writing-local-files-in-flash-player-10/
                    

                    and " Mike Chambers "  also stated in that write up ....

                    "While it will also be possible to use these APIs in Adobe AIR, in general, you will want to use the AIR File
                    APIs as they provide more functionality and flexibility."
                    

                    Anyway, doing some more research I was able to find something more AIR suitable !!!

                    http://www.aaronwest.net/blog/index.cfm/2007/8/12/Working-With-the-File-System-API-in-AIR
                    
                    Write UTF-8 encoded string data to a file on the user's desktop asynchronously.
                    
                    import flash.filesystem.*;
                    import flash.events.Event;
                    
                    // Create a reference to MyFile.txt on the user's desktop.
                    
                    var myFile:File = File.desktopDirectory.resolve("MyFile.txt");
                    
                    // Create a new FileStream instance.
                    
                    var fs:FileStream = new FileStream();
                    
                    // Add the function fileWrittenComplete as a close listener to the file stream.
                    
                    fs.addEventListener(Event.CLOSE, fileWrittenComplete);
                    
                    // Create a String we'll use to write inside the file.
                    
                    var myString:String = "Here's a string of text.";
                    
                    // Open MyFile.txt in write-only asynchronous mode.
                    
                    fs.openAsync(myFile, FileMode.WRITE);
                    
                    // Write the string to the file asynchronously.
                    
                    fs.writeUTFBytes(myString);
                    
                    // Close the file.
                    
                    fs.close();
                    
                    // Event handler for when the file has been written.
                    
                    public function fileWrittenComplete(event:Event):void {
                        trace("MyFile.txt has been written to the file system.");
                    }
                    
                    In addition to writing textual data to the file system, the FileStream class allows you to write binary data
                    (such as a JPEG) or a full ActionScript class or generic Object to the file system. The overall File API
                    included in Flex 3 is relatively easy to work with and extremely powerful. Using it properly opens your desktop
                    applications to a whole new realm of possibilities. Stay tuned in the near future to read more about real-world
                    ways to utilize these new classes in your applications.
                    

                     

                    Thanks again aktell2007

                    • 7. Re: fileStream.writeUTFBytes - Text String Line Brake ....
                      Aaron West

                      Glad you found the answer aktell2007... and more importantly the answer was on my blog. =)

                      1 person found this helpful
                      • 8. Re: fileStream.writeUTFBytes - Text String Line Brake ....
                        shawnynicole87
                        1. fileStream.open(newFile, FileMode.APPEND); 
                        2. fileStream.writeUTFBytes("One; Two; Three; Four; dummy; " + File.lineEnding); 
                        3. fileStream.writeUTFBytes("Five; Six; Seven; Eight; dummy; " + File.lineEnding); 
                        4. fileStream.close();

                         

                        FileMode.APPEND > Adobe AIR 1.5 * FileStream open modes

                        File.lineEnding > Adobe AIR 1.5 * Getting file system information