본문 바로가기

iPhone

Using The Document Directory To Store Files

출처 : http://www.ios-developer.net/iphone-ipad-programmer/development/file-saving-and-loading/using-the-document-directory-to-store-files

Does File Exist


	NSString *path;
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectory"];
	path = [path stringByAppendingPathComponent:@"SomeFileName"];
	if ([[NSFileManager defaultManager] fileExistsAtPath:path])
	{

Delete File


	NSString *path;
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectory"];
	path = [path stringByAppendingPathComponent:@"SomeFileName"];
	NSError *error;
	if ([[NSFileManager defaultManager] fileExistsAtPath:path])		//Does file exist?
	{
		if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error])	//Delete it
		{
			NSLog(@"Delete file error: %@", error);
		}
	}

Delete DIrectory


	NSString *path;
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectoryName"];
	NSError *error;	

	if ([[NSFileManager defaultManager] fileExistsAtPath:path])	//Does directory exist?
	{
		if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error])	//Delete it
		{
			NSLog(@"Delete directory error: %@", error);
		}
	}

Create Directory


	NSString *path;
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectoryName"];
	NSError *error;
	if (![[NSFileManager defaultManager] fileExistsAtPath:path])	//Does directory already exist?
	{
		if (![[NSFileManager defaultManager] createDirectoryAtPath:path
									   withIntermediateDirectories:NO
														attributes:nil
															 error:&error])
		{
			NSLog(@"Create directory error: %@", error);
		}
	}

Save NSData File


	NSData *file;
	file = ...
	NSString *filePath;
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectoryName"];
	filePath = [path stringByAppendingPathComponent:@"SomeFileName];

	[[NSFileManager defaultManager] createFileAtPath:filePath
											contents:file
										  attributes:nil];

Move File


	[[NSFileManager defaultManager] moveItemAtPath:MySourcePath toPath:MyDestPath error:nil];

List Files


    //----- LIST ALL FILES -----
    NSLog(@"LISTING ALL FILES FOUND");

    int Count;
    NSString *path;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectoryName"];
    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
    for (Count = 0; Count < (int)[directoryContent count]; Count++)
    {
        NSLog(@"File %d: %@", (Count + 1), [directoryContent objectAtIndex:Count]);
    }

 

'iPhone' 카테고리의 다른 글

iPhone Local Notification Sample  (0) 2011.08.26
iPhone Document 불러오기/저장/삭제 예제  (0) 2011.08.26
iphone 로컬라이징  (0) 2011.08.24
custom keyboard  (0) 2011.08.24
개발자료 정리  (0) 2011.08.24