2013-01-25 2 views
1

리소스 폴더 내에있는 Object Bank 디렉터리에있는 파일을 읽으려고합니다. 파일이 존재하지 않는 및 fileExistsAtPath:path 항상 파일이 존재 여부에 관계없이 yes를 반환하는 경우에도리소스 폴더에있는 파일을 읽을 수 없습니다 - iOS

Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0x8c410d0 {NSFilePath=/Users/ctb/Library/Application Support/iPhone Simulator/6.0/Applications/16881651-3790-4C87-A3A0-1E1D60563684/OAS IPAD.app/Object Bank/70118600.eam, NSStringEncoding=4} 

경로는 nil를 반환하지 않습니다

//Get path for file in object bank 
NSString* path = [[NSBundle mainBundle] pathForResource:fileId ofType:@"eam" inDirectory:@"Object Bank"]; 
NSError *error=nil; 

//Check if it is present at the location 
Boolean prepositioned=[[NSFileManager defaultManager] fileExistsAtPath:path]; 
NSLog(@"File Found : %@",[email protected]"Yes":@"No"); 

//This outputs "File Found : Yes" 

//Read file 
NSString *fileContents=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; 
NSLog(@"Prepostioned content length: %d",fileContents.length); 
//This outputs "Prepostioned content length: 0" 

//if file is present: 
if (!error) { 
    //Do something 

} 
else 
{ 
    //Print the error 
    NSLog(@"Error : %@",error.description); 
} 

나는 다음과 같은 오류를 받고 있어요 .

나뿐만 아니라이 시도했습니다

NSArray *paths =[[NSBundle mainBundle] pathsForResourcesOfType:@"eam" inDirectory:@"Object Bank"]; 
for (NSString *path in paths) { 
    NSString *fileString=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; 
    NSLog(@"Resource : %u ",fileString.length); 
} 

를하지만이 같은 문제가 있습니다.

+0

ducument의 디렉토리에서 읽고 싶습니까? –

답변

0

Resources 폴더를 읽지 않습니다. Object Bank 디렉토리에서 읽으려고합니다. 파일이 거기에 존재하지 않기 때문에 뒤로 nil 돌아 오는 것은 의미가 있습니다. 다음은

NSString *filePath = [[NSBundle mainBundle] pathForResource:fileId ofType:@"eam" inDirectory:@"Object Bank"]; 

NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if ([fileManager fileExistsAtPath:filePath]) { 
     NSLog(@"file exist"); 
    } 

내가 자원 디렉토리에서 pk라는 이름의 텍스트 파일을 읽으려고 스크린 샷입니다. 희망이 도움이됩니다. 해피 코딩 :-)

screenshot

+0

내 질문에 오해 한 것 같아 ... 리소스 폴더 안에 "Object Bank"라는 폴더가 있습니다. 필수 파일은 거기에 존재하거나 존재하지 않을 수 있습니다. 그러나 파일이있을 때에도 읽을 수 없습니다. 그리고'fileExistsAtPath'는 파일이 없어도 yes를 반환합니다. –

0

그렇지 않으면 당신은 그것이 아니라는 것을 말하고 있지에 오해의 소지 이러한 유형의 오류를해야합니다 절대적으로 당신의 파일이 NSUTF8StringEncoding입니다 읽을 것을 긍정적으로 확인 불일치 인코딩으로 인해 읽음

TextWrangler라고 말한 파일 (형식이 eam 인 fileId)을 열고 인코딩이 어떤 것인지 확인한 다음 인코딩 매개 변수에서 해당 파일을 사용하십시오.