文件管理器和文件對接器

文件管理器 NSFileManagerapp

 1 #pragma mark - 文件管理器  建立文件夾
 2     
 3     // 建立對象
 4     NSFileManager *manager = [NSFileManager defaultManager];
 5     
 6     // 建立路徑
 7     NSString *path = NSHomeDirectory();
 8     path = [path stringByAppendingPathComponent:@"test/myApp"];
 9     NSLog(@"%@", path);
10     
11     // 建立
12     NSError *error = nil;
13     BOOL success = [manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
14     NSLog(@"success = %d, error = %@", success, error);
15     
16     
17 #pragma mark - 文件管理器  添加內容
18     // 添加字符串
19     path = [path stringByAppendingPathComponent:@"hhh.txt"];
20     
21     // 初始化字符串
22     NSString *string = @"哈哈哈哈";
23     
24     [string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
25     
26     
27 #pragma mark - 文件管理器  刪除內容
28     [manager removeItemAtPath:path error:nil];
29     
30     
31 #pragma mark - 文件管理器  移動,copy
32     // 找到Documents路徑
33     NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
34     
35     
36     // 建立一個文件夾
37     NSString *copyPath = [documentPath stringByAppendingPathComponent:@"備份/test.txt"];
38     
39     // stringByDeletingLastPathComponent刪除最後一部份內容
40     [manager createDirectoryAtPath:[copyPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
41     
42     // 定義一個字符串
43     NSString *testStr = @"哈哈哈";
44     NSData *data = [testStr dataUsingEncoding:NSUTF8StringEncoding];
45     
46     [manager createFileAtPath:copyPath contents:data attributes:nil];
47     
48     
49     // 建立一個toPath[用來移動]
50     NSString *toPath = [documentPath stringByAppendingPathComponent:@"hello/aaa.txt"];
51     
52     [manager createDirectoryAtPath:[toPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
53     
54     NSError *error1 = nil;
55     BOOL result = [manager moveItemAtPath:copyPath toPath:toPath error:&error1];
56     NSLog(@"result = %d, error1 = %@", result, error1);
57     
58     
59     BOOL result1 = [manager copyItemAtPath:copyPath toPath:toPath error:nil];
60     NSLog(@"%d", result1);

文件對接器 NSFileHandleatom

 1 #pragma mark - 使用NSFileManager建立文件夾
 2     NSFileManager *fileManager = [NSFileManager defaultManager];
 3     
 4     NSString *documentPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
 5     NSString *strPath = [documentPath stringByAppendingPathComponent:@"file.txt"];
 6     
 7     [fileManager createFileAtPath:strPath contents:[@"hello" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
 8     NSLog(@"%@", strPath);
 9     
10     
11 #pragma mark - 使用NSFileHandle向文件裏追加內容
12     // 建立handle對象
13     NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:strPath];
14     
15     // 搜索到文本內容末尾
16     [fileHandle seekToEndOfFile];
17     
18     
19     // 建立數據
20     NSString *appendStr = @"我是新添加的";
21     NSData *data1 = [appendStr dataUsingEncoding:NSUTF8StringEncoding];
22     
23     // 將數據寫入對接器
24     [fileHandle writeData:data1];
25     
26     // 關閉對接器
27     [fileHandle closeFile];
28     
29     
30 #pragma mark - 定位數據
31     
32     //將「123456」寫入file2.txt文件夾中
33     NSString * content = @"123456";
34     NSString * filePath2 = [documentPath stringByAppendingPathComponent:@"file2.txt"];
35     [fileManager createFileAtPath:filePath2 contents:[content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
36     
37     //經過fileHandle讀取
38     fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath2];
39     //  獲取數據長度
40     NSUInteger length = [[fileHandle availableData] length];
41     //  偏移量文件的一半
42     [fileHandle seekToFileOffset:length/2.0];
43     //從一半位置讀取到最後
44     NSData * data = [fileHandle readDataToEndOfFile];
45     [fileHandle closeFile];
46     //打印讀取的字符串
47     NSString * string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
48     NSLog(@"%@",string);
相關文章
相關標籤/搜索