==========================
文件操作
==========================
一、歸檔NSKeyedArchiver
1.第一種方式:存儲一種數據。
// 歸檔
// 第一種寫法
// 對象--文件
NSArray* array = [[NSArray alloc]initWithObjects:@"zhang", @"wang", @"li", nil];
NSString* filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"array.txt"];
BOOL success = [NSKeyedArchiver archiveRootObject:array toFile:filePath];
if (success) {
NSLog(@"保存成功");
}
// 解歸檔
NSArray* arr = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%@",arr);
2.第二種方式:存儲並行數據(存儲多種數據)。
// 第二種寫法:
NSArray* array = @[@"one", @"two", @"three"];
NSDictionary* dic = @{@"key":@"value"};
NSString* str = @"我是中國人,我愛中國";
// NSData 數據流類
// 用來存儲復雜的數據,把復雜的數據轉成數據流格式,可以方便進行存儲和傳輸。
// 例如:圖片、大文件
// 斷點續傳,假如圖片有20M大,
// 發郵件:添加附件
NSMutableData* data = [[NSMutableData alloc]init];
// initForWritingWithMutableData 指定要寫入的數據流文件
NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
// 編碼數據,參數一:要准備編碼的數據;參數二:編碼數據的key,key隨便寫
[archiver encodeObject:array forKey:@"array"];
[archiver encodeObject:dic forKey:@"dic"];
[archiver encodeObject:str forKey:@"str"];
// 編碼完成
[archiver finishEncoding];
// 指定文件路徑
NSString* filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"file2"];
// 寫入文件
[data writeToFile:filePath atomically:YES];
// =======================================
// 下部分
// 先把路徑下的文件讀入數據流中
NSMutableData* fileData = [[NSMutableData alloc]initWithContentsOfFile:filePath];
// 把數據流文件讀入到了 解歸檔中
NSKeyedUnarchiver* unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:fileData];
// 進行解歸檔
NSArray* UnArray = [unArchiver decodeObjectForKey:@"array"];
NSDictionary* UnDic = [unArchiver decodeObjectForKey:@"dic"];
NSString* UnStr = [unArchiver decodeObjectForKey:@"str"];
// 打印
NSLog(@"%@\n%@\n%@\n",UnArray,UnDic,UnStr);
3.第三種歸檔方式:對類對象進行歸檔
1.先在類的頭文件中實現<NSCoding>協議
2.在.m中重新編碼和解碼協議。
// 重新initWithCoder 解碼方法
- (id) initWithCoder: (NSCoder *)aDecoder
{
NSLog(@"我是解碼方法,我負責解碼");
self = [super init];
if (self) {
_name = [aDecoder decodeObjectForKey:@"name"];
_phone = [aDecoder decodeObjectForKey:@"phone"];
_address = [aDecoder decodeObjectForKey:@"address"];
}
return self;
}
//重新編碼方法
- (void) encodeWithCoder: (NSCoder *)aCoder
{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeObject:_phone forKey:@"phone"];
[aCoder encodeObject:_address forKey:@"address"];
}
//【注】forKey:是字符串;編碼方法和解碼方法字符串要一致
二、NSFileManager 文件管理類
1.1、文件路徑
// 根路徑
NSString* homePath = NSHomeDirectory();
NSLog(@"%@",homePath);
oc中有三個目錄是可以操作的。
1.Documents // 文稿目錄
2.tmp // 臨時目錄:程序退出的時候,臨時目錄內容可能會被情況
3.library--->Cache // 緩存目錄 // app目錄下
// 獲取Documents 目錄
// 寫法一
NSString* Documents = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSLog(@"Documents :%@",Documents);
// 寫法二
NSString* Documents1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDirectory, YES) objectAtIndex:0];
NSLog(@"Documents : %@",Documents1);
// 獲取應用程序的主目錄
NSString* userName = NSUserName();
NSString* rootPath = NSHomeDirectoryForUser(userName);
NSLog(@"app root path:%@",rootPath);
// 獲取cache目錄
NSString* cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"cache :%@",cachePath);
// 獲取tmp目錄
NSString* tmpPath = NSTemporaryDirectory();
NSLog(@"tmp:%@",tmpPath);
1.2、創建目錄和文件
// 獲取根目錄
NSString* homePath = NSHomeDirectory();
// 創建了一個文件管理器
NSFileManager* fileMgr = [NSFileManager defaultManager];
// 拼接出想要創建的文件路徑
NSString* filePath = [NSString stringWithFormat:@"%@/myFolder",homePath];
// 創建文件目錄
// 第一個參數傳入想要創建的文件目錄,第二個參數指導是否創建不存在的文件夾,yes代表創建
BOOL isOk = [fileMgr createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
if (isOk) {
NSLog(@"創建文件目錄成功");
}
NSString* string = @"我愛記歌詞";
// 把內容寫入到指定路徑下的指定文件中
BOOL isWriteOk = [string writeToFile:[NSString stringWithFormat:@"%@/1.txt",filePath] atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (isWriteOk) {
NSLog(@"寫入文件成功");
}
// 數組保存
NSArray* array = @[@"我是一", @"我是三", @"我是周7"];
BOOL isWriteOK1 = [array writeToFile:[NSString stringWithFormat:@"%@/2.txt",filePath] atomically:YES];
if (isWriteOK1) {
NSLog(@"數組寫入文件成功");
}
// 字典保存
NSDictionary* dic = @{@"key":@"value"};
BOOL isWriteOK2 = [dic writeToFile:[NSString stringWithFormat:@"%@/3.txt",filePath] atomically:YES];
if (isWriteOK2) {
NSLog(@"字典寫入文件成功");
}
2.對文件進行重命名
// 獲取根目錄
NSString* homePath = NSHomeDirectory();
// 創建了一個文件管理器
NSFileManager* fileMgr = [NSFileManager defaultManager];
// 拼接出想要創建的文件路徑
NSString* filePath = [NSString stringWithFormat:@"%@/myFolder/1.txt",homePath];
[fileMgr moveItemAtPath:filePath toPath:[NSString stringWithFormat:@"%@/ai.txt",homePath] error:nil];
3.刪除一個文件
// 聲明了一個錯誤信息的對象
NSError* error;
// 獲取根目錄
NSString* homePath = NSHomeDirectory();
// 創建了一個文件管理器
NSFileManager* fileMgr = [NSFileManager defaultManager];
// 拼接出想要創建的文件路徑
NSString* filePath = [NSString stringWithFormat:@"%@/myFolder/3.txt",homePath];
// 刪除文件
// 如果方法執行返回是NO,error會保存錯誤信息,如果方法執行返回是YES,error = nil
BOOL isok = [fileMgr removeItemAtPath:filePath error:&error];
if (isok) {
NSLog(@"刪除文件成功");
}
else
{
NSLog(@"刪除文件失敗");
// 打印錯誤信息
NSLog(@"%@",error.localizedDescription);
}
Δ【擴展】NSError類,是一個錯誤信息類
// 刪除文件
// 如果方法執行返回是NO,error會保存錯誤信息,如果方法執行返回是YES,error = nil
BOOL isok = [fileMgr removeItemAtPath:filePath error:&error];
∆4.刪除目錄下的所有文件
// 獲取根目錄
NSString* homePath = NSHomeDirectory();
// 創建了一個文件管理器
NSFileManager* fileMgr = [NSFileManager defaultManager];
// 拼接出想要創建的文件路徑
NSString* filePath = [NSString stringWithFormat:@"%@/myFolder/3.txt",homePath];
// 如果刪除的目錄中不帶具體的文件,則刪除的是整個目錄
[fileMgr removeItemAtPath:[NSString stringWithFormat:@"%@/myFolder/",homePath] error:nil];
5.獲取目錄下的所有文件
// 獲取根目錄
NSString* homePath = NSHomeDirectory();
// 創建了一個文件管理器
NSFileManager* fileMgr = [NSFileManager defaultManager];
// 拼接出想要創建的文件路徑
NSString* filePath = [NSString stringWithFormat:@"%@/myFolder/",homePath];
// 獲取當前目錄下的所有文件,包括隱藏文件
NSArray* allFile = [fileMgr contentsOfDirectoryAtPath:filePath error:nil];
NSLog(@"%@",allFile);
// 獲取當前目錄以及子目錄的所有文件
NSArray* subAllFile = [fileMgr subpathsOfDirectoryAtPath:filePath error:nil];
6.文件的屬性
// 獲取根目錄
NSString* homePath = NSHomeDirectory();
// 創建了一個文件管理器
NSFileManager* fileMgr = [NSFileManager defaultManager];
// 拼接出想要創建的文件路徑
NSString* filePath = [NSString stringWithFormat:@"%@/myFolder/3.txt",homePath];
NSDictionary* fileAttribute = [fileMgr fileAttributesAtPath:filePath traverseLink:YES];
// 獲取文件的屬性
NSData* data = [fileAttribute objectForKey:NSFileModificationDate];
NSLog(@"文件的創建日期:%@",data);
// 獲取文件屬性2(*)
NSDictionary* dic1 = [fileMgr attributesOfItemAtPath:filePath error:nil];
NSLog(@"屬性打印 %@",dic1);
// 文件占多少字節
NSNumber * number = [fileAttribute objectForKey:NSFileSize];
NSLog(@"文件的大小:%@",number);
// 判斷文件是否存在
NSFileManager *manager = [NSFileManager defaultManager];
BOOL isExist = [manager fileExistsAtPath:filePath];
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。