文件操作
1. 獲取Documents文件夾路徑
/**
* 獲取Documents文件夾路徑
*
* @return 文件夾路徑
*/
+ (NSString *)getDocumentsFolderPath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
2. 獲取Documents文件路徑
/**
* 獲取Documents文件路徑
*
* @param fileName 文件名稱
*
* @return 文件路徑
*/
+ (NSString *)getDocumentsFilePath:(NSString *)fileName
{
return [[self getDocumentsFolderPath] stringByAppendingPathComponent:fileName];
}
3. 獲取一個目錄所占的空間大小
/**
* 獲取一個目錄所占的空間大小
*
* @param filePath 文件目錄路徑
*
* @return 目錄空間大小
*/
+ (long long)fileSizeAtPath:(NSString*) filePath{
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
else{
return 0;
}
}
4.文件是否存在
/*文件是否存在*/
+ (BOOL)isFileExisted:(NSString *)fileName{
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:[self getFilePath:fileName]]){
return NO;
}
return YES;
}
5.創(chuàng)建指定名字的文件
/*創(chuàng)建指定名字的文件*/
+ (BOOL)createFileAtPath:(NSString *)fileName{
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[array objectAtIndex:0] stringByAppendingPathComponent:fileName];
NSLog(@"-----%@:", path);
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:path]){
[fileManager createFileAtPath:path contents:nil attributes:nil];
return YES;
}
return NO;
}
6.創(chuàng)建指定名字的文件夾
/*創(chuàng)建指定名字的文件夾*/
+ (BOOL)createDirectoryAtPath:(NSString *)fileName{
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[array objectAtIndex:0] stringByAppendingPathComponent:fileName];
NSLog(@"-----%@:", path);
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:path]){
NSError *error = nil;
[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
return YES;
}
return NO;
}
7.刪除文件
+ (BOOL)deleteFileAtPath:(NSString *)fileName{
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[array objectAtIndex:0] stringByAppendingPathComponent:fileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:path]){
return NO;
}
[fileManager removeItemAtPath:path error:nil];
return YES;
}
NSUserDefaults
保存和取出
/**
保存
@param value <#value description#>
@param key <#key description#>
*/
+(void)saveValue:(id) value forKey:(NSString *)key{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:value forKey:key];
[userDefaults synchronize];
}
/**
取出
@param key <#key description#>
@return <#return value description#>
*/
+(id)valueForKey:(NSString *)key{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
return [userDefaults objectForKey:key];
}
/**
根據(jù)key取出NSString的value
@param key <#key description#>
@return <#return value description#>
*/
+(id)StringForKey:(NSString *)key{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
return [userDefaults stringForKey:key];
}
/**
根據(jù)key取出Bool型的值
@param key <#key description#>
@return <#return value description#>
*/
+(BOOL)boolValueForKey:(NSString *)key{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
return [userDefaults boolForKey:key];
}
/**
保存Bool型的值
@param value value
@param key key
*/
+(void)saveBoolValue:(BOOL)value forKey:(NSString *)key{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:value forKey:key];
[userDefaults synchronize];
}
/**
獲取userDefaults存儲的所有信息
*/
+(void)printAllUserDefault{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary *dic = [userDefaults dictionaryRepresentation];
NSLog(@"%@",dic);
}