1.获取文件路径
获取文件的路径常用的方法:
1.通过NSFileManager:NSArray *supportDircts = [fileManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask],通过这方法获取的是包含路径url的数组(推荐使用url形式)。
2. 通过NSSearchPathForDirectoriesInDomains:NSArray* supStrDirs = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);通过这种方法获取的是包含路径的string形式的数组
3.获取资源文件:NSURL *imgUrl = [[NSBundle mainBundle] URLForResource:@"imgName" withExtension:@"png"]
4.通过bookMark保存和获取路径的url,:
为啥使用bookMark: bookmark can usually be used to re-create a URL to a file even in cases where the file was moved or renamed.
1.核心代码:保存:
NSData* data = [url bookmarkDataWithOptions:NSURLBookmarkCreationSuitableForBookmarkFile includingResourceValuesForKeys:nil relativeToURL:nil error:&error];
[NSURL writeBookmarkData:data toURL:nil options:NSURLBookmarkCreationSuitableForBookmarkFile error:nil];
2.获取URL
NSURL *url = [NSURL URLByResolvingBookmarkData:data options:NSURLBookmarkResolutionWithoutUI relativeToURL:nil bookmarkDataIsStale:&isStale error:&error];
2.获取某文件下的文件信息
1.遍历获取通过url
//获取带有如下数组中的url属性,NSDirectoryEnumerationSkipsHiddenFiles:跳过隐藏文件
NSArray *urlResouceKeys = @[NSURLIsDirectoryKey,NSURLIsPackageKey];
NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtURL:url includingPropertiesForKeys:urlResouceKeys options:(NSDirectoryEnumerationSkipsHiddenFiles) errorHandler:^BOOL(NSURL * _Nonnull url, NSError * _Nonnull error) {
// Return YES if the enumeration should continue after the error.如需要当发生错误的时候停止遍历直接返回NO
return true;
}];
//遍历
for (NSURL *surl in dirEnum) {
NSNumber *isDir;//判断是否是文件夹
//通过url获取文件的属性
[surl getResourceValue:&isDir forKey:NSURLIsDirectoryKey error:nil];
if ([isDir boolValue]) {
NSString *localName;
[surl getResourceValue:&localName forKey:NSURLLocalizedNameKey error:nil];
NSNumber *isPackage;
[surl getResourceValue:&isPackage forKey:NSURLIsPackageKey error:nil];
if ([isPackage boolValue]) {
NSLog(@"package at==%@",localName);
}
else{
NSLog(@"dir at==%@",localName);
}
}
}
2.遍历获取通过path(如下获悉最近24小时内修改过的文件)
NSString*pstr = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, true) firstObject];
NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow:(-60*60*24)];
NSDirectoryEnumerator *enumeRator = [[NSFileManager defaultManager] enumeratorAtPath:pstr];
for (NSString*path in enumeRator) {
if ([[path pathExtension] isEqualToString:@"trfd"]) {
// Don't enumerate this directory.
[enumeRator skipDescendants];
}else{
NSDictionary *fileAttr = [enumeRator fileAttributes];//这个属性只有通过path获取才有,通过url形式的返回的是nil
NSDate *lastModifyDate = [fileAttr objectForKey:NSFileModificationDate];
if ([yesterday earlierDate:lastModifyDate] == yesterday) {
NSLog(@"modifyed in 24h at:%@",path);
}
}
}
3.直接获取(一次性获取)
NSURL *url = [NSURL fileURLWithPath:NSHomeDirectory()];
NSArray *urlResouceKeys = @[NSURLCreationDateKey,NSURLLocalizedNameKey,NSURLLocalizedTypeDescriptionKey];
NSArray *urls = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:url includingPropertiesForKeys:urlResouceKeys options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];