iOS 학습 노트(17)―파일 조작(NSFileManager)
6844 단어 iosnsfilemanager
위의 전체 경 로 는 사용자->자원 라 이브 러 리->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");
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Swift의 패스트 패스Objective-C를 대체하기 위해 만들어졌지만 Xcode는 Objective-C 런타임 라이브러리를 사용하기 때문에 Swift와 함께 C, C++ 및 Objective-C를 컴파일할 수 있습니다. Xcode는 S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.