nsfilemanager
2012-10-30 15:47 -king 阅读(205) 评论(0) 收藏 举报1.获取当前路径的两个方法
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserdomainMask,YES)
NSString *destPath = NSHomeDirectory();
得到的路径略有不同 根据不同情况使用
创建一个新文件夹的方法
NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"TestFolder"];
[fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myfile.xxx"];
[fileManager createFileAtPath:filePath contents:data attributes:nil];//此处的data为nsdata类型的数据
2.获取路径下文件和文件夹的方法
NSArray *fileList =[fileManager contentsOfDirectoryAtPath:MyDir error:&error];
会获得路径下所有的文件夹和文件 要区分可使用以下方法
3. BOOL isDir = NO;
//在上面那段程序中获得的fileList中列出文件夹名
for (NSString *file in fileList) {
NSString *path = [documentDir stringByAppendingPathComponent:file];
[fileManager fileExistsAtPath:path isDirectory:(&isDir)];//这个方法判断是文件夹还是文件 返回bool类型的isDir
if (isDir) {
[dirArray addObject:file];
//this is a folder
}else{
//this is a file
}
}
浙公网安备 33010602011771号