title: Get File from Directory into NSData
body: <p>I try to get my file in my self created Directory in the Documents Directory into a NSData Object like this:</p>
  
  <pre><code>NSString *path = [documentsDirectory stringByAppendingFormat:@"%@%@",@"/",fileName];
  
                  NSError* errorr = nil;
                  NSData *fileData = [NSData dataWithContentsOfFile:path options: 0 error: &amp;errorr];
                  if (fileData == nil)
                  {
                      NSLog(@"Failed to read file, error %@", errorr);
                  }
                  else
                  {
  
                  }
  </code></pre>
  
  <p>But i always get this error:</p>
  
  <pre><code>Failed to read file, error Error Domain=NSCocoaErrorDomain Code=257 "The operation couldn’t be completed. (Cocoa error 257.)" UserInfo=0x156be110 {NSFilePath=/var/mobile/Applications/679253E3-652C-45EE-B589-609E52E4E3B0/Documents/upload/test.xml, NSUnderlyingError=0x156ba7f0 "The operation couldn’t be completed. Permission denied"}
  </code></pre>
  
  <p>So if i check if the file is readable:</p>
  
  <pre><code>if ([[NSFileManager defaultManager] isReadableFileAtPath: path]  == YES)
                      NSLog (@"File is readable");
                  else
                      NSLog (@"File is read only");
  </code></pre>
  
  <p>i get the result that the file is readable, so why do i get the error if i want to parse that file into NSData?!</p>
  
  <p>UPDATE: i created my file like this:</p>
  
  <pre><code>NSString *filePath = [self dataFilePath:fileName];
  
  [xmlData writeToFile:filePath atomically:YES];
  
  - (NSString *)dataFilePath: (NSString *) path {
  
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                       NSUserDomainMask, YES);
  NSString *documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"upload"];
  
  NSError *error = nil;
  if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory])
      [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:NO attributes:nil error:&amp;error];
  
  NSString *documentsPath = [documentsDirectory
                             stringByAppendingPathComponent:path];
  return documentsPath;
  
  }
  </code></pre>
  
  <p>UPDATE2: After creating the file, i move it into another Directory with this function:</p>
  
  <pre><code>- (void)moveXmlFilesToUploadDirectory
  {
  @try {
      //Check if FILES_DIR exists
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                           NSUserDomainMask, YES);
      NSString *documentsDirectory = [[paths objectAtIndex:0]     stringByAppendingPathComponent:FILES_DIR];//filesDir
      NSString *uploadDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:UPLOAD_DIR];//filesDir
  
      if ([[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory])
      {
          NSString *extension = @"xml";
          NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL];
          NSEnumerator *e = [contents objectEnumerator];
          NSString *filename;
          while ((filename = [e nextObject])) {
  
              if ([[filename pathExtension] isEqualToString:extension]) {
                  [[NSFileManager defaultManager] moveItemAtPath:documentsDirectory toPath:[uploadDirectory stringByAppendingPathComponent:filename] error:NULL];
  
              }
          }
      }
  
  }
  @catch (NSException *exception) {
  
  }
  @finally {
  
  }
  
  }
  </code></pre>
  
  <p>Maybe the Problem is after moving the file! Cause if i try to get my file into a NSData before i move it, everything works...</p>