iOS--在Sandbox(沙盒)中创建文件夹和文件

来源:互联网 发布:自定义端口号 编辑:程序博客网 时间:2024/05/11 12:59

本文Demo源代码:https://github.com/gaussli/FileManagerDemo

之前了解过了沙盒内部的基本组成,今天学习下在沙盒中创建文件夹以及文件

之前在学习沙盒的时候,用过一种创建文件的方法(writeToFile:atomically:)。这次说说另外的一种方法

1. 创建文件夹(test文件夹)

[objc] view plain copy
 print?
  1. // 在Documents文件夹中创建文件夹  
  2. NSFileManager *fileManager = [NSFileManager defaultManager];  
  3. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];  
  4. [fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];  

2. 在test文件夹中创建三个文件,并且写入数据

[objc] view plain copy
 print?
  1. // 在test文件夹中创建三个测试文件(test1.txt/test2.txt/test3.txt),并写入数据  
  2. NSString *test1FilePath = [testDirectory stringByAppendingPathComponent:@"test1.txt"];  
  3. NSString *test2FilePath = [testDirectory stringByAppendingPathComponent:@"test2.txt"];  
  4. NSString *test3FilePath = [testDirectory stringByAppendingPathComponent:@"test3.txt"];  
  5. // 三个文件的内容  
  6. NSString *test1Content = @"helloWorld";  
  7. NSString *test2Content = @"helloTest2";  
  8. NSString *test3Content = @"helloTest3";  
  9. // 写入对应文件  
  10. [fileManager createFileAtPath:test1FilePath contents:[test1Content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];  
  11. [fileManager createFileAtPath:test2FilePath contents:[test2Content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];  
  12. [fileManager createFileAtPath:test3FilePath contents:[test3Content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];  

3. 获得文件夹中文件目录(递归获取)

[objc] view plain copy
 print?
  1. // 获得目录中的所有文件  
  2. // 在test目录下再创建一个子目录subtest文件夹,里面再创建三个子文件(subtest1.txt/subtest2.txt/subtest3.txt)  
  3. // 创建subtest目录  
  4. NSString *subtestDirectoryPath = [testDirectory stringByAppendingPathComponent:@"subtest"];  
  5. [fileManager createDirectoryAtPath:subtestDirectoryPath withIntermediateDirectories:YES attributes:nil error:nil];  
  6. // 创建三个子文件  
  7. [fileManager createFileAtPath:[subtestDirectoryPath stringByAppendingPathComponent:@"subtest1.txt"] contents:nil attributes:nil];  
  8. [fileManager createFileAtPath:[subtestDirectoryPath stringByAppendingPathComponent:@"subtest2.txt"] contents:nil attributes:nil];  
  9. [fileManager createFileAtPath:[subtestDirectoryPath stringByAppendingPathComponent:@"subtest3.txt"] contents:nil attributes:nil];  
  10.   
  11. NSArray *testDirectoryFiles = [fileManager subpathsOfDirectoryAtPath:testDirectory error:nil];  
  12. NSLog(@"subpathsOfDirectoryAtPath的结果:%@", testDirectoryFiles);  
  13. NSArray *testDirectoryFiles1 = [fileManager subpathsAtPath:testDirectory];  
  14. NSLog(@"subpathsAtPath的结果:%@", testDirectoryFiles1);  

打印结果:



结果:

文件目录:



最后,看到一大神分析的createFileAtPath和writeToFile两个函数的差异,这里截取一下。

原网址:http://www.tuicool.com/articles/7fE3Q3E

里面说到

创建文件方法createFileAtPath: contents: attributes: 与writeToFile:atomically:YES在效率上没有什么区别,因此估计两者使用了逻辑是一致的;writeToFile:atomically:NO 比writeToFile:atomically:YES 效率高也是合乎情理(因为atomically:YES是先写到一个临时备份文件然后再改名的,以此来保证操作的原子性,防止写数据过程中出现其他错误),但需要注意的是,在创建空文件时,writeToFile:atomically:NO 效率非常之高,可以考虑在频繁创建空文件的时候使用writeToFile:atomically:NO 。


至此,end~


0 0
原创粉丝点击