码迷,mamicode.com
首页 > 其他好文 > 详细

文件管理器和文件对接器

时间:2016-06-04 11:49:17      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

文件管理器 NSFileManager

 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);

文件对接器 NSFileHandle

 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);

 

文件管理器和文件对接器

标签:

原文地址:http://www.cnblogs.com/zhizunbao/p/5558331.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!