background image

stringByAppendingPathComponent:@"file2.txt"];
//判断是否移动
if ([fileMgr moveItemAtPath:filePath toPath:filePath2 error:&error] != YES)
NSLog(@"Unable to move file: %@", [error localizedDescription]);
//显示文件目录的内容
NSLog(@"Documentsdirectory: %@",
[fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);

3、删除一个文件
  
为了使这个技巧完整,让我们再一起看下如何删除一个文件:
//在 filePath2 中判断是否删除这个文件
if ([fileMgr removeItemAtPath:filePath2 error:&error] != YES)
NSLog(@"Unable to delete file: %@", [error localizedDescription]);
//显示文件目录的内容
NSLog(@"Documentsdirectory: %@",
[fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);
一旦文件被删除了,正如你所预料的那样,文件目录就会被自动清空:
这些示例能教你的,仅仅只是文件处理上的一些皮毛。想要获得更全面、详细的讲解,你就
需要掌握

NSFileManager 文件的知识。

4、删除目录下所有文件
  
//获取文件路径
- (NSString *)attchmentFolder{
NSString 

*document 

[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 

NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [document stringByAppendingPathComponent:@"Attchments"];

NSFileManager *manager = [NSFileManager defaultManager];

if(![manager contentsOfDirectoryAtPath:path error:nil]){
[manager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];
}

return path;
}

--清除附件
BOOL  result  =  [[NSFileManager  defaultManager]  removeItemAtPath:[[MOPAppDelegate 
instance] attchmentFolder] error:nil];