iOS開發(fā)中我們會需要需要操作沙盒中的文件补憾,創(chuàng)建文件夾,刪除文件卷员,獲取文件大小盈匾,創(chuàng)建時(shí)間,修改時(shí)間毕骡,文件夾大小削饵,刪除具體文件,詳細(xì)操作如下
創(chuàng)建文件夾
NSString *audioDir=[NSString stringWithFormat:@"%@/FlyElephant/", NSTemporaryDirectory()];
BOOL isDir=NO;
NSFileManager *fileManager=[NSFileManager defaultManager];
BOOL existed=[fileManager fileExistsAtPath:audioDir isDirectory:&isDir];
if (!(isDir == YES && existed == YES)){
[fileManager createDirectoryAtPath:audioDir withIntermediateDirectories:YES attributes:nil error:nil];
}
刪除文件夾
NSString *audioDir=[NSString stringWithFormat:@"%@/FlyElephant/", NSTemporaryDirectory()];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:audioDir error:nil];
拷貝文件
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *fromPath = [[self composeDir] stringByAppendingString:@"keso.wav"];
NSString *toPath = [[self composeDir] stringByAppendingString:@"FlyElephant.wav"];
NSError *error;
[fileManager copyItemAtURL:[NSURL fileURLWithPath:fromPath] toURL:[NSURL fileURLWithPath:toPath] error:&error];
if (error) {
NSLog(@"Error:%@",error.description);
}
獲取所有子文件
NSFileManager *fileManager=[NSFileManager defaultManager];
NSError *error=nil;
NSArray *childList=[fileManager contentsOfDirectoryAtPath:NSTemporaryDirectory() error:&error];
if (!error) {
NSLog(@"FlyElephant--%@--子文件目錄:%@",NSTemporaryDirectory(),childList);
}
獲取文件大小,創(chuàng)建時(shí)間,修改時(shí)間
for (NSInteger i=0; i<[childList count]; i++) {
NSString *fileName=[childList objectAtIndex:i];
if ([fileName hasSuffix:@"jpg"]) {
NSString *path=[NSTemporaryDirectory() stringByAppendingString:fileName];
NSError *pathError=nil;
NSDictionary *dict=[[NSFileManager defaultManager] attributesOfItemAtPath:path error:&pathError];
if (pathError==nil) {
NSLog(@"FlyElephant--%@",dict);
}
}
}
上面這個(gè)方法可以獲取tmp文件目錄中的jpg文件未巫,具體的創(chuàng)建時(shí)間窿撬,我們可以根據(jù)dict中的key值獲取,常用獲取方法如下:
- (unsigned long long)fileSize;
- (nullable NSDate *)fileModificationDate;
- (nullable NSString *)fileType;
- (NSUInteger)filePosixPermissions;
- (nullable NSString *)fileOwnerAccountName;
- (nullable NSString *)fileGroupOwnerAccountName;
- (NSInteger)fileSystemNumber;
- (NSUInteger)fileSystemFileNumber;
- (BOOL)fileExtensionHidden;
- (OSType)fileHFSCreatorCode;
- (OSType)fileHFSTypeCode;
- (BOOL)fileIsImmutable;
- (BOOL)fileIsAppendOnly;
- (nullable NSDate *)fileCreationDate;
- (nullable NSNumber *)fileOwnerAccountID;
- (nullable NSNumber *)fileGroupOwnerAccountID;
基于以上的屬性叙凡,我們可以獲取特定目錄下的特定類型文件的大小劈伴,計(jì)算出緩存的大小,清除緩存~