文件夹管理器
1、文件管理器(NSFileManager)
1> 創建文件夾
創建所需的方法在頭文件的聲明:
/* createDirectoryAtPath:withIntermediateDirectories:attributes:error: creates a directory at the specified path. If you pass 'NO' for createIntermediates, the directory must not exist at the time this call is made. Passing 'YES' for 'createIntermediates' will create any necessary intermediate directories. This method returns YES if all directories specified in 'path' were created and attributes were set. Directories are created with attributes specified by the dictionary passed to 'attributes'. If no dictionary is supplied, directories are created according to the umask of the process. This method returns NO if a failure occurs at any stage of the operation. If an error parameter was provided, a presentable NSError will be returned by reference.
This method replaces createDirectoryAtPath:attributes:
*/
// 參數1:創建的文件夾的路徑
// 參數2:是否創建媒介的布爾值,一般為YES
// 參數3: 屬性,沒有就置為nil
// 參數4: 錯誤信息
- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(nullable NSDictionary<NSString *, id> *)attributes error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
實例代碼:
// 創建對象
NSFileManager *manager = [NSFileManager defaultManager];
// 創建路徑
NSString *path = NSHomeDirectory();
path = [path stringByAppendingPathComponent:@"test/myApp"];
NSLog(@"%@", path);
NSError *error = nil;
// 創建文件夾
BOOL success = [manager createDirectoryAtPath:path
withIntermediateDirectories:YES
attributes:nil
error:&error];
NSLog(@"success = %d,error = %@", success,error);
2> 向文件夾中添加文件
內容寫入方法在頭文件的聲明:
// 參數1:要寫入內容的文件的文件路徑 // 參數2:一個BOOL值,一般為YES // 參數3: 編碼方式,一般為UTF8 // 參數4:錯誤信息 - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;
實例代碼:
//向文件夾中添加字符串
path = [path stringByAppendingPathComponent:@"zifucuan.txt"];
//初始化一個字符串
NSString *string = @"hello";
BOOL success1 = [string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (success1) {
NSLog(@"成功:%@",path);
}else{
NSLog(@"失敗");
}
3> 刪除文件夾中文件
刪除文件方法在頭文件的聲明:
// 參數1:路徑 // 參數2:錯誤信息 - (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
實例代碼:
// 刪除path目錄下的所有文件
[manager removeItemAtPath:path error:nil];
4> 文件移動
文件移動方法在頭文件的聲明:
// 參數1:要移動的文件路徑 // 參數2:要移動到的文件路徑(目的地) // 參數3:錯誤信息 - (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
實例代碼:
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// 創建一個文件夾
NSString *copyPath = [documentPath stringByAppendingPathComponent:@"備份/test.txt"];
// stringByDeletingLastPathComponent 刪除最后一個路徑
[manager createDirectoryAtPath:[copyPath stringByDeletingLastPathComponent]
withIntermediateDirectories:YES
attributes:nil
error:nil];
// 定義一個字符串
NSString *testStr = @"Hello World";
NSData *data = [testStr dataUsingEncoding:NSUTF8StringEncoding];
// 將內容寫入文件
[manager createFileAtPath:copyPath
contents:data
attributes:nil];
// 創建一個toPath
NSString *toPath = [documentPath stringByAppendingPathComponent:@"hello/copyTest.txt"];
// 創建一個移動到的文件夾及文件
[manager createDirectoryAtPath:[toPath stringByDeletingLastPathComponent]
withIntermediateDirectories:YES
attributes:nil
error:nil];
BOOL result = [manager moveItemAtPath:copyPath
toPath:toPath
error:nil];
NSLog(@"result = %d", result);
5>文件copy(拷貝)
文件copy(拷貝)方法在頭文件的聲明:
// 參數1:要拷貝的文件路徑
// 參數2:要拷貝到的文件路徑(目的地)
// 參數3:錯誤信息
- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
實例代碼:
// 路徑使用上面的路徑
[manager copyItemAtPath:copyPath
toPath:toPath
error:nil];
2、文件夾處理器(NSFileHandle)
1>使用NSFileHandle向文件夾追加內容
通過fileHandle更新
// 參數為文件路徑 + (nullable instancetype)fileHandleForUpdatingAtPath:(NSString *)path;
搜索到文本內容末尾方法
// 搜索到文件內容的末尾 - (unsigned long long)seekToEndOfFile;
實例代碼:(使用上面的路徑)
// 創建handle對象
NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:path];
// 搜索到文本內容末尾
[fileHandle seekToEndOfFile];
NSString *appendStr = @"我是后來的";
NSData *appendData = [appendStr dataUsingEncoding:NSUTF8StringEncoding];
// 將數據寫入到對接起
[fileHandle writeData:appendData];
// 關閉對接起
[fileHandle closeFile];
2>定位數據
通過fileHandle讀取
// 參數為文件路徑
+ (nullable instancetype)fileHandleForReadingAtPath:(NSString *)path;
獲取文件中可獲得的數據(所有數據)
@property (readonly, copy) NSData *availableData;
設置文件的偏移量
// 參數為一個和文件長度有關的數值 - (void)seekToFileOffset:(unsigned long long)offset;
從文件的偏移量位置讀取到最后
- (NSData *)readDataToEndOfFile;
實例代碼:
// 將“123456”寫入file2.txt文件夾中
NSString * content = @"123456";
NSString * filePath2 = [documentPath stringByAppendingPathComponent:@"file2.txt"];
[fileManager createFileAtPath:filePath2 contents:[content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
// 通過fileHandle讀取
fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath2];
// 獲取數據長度
NSUInteger length = [[fileHandle availableData] length];
// 設置文件的偏移量為文件的一半
[fileHandle seekToFileOffset:length/2.0];
// 從文件的偏移量位置讀取到最后
NSData * data = [fileHandle readDataToEndOfFile];
[fileHandle closeFile];
// 打印讀取的字符串
NSString * string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",string);
總結
- 上一篇: 水榕怎么养(水榕在鱼缸里面给对水质有用)
- 下一篇: urb传输的代码分析【转】