iOS 학습 노트(17)―파일 조작(NSFileManager)

6844 단어 iosnsfilemanager
iOS 의 샌 드 박스 메커니즘 은 자신의 응용 디 렉 터 리 에 있 는 파일 에 만 접근 할 수 있 습 니 다.iOS 는 안 드 로 이 드 와 달리 SD 카드 개념 이 없어 이미지,동 영상 등 콘 텐 츠 에 직접 접근 할 수 없다.iOS 응용 프로그램 에서 생 성 된 내용,예 를 들 어 이미지,파일,캐 시 내용 등 은 모두 자신의 샌 드 박스 에 저장 해 야 합 니 다.기본적으로 샌 드 박스 마다 3 개의 폴 더 가 있 습 니 다:Documents,Library,tmp.Library 는 Caches,Preferences 디 렉 터 리 를 포함 합 니 다.

위의 전체 경 로 는 사용자->자원 라 이브 러 리->Application Support->iPhone Simulator->6.1->Aplications 입 니 다.
Documents:애플 은 프로그램 이 만 든 파일 과 탐색 을 통 해 생 성 된 파일 데 이 터 를 이 디 렉 터 리 에 저장 하 는 것 을 권장 합 니 다.iTunes 백업 과 복구 시 이 디 렉 터 리 를 포함 합 니 다.
Library:프로그램의 기본 설정 이나 다른 상태 정 보 를 저장 합 니 다.
Library/caches:캐 시 파일 을 저장 하고 오래 지속 되 는 데 이 터 를 저장 합 니 다.업그레이드 나 응용 프로그램 이 닫 힌 데 이 터 를 저장 할 때 아 이 튠 즈 에 동기 화 되 지 않 기 때문에 동기 화 시간 을 줄 이기 위해 비교적 큰 파일 을 백업 할 필요 가 없 는 문 서 를 이 디 렉 터 리 에 두 는 것 을 고려 할 수 있 습 니 다.
tmp:임시 파일 을 즉시 만 드 는 곳 을 제공 하지만 오래 지속 되 지 않 아 도 됩 니 다.프로그램 이 닫 힌 후에 이 디 렉 터 리 의 데 이 터 는 삭 제 됩 니 다.프로그램 이 실행 되 지 않 을 때 시스템 이 삭 제 될 수도 있 습 니 다.

iOS 는 샌 드 박스 경 로 를 어떻게 가 져 옵 니까?파일 을 어떻게 조작 합 니까?다음은 답 을 드 리 겠 습 니 다.
응용 샌 드 박스 루트 가 져 오기:

-(void)dirHome{ 
  NSString *dirHome=NSHomeDirectory();   
  NSLog(@"app_home: %@",dirHome); 
} 
Documents 디 렉 터 리 경로 가 져 오기:

//  Documents   
-(NSString *)dirDoc{ 
  //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
  NSString *documentsDirectory = [paths objectAtIndex:0]; 
  NSLog(@"app_home_doc: %@",documentsDirectory); 
  return documentsDirectory; 
} 
라 이브 러 리 디 렉 터 리 경로 가 져 오기:

//  Library   
-(void)dirLib{ 
  //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"]; 
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); 
  NSString *libraryDirectory = [paths objectAtIndex:0]; 
  NSLog(@"app_home_lib: %@",libraryDirectory); 
} 
캐 시 디 렉 터 리 경로 가 져 오기:

//  Cache   
-(void)dirCache{ 
  NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
  NSString *cachePath = [cacPath objectAtIndex:0]; 
  NSLog(@"app_home_lib_cache: %@",cachePath); 
} 
Tmp 디 렉 터 리 경로 가 져 오기:

//  Tmp   
-(void)dirTmp{ 
  //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"]; 
  NSString *tmpDirectory = NSTemporaryDirectory(); 
  NSLog(@"app_home_tmp: %@",tmpDirectory); 
} 
폴 더 만 들 기:

//      
-(void *)createDir{ 
  NSString *documentsPath =[self dirDoc]; 
  NSFileManager *fileManager = [NSFileManager defaultManager]; 
  NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; 
  //      
  BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil]; 
  if (res) { 
    NSLog(@"       "); 
  }else 
    NSLog(@"       "); 
 } 
파일 생 성

 //     
-(void *)createFile{ 
  NSString *documentsPath =[self dirDoc]; 
  NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; 
  NSFileManager *fileManager = [NSFileManager defaultManager]; 
  NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; 
  BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil]; 
  if (res) { 
    NSLog(@"      : %@" ,testPath); 
  }else 
    NSLog(@"      "); 
} 
파일 로 데이터 쓰기:

//    
-(void)writeFile{ 
  NSString *documentsPath =[self dirDoc]; 
  NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; 
  NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; 
  NSString *content=@"      !"; 
  BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; 
  if (res) { 
    NSLog(@"      "); 
  }else 
    NSLog(@"      "); 
} 
파일 데이터 읽 기:

//    
-(void)readFile{ 
  NSString *documentsPath =[self dirDoc]; 
  NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; 
  NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; 
//  NSData *data = [NSData dataWithContentsOfFile:testPath]; 
//  NSLog(@"      : %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); 
  NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil]; 
  NSLog(@"      : %@",content); 
} 
파일 속성:

//     
-(void)fileAttriutes{ 
  NSString *documentsPath =[self dirDoc]; 
  NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; 
  NSFileManager *fileManager = [NSFileManager defaultManager]; 
  NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; 
  NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];   
  NSArray *keys; 
  id key, value; 
  keys = [fileAttributes allKeys]; 
  int count = [keys count]; 
  for (int i = 0; i < count; i++) 
  { 
    key = [keys objectAtIndex: i]; 
    value = [fileAttributes objectForKey: key]; 
    NSLog (@"Key: %@ for value: %@", key, value); 
  } 
} 
파일 삭제:

//     
-(void)deleteFile{ 
  NSString *documentsPath =[self dirDoc]; 
  NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; 
  NSFileManager *fileManager = [NSFileManager defaultManager]; 
  NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];   
  BOOL res=[fileManager removeItemAtPath:testPath error:nil]; 
  if (res) { 
    NSLog(@"      "); 
  }else 
    NSLog(@"      ");   
  NSLog(@"      : %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO"); 
} 
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기