1 Reply Latest reply on Jul 19, 2011 7:41 AM by philipwi

    Accessing parent or sibling directories in Flex.

    philipwi

      Hello,

       

      For our project setup I have been asked to read a file from a sibling directory, something like ("../sibling/my-file"). Unfortunately my testing seems to indicate that this is impossible. Consider the following code:

       

      private function init() : void {
                      resultsArea.text += readDocFile("HelloWorld.txt") + "\n\n";
                      resultsArea.text += readDocFile("child/HelloWorld.txt") + "\n\n";
                      resultsArea.text += readDocFile("../HelloWorld.txt") + "\n\n";
                      resultsArea.text += readDocFile("../sibling/HelloWorld.txt") + "\n\n";
                  }
                 
                  public function readDocFile(fileName:String):String
                  {
                      var data:String = null;
                      try {
                          var file:File = File.applicationDirectory.resolvePath(fileName);
                          data = file.nativePath + " ";
                          data += String(file.exists) + " " + String(file.isDirectory) + " ";
                          var fileStr:FileStream = new FileStream();
                          fileStr.open(file, FileMode.READ);
                          data += fileStr.readUTFBytes(fileStr.bytesAvailable);
                      }
                      catch (error:Error) {
                          data += error.message;
                      }
                      return data;
                  }

       

      On the following file structure:

      .
      ./.actionScriptProperties
      ./.flexProperties
      ./.project
      ./.settings
      ./.settings/org.eclipse.core.resources.prefs
      ./bin-debug
      ./bin-debug/child
      ./bin-debug/child/HelloWorld.txt
      ./bin-debug/FileTesting-app.xml
      ./bin-debug/FileTesting.swf
      ./bin-debug/HelloWorld.txt
      ./HelloWorld.txt
      ./libs
      ./sibling
      ./sibling/HelloWorld.txt
      ./src
      ./src/child
      ./src/child/HelloWorld.txt
      ./src/FileTesting-app.xml
      ./src/FileTesting.mxml
      ./src/HelloWorld.txt

       

      With this result:

       

      C:\eclipse\3.6.1_j2ee\workspace\FileTesting\bin-debug\HelloWorld.txt true false Hello adjacent

      C:\eclipse\3.6.1_j2ee\workspace\FileTesting\bin-debug\child\HelloWorld.txt true false Hello Child

      C:\eclipse\3.6.1_j2ee\workspace\FileTesting\bin-debug true true Error #3006: Not a file.

      C:\eclipse\3.6.1_j2ee\workspace\FileTesting\bin-debug true true Error #3006: Not a file.

       

      We can see that that everything after the "../" in the path structure is being ignored. I've tried variants using the parent property of the File.applicationDirectory without success.

       

       

      If someone could point me to some documentation that confirms that I cannot access a parent directory or otherwise let me know what I am doingwrong it would be greatly appreciated.

       

      Thanks,

       

      Philip

        • 1. Re: Accessing parent or sibling directories in Flex.
          philipwi Level 1

          More interesting discoveries:

           

          private function init() : void {
                          //Works
                          resultsArea.text += readDocFile("HelloWorld.txt") + "\n\n";
                          //Works
                          resultsArea.text += readDocFile("child/HelloWorld.txt") + "\n\n";
                          //Works
                          resultsArea.text += readDocFile("child/../child/HelloWorld.txt") + "\n\n";
                          //Fails
                          resultsArea.text += readDocFile("../HelloWorld.txt") + "\n\n";
                          //Fails
                          resultsArea.text += readDocFile("../sibling/HelloWorld.txt") + "\n\n";
                          //Works
                          resultsArea.text += readDocAbsolute("C:\\eclipse\\3.6.1_j2ee\\workspace\\FileTesting\\sibling\\HelloWorld.txt ") + "\n\n";
                          //Works
                          resultsArea.text += readDocCheating("../sibling/HelloWorld.txt") + "\n\n";
                          //Works
                          resultsArea.text += readDocFinal("../sibling/HelloWorld.txt") + "\n\n";
                      }
                     
                      public function readDocFile(relativePath:String):String
                      {
                          var data:String = null;
                          try {
                              var file:File = File.applicationDirectory.resolvePath(relativePath);
                              data = file.nativePath + " ";
                              data += String(file.exists) + " " + String(file.isDirectory) + " ";
                              var fileStr:FileStream = new FileStream();
                              fileStr.open(file, FileMode.READ);
                              data += fileStr.readUTFBytes(fileStr.bytesAvailable);
                          }
                          catch (error:Error) {
                              data += error.message;
                          }
                          return data;
                      }
                     
                      public function readDocCheating(relativePath:String) : String {
                          var data:String = null;
                          try {
                              var basePath:String = File.applicationDirectory.nativePath;
                              var file:File = new File(basePath + "/" + relativePath);
                              data = file.nativePath + " ";
                              data += String(file.exists) + " " + String(file.isDirectory) + " ";
                              var fileStr:FileStream = new FileStream();
                              fileStr.open(file, FileMode.READ);
                              data += fileStr.readUTFBytes(fileStr.bytesAvailable);
                          }
                          catch (error:Error) {
                              data += error.message;
                          }
                          return data;
                      }
                     
                      public function readDocAbsolute(absolutePath:String) : String {
                          var data:String = null;
                          try {
                              var file:File = new File(absolutePath);
                              data = file.nativePath + " ";
                              data += String(file.exists) + " " + String(file.isDirectory) + " ";
                              var fileStr:FileStream = new FileStream();
                              fileStr.open(file, FileMode.READ);
                              data += fileStr.readUTFBytes(fileStr.bytesAvailable);
                          }
                          catch (error:Error) {
                              data += error.message;
                          }
                          return data;
                      }
                     
                      public function readDocFinal(relativePath:String ) : String {
                          var data:String = null;
                          try {
                              var baseFile:File = new File(File.applicationDirectory.nativePath);
                              var file:File = baseFile.resolvePath(relativePath);
                              data = file.nativePath + " ";
                              data += String(file.exists) + " " + String(file.isDirectory) + " ";
                              var fileStr:FileStream = new FileStream();
                              fileStr.open(file, FileMode.READ);
                              data += fileStr.readUTFBytes(fileStr.bytesAvailable);
                          }
                          catch (error:Error) {
                              data += error.message;
                          }
                          return data;
                      }

           

          Where those calls denoted as "works" in the init method work as expected. I think I've come to the conclusion that the static file instances are "special" and their parenting works differently from files I create myself.