目录
一、writeToFile的基本使用
二、writeToFile的其它方法
一、writeToFile的基本使用
writeToFile可以用来把用户数据存储在Documents文件夹下,也可以用来把缓存数据存储在Library/Caches文件夹下,也可以用来把临时数据存储在tmp文件夹下。
- 创建文件夹(不存在才会创建,已存在不会重复创建)
假设要在Documents文件夹下创建一个TestFolder文件夹。
NSString *testFolderPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"TestFolder"];
[[NSFileManager defaultManager] createDirectoryAtPath:testFolderPath withIntermediateDirectories:YES attributes:nil error:nil];
- 创建文件(不存在才会创建,已存在不会重复创建)
假设要在Documents/TestFolder文件夹下创建七个文件。
NSString *testFile_txt = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"TestFolder/testFile.txt"];
[[NSFileManager defaultManager] createFileAtPath:testFile_txt contents:nil attributes:nil];
NSString *testFile1_plist = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"TestFolder/testFile1.plist"];
[[NSFileManager defaultManager] createFileAtPath:testFile1_plist contents:nil attributes:nil];
NSString *testFile2_plist = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"TestFolder/testFile2.plist"];
[[NSFileManager defaultManager] createFileAtPath:testFile2_plist contents:nil attributes:nil];
NSString *testFile = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"TestFolder/testFile"];
[[NSFileManager defaultManager] createFileAtPath:testFile contents:nil attributes:nil];
NSString *testFile_png = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"TestFolder/testFile.png"];
[[NSFileManager defaultManager] createFileAtPath:testFile_png contents:nil attributes:nil];
NSString *testFile_mp3 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"TestFolder/testFile.mp3"];
[[NSFileManager defaultManager] createFileAtPath:testFile_mp3 contents:nil attributes:nil];
NSString *testFile_mp4 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"TestFolder/testFile.mp4"];
[[NSFileManager defaultManager] createFileAtPath:testFile_mp4 contents:nil attributes:nil];
- 存储和读取基本数据类型的数据 + 存储和读取字符串
writeToFile是个对象方法,所以它不能直接存储基本数据类型的数据,但是我们可以把基本数据类型的数据转换成字符串来存储,字符串要存储在后缀名为txt的文件里。
NSString *string = @"3.1415926";
[string writeToFile:testFile_txt atomically:YES];
NSString *readString = [NSString stringWithContentsOfFile:testFile_txt];
NSLog(@"%@", readString);
- 存储和读取数组 + 字典
数组和字典要存储在后缀名为plist的文件里。
NSArray *array = @[@"YES", @"11", @"3.1415926", @"哈哈哈"];
[array writeToFile:testFile1_plist atomically:YES];
NSArray *readArray = [NSArray arrayWithContentsOfFile:testFile1_plist];
NSLog(@"%@", readArray);
NSDictionary *dictionary = @{
@"bool": @"YES",
@"int": @"11",
@"double": @"3.1415926",
@"string": @"哈哈哈",
};
[dictionary writeToFile:testFile2_plist atomically:YES];
NSDictionary *readDictionary = [NSDictionary dictionaryWithContentsOfFile:testFile2_plist];
NSLog(@"%@", readDictionary);
- 存储和读取自定义对象类型的数据
writeToFile存储自定义对象类型的数据,也得做归档和反归档,并且给自定义对象实现encodeWithCoder:和initWithCoder:方法。
-----------Person.h-----------
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *sex;
@property (nonatomic, assign) NSInteger age;
@end
-----------Person.m-----------
#import "Person.h"
@implementation Person
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeObject:_sex forKey:@"sex"];
[aCoder encodeObject:@(_age) forKey:@"age"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
_name = [aDecoder decodeObjectForKey:@"name"];
_sex = [aDecoder decodeObjectForKey:@"sex"];
_age = [[aDecoder decodeObjectForKey:@"age"] integerValue];
}
return self;
}
@end
Person *person = [[Person alloc] init];
person.name = @"张三";
person.sex = @"男";
person.age = 11;
// 归档
NSData *writeData = [NSKeyedArchiver archivedDataWithRootObject:person];
// 存储
[writeData writeToFile:testFile atomically:YES];
// 读取
NSData *readData = [NSData dataWithContentsOfFile:testFile];
// 反归档
Person *man = [NSKeyedUnarchiver unarchiveObjectWithData:readData];
NSLog(@"%@ %@ %ld", man.name, man.sex, man.age);
- 存储和读取图片、音频、视频等多媒体文件
图片要存储在后缀名为png等的文件里,音频要存储在后缀名为mp3等的文件里,视频要存储在后缀名为mp4等的文件里。
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"baby.png"], 0.5);
[imageData writeToFile:testFile_png atomically:YES];
// 读取图片:已经能拿到图片的磁盘路径了,直接拿磁盘路径就能读取到图片
NSData *audioData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"AllTheRightMoves" ofType:@"mp3"]];
[audioData writeToFile:testFile_mp3 atomically:YES];
// 读取音频:已经能拿到音频的磁盘路径了,直接拿磁盘路径就能读取到音频
NSData *videoData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"LOL版稻香" ofType:@"mp4"]];
[videoData writeToFile:testFile_mp4 atomically:YES];
// 读取视频:已经能拿到视频的磁盘路径了,直接拿磁盘路径就能读取到视频
二、writeToFile的其它方法
- 查询文件夹或文件是否存在
BOOL exit = [[NSFileManager defaultManager] fileExistsAtPath:testFolderPath];
if (exit) {
NSLog(@"文件夹或文件存在");
} else {
NSLog(@"文件夹或文件不存在");
}
- 获取单个文件的大小
NSLog(@"图片大小:%.2fKB", [[[NSFileManager defaultManager] attributesOfItemAtPath:testFile_png error:nil] fileSize] / 1024.0); // 152.53KB
NSLog(@"音频大小:%.2fM", [[[NSFileManager defaultManager] attributesOfItemAtPath:testFile_mp3 error:nil] fileSize] / 1024 / 1024.0); // 3.63M
NSLog(@"视频大小:%.2fM", [[[NSFileManager defaultManager] attributesOfItemAtPath:testFile_mp4 error:nil] fileSize] / 1024 / 1024.0); // 4.03M
- 获取整个文件夹的大小
NSArray *testFileArray = [[NSFileManager defaultManager] subpathsAtPath:testFolderPath];
unsigned long long folderSize = 0;
for (NSString *testFile in testFileArray) {
NSString *testFilePath = [testFolderPath stringByAppendingPathComponent:testFile];
unsigned long long testFileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:testFilePath error:nil] fileSize];
folderSize += testFileSize;
}
NSLog(@"testFolder文件夹大小:%.2fM", folderSize / 1024 / 1024.0); // 7.81M
- 删除文件夹或文件
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:testFolderPath error:nil];
if (success) {
NSLog(@"删除文件夹或文件成功");
} else {
NSLog(@"删除文件夹或文件失败");
}
- 移动文件夹或文件
[[NSFileManager defaultManager] moveItemAtPath:源路径 toPath:目标路径 error:nil];