首先
(稍后抽時(shí)間再總結(jié)禽作,先記錄)
先了解下常見的NSFileManager文件方法:
獲取App沙盒根路徑:
NSString *homePath=NSHomeDirectory();
獲取Documents目錄路徑:
NSArray *Documentspaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
獲取Library目錄路徑
NSArray *Librarypaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
獲取Library/Caches目錄路徑
NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cacPath objectAtIndex:0];
獲取Tmp目錄路徑
NSString *tmpDirectory = NSTemporaryDirectory();
-(NSData *)contentsAtPath:path //從一個(gè)文件讀取數(shù)據(jù)
-(BOOL)createFileAtPath: path contents:(NSData *)data attributes:attr //向一個(gè)文件寫入數(shù)據(jù)
-(BOOL)removeItemAtPath:path error:err //刪除一個(gè)文件
-(BOOL)moveItemAtPath:from toPath:to error:err //重命名或者移動(dòng)一個(gè)文件(to不能是已存在的)
-(BOOL)copyItemAtPath:from toPath:to error:err //復(fù)制文件(to不能是已存在的)
-(BOOL)contentsEqualAtPath:path andPath:path2 //比較兩個(gè)文件的內(nèi)容
-(BOOL)fileExistAtPath:path //測(cè)試文件是否存在
-(BOOL)isReadableFileAtPath:path //測(cè)試文件是否存在旷偿,并且是否能執(zhí)行讀操作
-(BOOL)isWriteableFileAtPath:path //測(cè)試文件是否存在,并且是否能執(zhí)行寫操作
-(NSDictionary *)attributesOfItemAtPath:path error:err //獲取文件的屬性
-(BOOL)setAttributesOfItemAtPath:attr error:err //更改文件的屬性
使用目錄
-(NSString *)currentDirectoryPath //獲取當(dāng)前目錄
-(BOOL)changeCurrentDirectoryPath:path //更改當(dāng)前目錄
-(BOOL)copyItemAtPath:from toPath:to error:err //復(fù)制目錄結(jié)構(gòu)(to不能是已存在的)
-(BOOL)createDirectoryAtPath:path withIntermediateDirectories:(BOOL)flag attribute:attr //創(chuàng)建一個(gè)新目錄
-(BOOL)fileExistAtPath:path isDirectory:(BOOL*)flag //測(cè)試文件是不是目錄(flag中儲(chǔ)存結(jié)果YES/NO)
-(NSArray *)contentsOfDirectoryAtPath:path error:err //列出目錄內(nèi)容
-(NSDirectoryEnumerator *)enumeratorAtPath:path //枚舉目錄的內(nèi)容
-(BOOL)removeItemAtPath:path error:err //刪除空目錄
-(BOOL)moveItemAtPath:from toPath:to error:err //重命名或移動(dòng)一個(gè)目錄(to不能是已存在的)
常用路徑工具方法
+(NSString *)pathWithComponens:components //根據(jù)components中的元素構(gòu)造有效路徑
-(NSArray *)pathComponents //析構(gòu)路徑幢妄,獲得組成此路徑的各個(gè)部分
-(NSString *)lastPathComponent //提取路徑的最后一個(gè)組成部分
-(NSString *)pathExtension //從路徑的最后一個(gè)組成部分中提取其擴(kuò)展名
-(NSString *)stringByAppendingPathComponent:path //將path添加到現(xiàn)有路徑的末尾
-(NSString *)stringByAppendingPathExtension:ext //將指定的擴(kuò)展名添加到路徑的最后一個(gè)組成部分
-(NSString *)stringByDeletingLastPathComponent //刪除路徑的最后一個(gè)組成部分
-(NSString *)stringByDeletingPathExtension //從文件的最后一部分刪除擴(kuò)展名
-(NSString *)stringByExpandingTileInPath //將路徑中代字符擴(kuò)展成用戶主目錄(~)或指定用戶的主目錄(~user)
-(NSString *)stringByresolvingSymlinksInPath //嘗試解析路徑中的符號(hào)鏈接
-(NSString *)stringByStandardizingPath //通過嘗試解析~蕉鸳、..(父目錄符號(hào))朽褪、.(當(dāng)前目錄符號(hào))和符號(hào)鏈接來標(biāo)準(zhǔn)化路徑
常用的路徑工具函數(shù)
NSString* NSUserName(void) //返回當(dāng)前用戶的登錄名
NSString* NSFullUserName(void) //返回當(dāng)前用戶的完整用戶名
NSString* NSHomeDirectory(void) //返回當(dāng)前用戶主目錄的路徑
NSString* NSHomeDirectoryForUser(NSString* user) //返回用戶user的主目錄
NSString* NSTemporaryDirectory(void) //返回可用于創(chuàng)建臨時(shí)文件的路徑目錄
常用的IOS目錄
Documents(NSDocumentDirectory) //用于寫入應(yīng)用相關(guān)數(shù)據(jù)文件的目錄缔赠,在ios中寫入這里的文件能夠與iTunes共享并訪問友题,存儲(chǔ)在這里的文件會(huì)自動(dòng)備份到云端
Library/Caches(NSCachesDirectory) //用于寫入應(yīng)用支持文件的目錄,保存應(yīng)用程序再次啟動(dòng)需要的信息踢匣。iTunes不會(huì)對(duì)這個(gè)目錄的內(nèi)容進(jìn)行備份
tmp(use NSTemporaryDirectory()) //這個(gè)目錄用于存放臨時(shí)文件戈抄,只程序終止時(shí)需要移除這些文件,當(dāng)應(yīng)用程序不再需要這些臨時(shí)文件時(shí)输莺,應(yīng)該將其從這個(gè)目錄中刪除
Library/Preferences //這個(gè)目錄包含應(yīng)用程序的偏好設(shè)置文件,使用 NSUserDefault類進(jìn)行偏好設(shè)置文件的創(chuàng)建嫂用、讀取和修改
清除應(yīng)用緩存
//
// ViewController.m
// testDemocach
//
// Created by Sunniwell on 2017/3/14.
// Copyright ? 2017年 ZPengs. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(100, 100, 100, 50);
[btn setTitle:@"清除緩存" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor purpleColor];
[btn addTarget:self action:@selector(putBufferBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
// Do any additional setup after loading the view, typically from a nib.
}
//清除緩存按鈕的點(diǎn)擊事件
- (void)putBufferBtnClicked:(UIButton *)btn{
CGFloat size = [self folderSizeAtPath:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject] + [self folderSizeAtPath:NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject] + [self folderSizeAtPath:NSTemporaryDirectory()];
NSString *message = size > 1 ? [NSString stringWithFormat:@"緩存%.0fM, 刪除緩存", size] : [NSString stringWithFormat:@"緩存%.0fK, 刪除緩存", size * 1024.0];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:(UIAlertControllerStyleAlert)];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
[self cleanCaches:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject];
[self cleanCaches:NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject];
[self cleanCaches:NSTemporaryDirectory()];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
[alert addAction:action];
[alert addAction:cancel];
[self showDetailViewController:alert sender:nil];
}
// 計(jì)算目錄大小
- (CGFloat)folderSizeAtPath:(NSString *)path{
// 利用NSFileManager實(shí)現(xiàn)對(duì)文件的管理
NSFileManager *manager = [NSFileManager defaultManager];
CGFloat size = 0;
if ([manager fileExistsAtPath:path]) {
// 獲取該目錄下的文件甘畅,計(jì)算其大小
NSArray *childrenFile = [manager subpathsAtPath:path];
for (NSString *fileName in childrenFile) {
NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
size += [manager attributesOfItemAtPath:absolutePath error:nil].fileSize;
}
// 將大小轉(zhuǎn)化為M
return size / 1024.0 / 1024.0;
}
return 0;
}
// 根據(jù)路徑刪除文件
- (void)cleanCaches:(NSString *)path{
// 利用NSFileManager實(shí)現(xiàn)對(duì)文件的管理
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:path]) {
// 獲取該路徑下面的文件名
NSArray *childrenFiles = [fileManager subpathsAtPath:path];
for (NSString *fileName in childrenFiles) {
// 拼接路徑
NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
// 將文件刪除
[fileManager removeItemAtPath:absolutePath error:nil];
}
}
}
@end