从目录中获取文件到NSData

时间:2014-01-22 16:45:33

标签: ios iphone objective-c ios7 nsdata

我尝试将文档目录中自己创建的目录中的文件转换为NSData对象,如下所示:

NSString *path = [documentsDirectory stringByAppendingFormat:@"%@%@",@"/",fileName];

                NSError* errorr = nil;
                NSData *fileData = [NSData dataWithContentsOfFile:path options: 0 error: &errorr];
                if (fileData == nil)
                {
                    NSLog(@"Failed to read file, error %@", errorr);
                }
                else
                {

                }

但我总是得到这个错误:

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"}

所以,如果我检查文件是否可读:

if ([[NSFileManager defaultManager] isReadableFileAtPath: path]  == YES)
                    NSLog (@"File is readable");
                else
                    NSLog (@"File is read only");

我得到文件可读的结果,如果我想将该文件解析为NSData,为什么会出现错误?!

更新:我创建了我的文件:

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:&error];

NSString *documentsPath = [documentsDirectory
                           stringByAppendingPathComponent:path];
return documentsPath;

}

UPDATE2:创建文件后,我将其移动到具有此功能的另一个目录中:

- (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 {

}

}

移动文件后问题可能就是问题!因为如果我在移动它之前尝试将我的文件放入NSData,一切正常......

2 个答案:

答案 0 :(得分:3)

您似乎尝试将整个目录复制到目标文件中。结果是文件路径最后是一个目录路径,因此您无法像导致权限错误消息的文件那样打开它。

您应该将复制代码更新为

 while ((filename = [e nextObject])) {

            if ([[filename pathExtension] isEqualToString:extension]) {
                [[NSFileManager defaultManager] moveItemAtPath:[documentsDirectory stringByAppendingPathComponent: filename] toPath:[uploadDirectory stringByAppendingPathComponent:filename] error:NULL];

            }
        }

单独复制每个文件。这将创建一个目录路径打算的实际文件。

答案 1 :(得分:1)

您没有该文件的正确权限的可能性很高。请检查权限并为当前用户或“所有人”添加读取权限。您可以通过信息对话框(cmd + i)或使用chmod u+r test.xml在终端中为用户设置读取权限。最后你必须使用

设置所有者
sudo chown yourusername test.xml 

希望这是有道理的。