簡單數(shù)據(jù)的存儲
1. 獲取沙盒的位置
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
也可以使用
NSString *homePath = NSHomeDirectory();
NSString *documentPath2 = [homePath stringByAppendingPathComponent:@"Documents"];
2. 簡單數(shù)據(jù)的存儲
2.1 NSString
(1)創(chuàng)建txt文件
NSString *str = @"ADC";
NSString *strFilePath = [documentPath stringByAppendingPathComponent:@"str.txt"];
(2)寫入數(shù)據(jù)
[str writeToFile:strFilePath atomically:YES];
(3)讀取數(shù)據(jù)
NSString *readStr = [[NSString alloc] initWithContentsOfFile:strFilePath encoding:NSUTF8StringEncoding error:nil];
2.2 NSArray
//先創(chuàng)建一個存儲array對象的txt文件路徑
NSString *arrPath = [documentPath stringByAppendingPathComponent:@"array.txt"];
//使用字面量創(chuàng)建Array
NSArray *nameArr = @[];
//將數(shù)據(jù)寫入txt文件路徑
[nameArr writeToFile:arrPath atomically:YES];
//從路徑讀取出數(shù)據(jù)
NSArray *arr = [NSArray arrayWithContentsOfFile:arrPath];
2.3 字典
//字典的處理步驟同NSArray
NSString *dictPath = [documentPath stringByAppendingPathComponent:@"dict.txt"];
NSDictionary *dict = @{};
[dict writeToFile:dictPath atomically:YES];
NSDictionary *readDict = [NSDictionary dictionaryWithContentsOfFile:dictPath];
2.4 NSData
NSString *imgPath = [documentPath stringByAppendingPathComponent:@"img.txt"];
UIImage *image = [UIImage imageNamed:@"address"];
//將圖片轉(zhuǎn)換成NSData類型
NSData *imgData = UIImagePNGRepresentation(image);
[imgData writeToFile:imgPath atomically:YES];
//讀取圖片
UIImage *img = [UIImage imageWithContentsOfFile:imgPath];
2.5 從資源包讀取圖片
//獲取絕對路徑
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"address" ofType:@"png"];
//通過路徑加載圖片
UIImage *image1 = [UIImage imageWithContentsOfFile:imagePath];
imageView.image = image1;
[self.view addSubview:imageView];
UIView *view = [[NSBundle mainBundle] loadNibNamed:@"a" owner:self options:nil][0];
數(shù)據(jù),文件操作
- 聲明文件的路徑
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
- 創(chuàng)建txt文件及其路徑
NSString *filePath = [documentPath stringByAppendingPathComponent:@"file.txt"];
- 創(chuàng)建FileManager,并初始化文件
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:filePath contents:strData attributes:nil];
- 讀取數(shù)據(jù)
NSData *readStrData = [fileManager contentsAtPath:filePath];
NSString *str1 = [[NSString alloc] initWithData:readStrData encoding:NSUTF8StringEncoding];
- 移動文件
5.1 創(chuàng)建目標文件的路徑
NSString *moveFilePath = [topath stringByAppendingPathComponent:@"file.txt"];
5.2 移動文件
[fileManager moveItemAtPath:filePath toPath:moveFilePath error:nil];
- 復(fù)制文件
NSString *copyPath = [documentPath stringByAppendingPathComponent:@"備份/copy.txt"];
[fileManager createDirectoryAtPath:[copyPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager copyItemAtPath:moveFilePath toPath:copyPath error:nil];
- 獲取文件信息(文件大小)
NSDictionary *dic = [fileManager attributesOfItemAtPath:filePath error:nil];
NSInteger fileLength = [dic[NSFileSize] integerValue];
- 判斷文件是否存在
[fileManager fileExistsAtPath:filePath]
使用NSFileHandle操作文件
//先通過fileManager創(chuàng)建文件并在里面寫入內(nèi)容
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [documentPath stringByAppendingPathComponent:@"file.txt"];
[fileManager createFileAtPath:filePath contents:[@"abc" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
修改內(nèi)容
NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
//查找到文件最末端
[fileHandle seekToEndOfFile];
[fileHandle writeData:[@"123" dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
讀取文件
NSFileHandle *readFileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
//讀取長度
NSUInteger length = [[readFileHandle availableData] length];
//設(shè)置偏移位置
[readFileHandle seekToFileOffset:length/2];
//從偏移位置開始讀取到文件末尾
NSData *readData = [readFileHandle readDataToEndOfFile];
NSString *readStr = [[NSString alloc] initWithData:readData encoding:NSUTF8StringEncoding];
復(fù)制文件
NSString *copypath = [documentPath stringByAppendingPathComponent:@"copy.txt"];
[fileManager createFileAtPath:copypath contents:nil attributes:nil];
//打開文件準備寫入
NSFileHandle *copyHandle = [NSFileHandle fileHandleForWritingAtPath:copypath];
//writeData會直接覆蓋原來的
[copyHandle writeData:readData];
[copyHandle closeFile];
文件的歸檔與反歸檔
要進行歸檔, 必須先讓類繼承<NSCoding>并實現(xiàn)兩個方法
//反序列化
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
//序列化
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
}
歸檔過程:Person對象-->NSData-->writeFile
NSMutableData *mulData = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mulData];
[archiver encodeObject:person forKey:@"Person"];
[archiver finishEncoding];
寫入文件
NSString *personDataPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/person.txt"];
[mulData writeToFile:personDataPath atomically:YES];
反歸檔過程: 從文件里面讀取-->NSData-->Person對象
NSData *personData = [NSData dataWithContentsOfFile:personDataPath];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:personData];
Person *person1 = [unarchiver decodeObjectForKey:@"Person"];
NSLog(@"perosn1 = %@", person1);
另外,還有一個系統(tǒng)單利類可以進行數(shù)據(jù)存儲
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"123" forKey:@"userName"];