NSTemporaryDirectory 临时文件

唯一标识 :

 NSString*identifier=[[NSProcessInfoprocessInfo]globallyUniqueString];

 

创建临时文件路径:

NSString *fileName=[NSStringstringWithFormat:@"%@_%@", [[NSProcessInfoprocessInfo] globallyUniqueString], @"file.txt"];

NSURL *fileURL=[NSURLfileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:fileName]];

 

创建临时文件:

NSURL *directoryURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]] isDirectory:YES];
[[NSFileManager defaultManager] createDirectoryAtURL:directoryURL withIntermediateDirectories:YES attributes:nil error:&error];

 

写入临时文件:

NSData *data = ...;
NSError *error = nil;
[data writeToURL:fileURL options:NSDataWritingAtomic error:&error];
NSOutputStream *outputStream = [NSOutputStream outputStreamToFileAtPath:[fileURL absoluteString] append:NO];

 

清理:

NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtURL:fileURL error:&error];

 

posted @ 2014-03-11 12:09  翛尧  阅读(3669)  评论(0编辑  收藏  举报