文件管理器(NSFileManager)此類主要是對文件進行創(chuàng)建/刪除/改等操作,以及文件的獲取
文件連接器(NSFileHandle)此類主要是對文件內(nèi)容進行讀取和寫入操作.是把NSData,通過連接器一個字節(jié)一個字節(jié)的寫入/讀取文件(NSData<->NSFileHandle<->文件)
使用步驟:
- 文件對接并獲取一個NSFileHandle對象
- 讀寫操作
- 關(guān)閉對接
注意:NSFileHandle類并沒有提供創(chuàng)建文件的功能.必須使用NSFileManager方法來創(chuàng)建文件.因此,在使用下一頁表中的方法時,都是保證文件已經(jīng)存在,否則返回nil.
文件管理器的簡單例子
- (void)fileManger{
//初始化一個NSFileManager對象(使用單例)
NSFileManager *manager = [NSFileManager defaultManager];
//1:獲取根路徑
NSString *root = NSHomeDirectory();
NSLog(@"%@",root);
//2:創(chuàng)建文件
root = [root stringByAppendingString:@"/text/myApp"];
//3:創(chuàng)建目錄 withIntermediateDirectories:是否在當(dāng)前路徑下創(chuàng)建/text/myApp
[manager createDirectoryAtPath:root withIntermediateDirectories:YES attributes:nil error:nil];
NSString *rootPath = [root stringByAppendingString:@"/text3/txt"];
[manager createDirectoryAtPath:rootPath withIntermediateDirectories:YES attributes:nil error:nil];
#pragma mark---文件管理 添加----
//必須要在文件路徑下添加文檔,不然不能對內(nèi)容進行復(fù)制,讀寫
NSString * root1 = [root stringByAppendingString:@"/字符串.txt"];
NSString *str = @"大家fghfghfhggfh好";
BOOL result = [str writeToFile:root1 atomically:YES encoding:4 error:nil];
//[manager createFileAtPath:root1 contents:[str dataUsingEncoding:NSUTF8StringEncoding] attributes:nil]
if (result) {
NSLog(@"我成功了");
}else{
NSLog(@"我失敗了");
}
// NSLog(@"root的路徑為:%@",root);
rootPath = [rootPath stringByAppendingString:@"字符串2.txt"];
#pragma mark---文件復(fù)制---
result = [manager copyItemAtPath:root1 toPath:rootPath error:nil];
if (result) {
NSLog(@"成功");
}else{
NSLog(@"失敗");
}
NSLog(@"復(fù)制成功后路徑:%@",rootPath);
#pragma mark ---文件移除---
// 移除的是創(chuàng)建的文件, 而不是所在的文件夾
[manager removeItemAtPath:root error:nil];
NSLog(@"移除后路徑:%@",root);
}
// 判斷文件是否存在
if ([manager fileExistsAtPath:rootPath])
{
NSLog(@"6: 證明文件存在");
}
// : 比較兩文件是否相等
if([manager contentsEqualAtPath:root1 andPath:rootPath])
{
NSLog(@"9: 兩者相等'");
}
文件連接器的簡單例子
- (void)fileHandle
{
// 1: 創(chuàng)建一個文件
NSString *filePath = [NSHomeDirectory() stringByAppendingString:@"/text.txt"];
NSString *fileContent = @"最是人間留不住,朱顏辭鏡花辭樹";
NSFileManager *manager = [NSFileManager defaultManager];
if ([manager createFileAtPath:filePath contents:[fileContent dataUsingEncoding:NSUTF8StringEncoding] attributes:nil]) {
NSLog(@"1: 創(chuàng)建成功%@",filePath);
}
// 2: 創(chuàng)建第二個文件 用于寫數(shù)據(jù) ( 必須要有這個文件,才能進行內(nèi)容的操作寫入 )
NSString *copyPath = [NSHomeDirectory() stringByAppendingString:@"/text2.txt"];
[[NSFileManager defaultManager] createFileAtPath:copyPath contents:nil attributes:nil];
// 3: 打開text.txt 用于讀操作
NSFileHandle *fileHandleRead = [NSFileHandle fileHandleForReadingAtPath:filePath];
if (!fileHandleRead)
{
NSLog(@"3: 打開filepath讀功能失敗");
}
// 4: 打開text2.txt 的寫入操作
NSFileHandle *fileHandleWrite = [NSFileHandle fileHandleForWritingAtPath:copyPath];
if (!fileHandleWrite)
{
NSLog(@"4: 打開text2.txt 的寫入操作 失敗");
}
// 5: 創(chuàng)建 NSData 實例接收fileHandleRead數(shù)據(jù)
NSData *dataTemp = [fileHandleRead readDataToEndOfFile];
// 6: 寫入到 fileHandleWrite 里面
[fileHandleWrite writeData:dataTemp];
// 7: 看能否從CopyPath中取出數(shù)據(jù) 驗證效果
NSLog(@"從CopyPath中讀取數(shù)據(jù) --> %@",[NSString stringWithContentsOfFile:copyPath encoding:NSUTF8StringEncoding error:nil]);
}