iOS之文件管理器和文件对接器

Posted 悠然的南山

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS之文件管理器和文件对接器相关的知识,希望对你有一定的参考价值。

文件管理器:

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 
 5 @end
 6 
 7 @implementation ViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     // Do any additional setup after loading the view, typically from a nib.
12     
13 #pragma mark - 文件管理器 创建
14     //创建对象
15     NSFileManager *manager = [NSFileManager defaultManager];
16     
17     //创建路径
18     NSString *path = NSHomeDirectory();
19   
20     path = [path stringByAppendingPathComponent:@"test/myApp"];
21     NSLog(@"path = %@", path);
22     BOOL success = [manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
23     if (success) {
24         NSLog(@"文件创建成功");
25     }else {
26         NSLog(@"文件创建失败");
27     }
28     
29     NSString *path2 = [path stringByAppendingPathComponent:@"test2/myApp"];
30     NSLog(@"======%@", path2);
31     [manager createDirectoryAtPath:path2 withIntermediateDirectories:YES attributes:nil error:nil];
32     
33 #pragma mark - 文件管理器 添加
34     //向文件夹添加字符串
35     path = [path stringByAppendingPathComponent:@"zifuchuan.txt"];
36    
37     //初始化一个字符串
38     NSString *string = @"今天是个号入职";
39    BOOL success1 = [string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
40     if (success1) {
41         NSLog(@"成功");
42          NSLog(@"path=========%@", path);
43     }else {
44         NSLog(@"失败");
45     }
46     
47     path = [path2 stringByAppendingPathComponent:@"new.text"];
48     NSString *newStr = @"随便写点吧";
49     [newStr writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
50     NSLog(@"-------%@", path);
51     
52 #pragma mark - 文件管理器 删除
53     [manager removeItemAtPath:path error:nil];//删除的是该文件夹下的文档,该文件夹不能删除
54 #pragma mark - 文件管理器  移动、copy
55     
56     //Documents路径
57     NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
58     
59     //创建一个文件夹     stringByDeletingLastPathComponent删除最后一部分内容
60     NSString *copyPath = [documentPath stringByAppendingPathComponent:@"备份/text.txt"];
61     [manager createDirectoryAtPath:[copyPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
62     NSString *str = @"往里面写内容的时候才有txt文本";
63     NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];//字符串转换为data类型
64     [manager createFileAtPath:copyPath contents:data attributes:nil];
65     NSLog(@"-=-=-=-=-=%@", copyPath);
66     
67    //创建一个toPath  将copyPath中的内容移动/复制到toPath
68     NSString *toPath = [documentPath stringByAppendingPathComponent:@"hello/wode.txt"];
69     
70     [manager createDirectoryAtPath:[toPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
71     //移动
72     //[manager moveItemAtPath:copyPath toPath:toPath error:nil];
73     NSLog(@"topath = %@", toPath);
74     //复制
75     [manager copyItemAtPath:copyPath toPath:toPath error:nil];
76     
77 }
78 
79 @end

 

文件对接器:

 1 @interface ViewController ()
 2 
 3 @end
 4 
 5 @implementation ViewController
 6 
 7 - (void)viewDidLoad {
 8     [super viewDidLoad];
 9     // Do any additional setup after loading the view, typically from a nib.
10 #pragma mark - 使用NSFileMessage创建文件
11     NSFileManager *fileManager = [NSFileManager defaultManager];
12     NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
13     NSString *strPath = [documentPath stringByAppendingPathComponent:@"file.txt"];
14     
15     [fileManager createFileAtPath:strPath contents:[@"hello" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
16     
17     NSLog(@"%@", strPath);
18     
19 #pragma mark - 使用NSFileHandle向文件里追加内容
20     //创建handle对象
21     NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:strPath];
22     //搜索到文本内容末尾
23     [fileHandle seekToEndOfFile];
24     
25     NSString *appendStr = @"我是handle的内容";
26     [fileHandle writeData:[appendStr dataUsingEncoding:NSUTF8StringEncoding]];
27     
28     [fileHandle closeFile];
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);
49     
50 }
51 
52 - (void)didReceiveMemoryWarning {
53     [super didReceiveMemoryWarning];
54     // Dispose of any resources that can be recreated.
55 }
56 
57 @end

 

以上是关于iOS之文件管理器和文件对接器的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

文件管理器和文件对接器

pbootcms对接微信扫码登录代码核心片段和步骤(前后端)

cocoscreator 之iOS 对接google的广告

cocoscreator 之iOS 对接google的广告

Android 逆向使用 Python 解析 ELF 文件 ( Capstone 反汇编 ELF 文件中的机器码数据 | 创建反汇编解析器实例对象 | 设置汇编解析器显示细节 )(代码片段