iOS 常用操作之文件操作

在项目开发中,不可避免的会遇到将一些东西存到本地,或者在本地创建新的文件或文件夹,特地在此总结了一下。

文件操作

获取Documents文件夹路径

1
2
3
4
5
6
7
8
9
/**
* 获取Documents文件夹路径
*
* @return 文件夹路径
*/
+ (NSString *)getDocumentsFolderPath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}

获取Documents文件路径

1
2
3
4
5
6
7
8
9
10
11
/**
* 获取Documents文件路径
*
* @param fileName 文件名称
*
* @return 文件路径
*/
+ (NSString *)getDocumentsFilePath:(NSString *)fileName
{
return [[self getDocumentsFolderPath] stringByAppendingPathComponent:fileName];
}

获取一个目录所占的空间大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

/**
* 获取一个目录所占的空间大小
*
* @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;
}
}

文件是否存在

1
2
3
4
5
6
7
8
9
/*文件是否存在*/  
+ (BOOL)isFileExisted:(NSString *)fileName{
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:[self getFilePath:fileName]]){
return NO;
}

return YES;
}

创建指定名字的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
/*创建指定名字的文件*/  
+ (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;
}

创建指定名字的文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*创建指定名字的文件夹*/  
+ (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;
}

删除文件

1
2
3
4
5
6
7
8
9
10
11
12
+ (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

保存和取出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
保存

@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];
}


/**
根据key取出NSString的value

@param key <#key description#>
@return <#return value description#>
*/
+(id)StringForKey:(NSString *)key{

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
return [userDefaults stringForKey:key];
}


/**
根据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);
}
-------------本文结束感谢您的阅读-------------
分享使我快乐!