IOS文件系统,文件目录结构学习

转载别人的,找不到原处了,只是为了自己学习收藏。
NSFileManager
判断一个给定路劲是否为文件夹

[self.fileManagerfileExistsAtPath:isDirectory:];



用于执行一般的文件系统操作 (reading and writing is done via NSData, et. al.).
主要功能包括:从一个文件中读取数据;向一个文件中写入数据;删除文件;复制文件;移动文件;比较两个文件的内容;测试文件的存在性;读取/更改文件的属性... ...<wbr><br><span style="line-height:25px">Just alloc/init an instance and start performing operations. Thread safe.</span><br><br><ul style="line-height:25px; margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:40px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial"><li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:30px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:disc; list-style-position:initial"> 常见的<strong>NSFileManager处理<span style="word-wrap:normal; word-break:normal; line-height:21px; background-color:rgb(255,255,0)">文件</span>的方法</strong>如下:</li></ul> <pre style="line-height:25px; white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="color:#66066; word-wrap:normal; word-break:normal; line-height:21px">NSFileManager</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">*</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileManager</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">=</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[[</span><span style="color:#66066; word-wrap:normal; word-break:normal; line-height:21px">NSFileManager</span><span style="word-wrap:normal; word-break:normal; line-height:21px">alloc</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">]</span><span style="word-wrap:normal; word-break:normal; line-height:21px">init</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#8800; word-wrap:normal; word-break:normal; line-height:21px">//最好不要用defaultManager。</span></pre> <pre style="line-height:25px; white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="color:#66066; word-wrap:normal; word-break:normal; line-height:21px">NSData</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">*</span><span style="word-wrap:normal; word-break:normal; line-height:21px">myData</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">=</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileManager contentsAtPath</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#8800; word-wrap:normal; word-break:normal; line-height:21px">// 从一个文件中读取数据</span></pre> <pre style="line-height:25px; white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileManager createFileAtPath</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path contents</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">myData attributes</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">dict</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span><span style="color:#8800; word-wrap:normal; word-break:normal; line-height:21px">//向一个文件中写入数据,属性字典允许你制定要创建</span></pre> <pre style="line-height:25px; white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileManager removeItemAtPath</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path error</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">err</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span></pre> <pre style="line-height:25px; white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileManager moveItemAtPath</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path toPath</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path2 error</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">err</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span></pre> <pre style="line-height:25px; white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileManager copyItemAtPath</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path toPath</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path2 error</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">err</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span></pre> <pre style="line-height:25px; white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileManager contentsEqualAtPath</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path andPath</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path2</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span></pre> <pre style="line-height:25px; white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileManager fileExistsAtPath</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">...</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">...</span></pre> <ul style="line-height:25px; margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:40px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial"><li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:30px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:disc; list-style-position:initial"> 常见的<strong>NSFileManager处理<span style="word-wrap:normal; word-break:normal; line-height:21px; background-color:rgb(255,255,0)">目录</span>的方法</strong>如下:</li></ul> <pre style="line-height:25px; white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileManager currentDirectoryPath</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span></pre> <pre style="line-height:25px; white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileManager changeCurrentDirectoryPa<wbr>th</wbr></span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span></pre> <pre style="line-height:25px; white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileManager copyItemAtPath</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path toPath</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path2 error</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">err</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span></pre> <pre style="line-height:25px; white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileManager createDirectoryAtPath</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path withIntermediateDirector<wbr>ies</wbr></span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">YES attributes</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="color:#0088; word-wrap:normal; word-break:normal; line-height:21px">nil</span><span style="word-wrap:normal; word-break:normal; line-height:21px">error</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">err</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span></pre> <pre style="line-height:25px; white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileManager fileExistsAtPath</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path isDirectory</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">YES</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span></pre> <pre style="line-height:25px; white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileManager enumeratorAtPath</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#8800; word-wrap:normal; word-break:normal; line-height:21px">//获取目录的内容列表。一次可以枚举指定目录中的每个文件。</span><span style="color:#8800; word-wrap:normal; word-break:normal; line-height:21px">... ...</span></pre> <span style="line-height:25px">Has a delegate with lots of “should” methods (to do an operation or proceed after an error).</span><br><span style="line-height:25px">And plenty more. Check out the documentation.</span></wbr>

1、文件的创建

-(IBAction) CreateFile

{

//对于错误信息

NSError *error;

// 创建文件管理器

NSFileManager *fileMgr = [NSFileManager defaultManager];

//指向文件目录

NSString *documentsDirectory= [NSHomeDirectory() stringByAppendingPathCom<wbr><wbr>ponent:@"Documents"];</wbr></wbr>


//创建一个目录

[[NSFileManager defaultManager]<wbr><wbr><wbr><wbr><wbr>createDirectoryAtPath: [NSString stringWithFormat:@"</wbr></wbr></wbr></wbr></wbr>%@/myFolder", NSHomeDirectory()] attributes:nil];

// File we want to create in the documents directory我们想要创建的文件将会出现在文件目录中

// Result is: /Documents/file1.txt结果为:/Documents/file1.txt

NSString *filePath= [documentsDirectory

stringByAppendingPathCom<wbr><wbr>ponent:@"file2.txt"];</wbr></wbr>

//需要写入的字符串

NSString *str= @"iPhoneDeveloper Tips\nhttp://iPhoneDevelopTips,com";

//写入文件

[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

//显示文件目录的内容

NSLog(@"Documentsdirectory:<wbr></wbr>%@",[fileMgr<wbr>contentsOfDirectoryAtPat<wbr><wbr>h:documentsDirectory error:&amp;error]);<br> }<wbr></wbr></wbr></wbr></wbr>


<wbr></wbr>

2、对文件重命名<wbr></wbr>

对一个文件重命名
想要重命名一个文件,我们需要把文件移到一个新的路径下。下面的代码创建了我们所期望的目标文件的路径,然后请求移动文件以及在移动之后显示文件目录。
//通过移动该文件对文件重命名
NSString *filePath2= [documentsDirectory
stringByAppendingPathCom<wbr><wbr>ponent:@"file2.txt"];<br></wbr></wbr>
//判断是否移动
if ([fileMgr moveItemAtPath:filePath toPath:filePath2 error:&error] != YES)
NSLog(@"Unable to move file: %@", [error localizedDescription]);
//显示文件目录的内容
NSLog(@"Documentsdirectory: %@",
[fileMgr contentsOfDirectoryAtPat<wbr><wbr>h:documentsDirectoryerror:&amp;error]);</wbr></wbr>

<wbr><wbr></wbr></wbr>

3、删除一个文件<wbr></wbr>


为了使这个技巧完整,让我们再一起看下如何删除一个文件:
//在filePath2中判断是否删除这个文件
if ([fileMgr removeItemAtPath:filePath2 error:&error] != YES)
NSLog(@"Unable to delete file: %@", [error localizedDescription]);
//显示文件目录的内容
NSLog(@"Documentsdirectory: %@",
[fileMgr contentsOfDirectoryAtPat<wbr><wbr>h:documentsDirectoryerror:&amp;error]);<br> 一旦文件被删除了,正如你所预料的那样,文件目录就会被自动清空:</wbr></wbr>

这些示例能教你的,仅仅只是文件处理上的一些皮毛。想要获得更全面、详细的讲解,你就需要掌握NSFileManager文件的知识。


<wbr><wbr></wbr></wbr>

4、删除目录下所有文件<wbr></wbr>

//获取文件路径
- (NSString *)attchmentFolder{

NSString *document = [NSSearchPathForDirectori<wbr><wbr>esInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];</wbr></wbr>

NSString *path = [document stringByAppendingPathCom<wbr><wbr>ponent:@"Attchments"];</wbr></wbr>


NSFileManager *manager = [NSFileManager defaultManager];


if(![manager contentsOfDirectoryAtPat<wbr><wbr>h:path error:nil]){</wbr></wbr>

[manager createDirectoryAtPath:path withIntermediateDirector<wbr><wbr>ies:NO attributes:nil error:nil];</wbr></wbr>

}
return path;

}<wbr></wbr>

--清除附件
BOOL result = [[NSFileManager defaultManager] removeItemAtPath:[[MOPAppDelegate instance] attchmentFolder] error:nil];


5、判断文件是否存在

NSString<wbr>*filePath = [<span style="color:#b92e9f; word-wrap:normal; word-break:normal; line-height:21px">self</span><wbr><span style="color:#32595d; word-wrap:normal; word-break:normal; line-height:21px">dataFilePath</span>];</wbr></wbr>

if<wbr>([[</wbr>NSFileManager<wbr></wbr>defaultManager]fileExistsAtPath:filePath])<wbr></wbr>

<wbr><wbr>//do some thing</wbr></wbr>

附:

<wbr></wbr>

-(NSString<wbr>*)dataFilePath</wbr>

{

NSArray<wbr>*paths =<wbr></wbr></wbr>NSSearchPathForDirectori<wbr>esInDomains<span style="color:#000000; word-wrap:normal; word-break:normal; line-height:21px">(</span>NSDocumentDirectory<span style="color:#000000; word-wrap:normal; word-break:normal; line-height:21px">,<wbr></wbr></span>NSUserDomainMask<span style="color:#000000; word-wrap:normal; word-break:normal; line-height:21px">,<wbr></wbr></span><span style="color:#b92e9f; word-wrap:normal; word-break:normal; line-height:21px">YES</span><span style="color:#000000; word-wrap:normal; word-break:normal; line-height:21px">);</span></wbr>

NSString<wbr>*documentDirectory = [paths<wbr><span style="color:#3e217e; word-wrap:normal; word-break:normal; line-height:21px">objectAtIndex</span>:<span style="color:#2c32d3; word-wrap:normal; word-break:normal; line-height:21px">0</span>];</wbr></wbr>

return<wbr>[documentDirectory<wbr></wbr></wbr>stringByAppendingPathCom<wbr>ponent<span style="color:#000000; word-wrap:normal; word-break:normal; line-height:21px">:<span style="color:#cf2b23; word-wrap:normal; word-break:normal; line-height:normal"><span style="color:#77482e; word-wrap:normal; word-break:normal; line-height:21px">@</span>"data.plist"</span>];</span></wbr>

}

<wbr></wbr>


常用路径工具函数
NSString * NSUserName(); 返回当前用户的登录名<wbr></wbr>
NSString * NSFullUserName(); 返回当前用户的完整用户名<wbr></wbr>
NSString * NSHomeDirectory(); 返回当前用户主目录的路径<wbr></wbr>
NSString * NSHomeDirectoryForUser(); 返回用户user的主目录<wbr></wbr>
NSString * NSTemporaryDirectory(); 返回可用于创建临时文件的路径目录<wbr></wbr>

常用路径工具方法
-(NSString *) pathWithComponents:components <wbr><wbr>根据components(NSArray对象)中元素构造有效路径<wbr></wbr></wbr></wbr>
-(NSArray *)pathComponents <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>析构路径,获取路径的各个部分<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(NSString *)lastPathComponent <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>提取路径的最后一个组成部分<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(NSString *)pathExtension <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>路径扩展名<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(NSString *)stringByAppendingPathCom<wbr>ponent:path <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>将path添加到现有路径末尾<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(NSString *)stringByAppendingPathExt<wbr>ension:ext <wbr><wbr><wbr><wbr><wbr>将拓展名添加的路径最后一个组成部分<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(NSString *)stringByDeletingPathComp<wbr>onent <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>删除路径的最后一个部分<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(NSString *)stringByDeletingPathExte<wbr>nsion <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>删除路径的最后一个部分 的扩展名<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(NSString *)stringByExpandingTildeIn<wbr>Path <wbr><wbr><wbr><wbr><wbr>将路径中的代字符扩展成用户主目录(~)或指定用户主目录(~user)<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(NSString *)stringByResolvingSymlink<wbr>sInPath <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>尝试解析路径中的符号链接<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(NSString *)stringByStandardizingPat<wbr>h <wbr><wbr><wbr><wbr><wbr><wbr>通过尝试解析~、..、.、和符号链接来标准化路径<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-<wbr></wbr>
使用路径NSPathUtilities.h<wbr></wbr>
tempdir = NSTemporaryDirectory(); 临时文件的目录名<wbr></wbr>
path = [fm currentDirectoryPath];
[path lastPathComponent]; 从路径中提取最后一个文件名<wbr></wbr>
fullpath = [path stringByAppendingPathCom<wbr>ponent:fname];将文件名附加到路劲的末尾<wbr></wbr></wbr>
extenson = [fullpath pathExtension]; 路径名的文件扩展名<wbr></wbr>
homedir = NSHomeDirectory();用户的主目录<wbr></wbr>
component = [homedir pathComponents]; <wbr>路径的每个部分<wbr></wbr></wbr>

NSProcessInfo类:允许你设置或检索正在运行的应用程序的各种类型信息
(NSProcessInfo *)processInfo <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>返回当前进程的信息</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(NSArray*)arguments <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>以NSString对象数字的形式返回当前进程的参数</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(NSDictionary *)environment <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>返回变量/值对词典。描述当前的环境变量</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(int)processIdentity <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>返回进程标识</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(NSString *)processName <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>返回进程名称</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(NSString *)globallyUniqueString <wbr>每次调用该方法都会返回不同的单值字符串,可以用这个字符串生成单值临时文件名 <wbr><wbr></wbr></wbr></wbr>
-(NSString *)hostname <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>返回主机系统的名称<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(unsigned int)operatingSystem <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>返回表示操作系统的数字<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(NSString *)operatingSystemName <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>返回操作系统名称<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(NSString *)operatingSystemVersionSt<wbr>ring <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>返回操作系统当前版本</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-(void)setProcessName:(NSString *)name <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>将当前进程名称设置为name<wbr><br><br> ============================================================================<br><wbr><wbr><strong><span style="color:#ff00; word-wrap:normal; word-break:normal; line-height:21px">NSFileHandle</span></strong>类允许更有效地使用文件。<br> 可以实现如下<strong>功能</strong>:<br> 1、打开一个文件,执行读、写或更新(读写)操作;<br> 2、在文件中查找指定位置;<br> 3、从文件中读取特定数目的字节,或将特定数目的字节写入文件中<br> 另外,NSFileHandle类提供的方法也可以用于各种设备或套接字。一般而言,我们处理文件时都要经历以下三个<strong>步骤</strong>:<br> 1、打开文件,获取一个NSFileHandle对象(以便在后面的I/O操作中引用该文件)。<br> 2、对打开文件执行I/O操作。<br> 3、关闭文件。<br><pre style="white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="color:#66066; word-wrap:normal; word-break:normal; line-height:21px">NSFileHandle</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">*</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileHandle</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">=</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[[</span><span style="color:#66066; word-wrap:normal; word-break:normal; line-height:21px">NSFileHandle</span><span style="word-wrap:normal; word-break:normal; line-height:21px">alloc</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">]</span><span style="word-wrap:normal; word-break:normal; line-height:21px">init</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span></pre> <pre style="white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="word-wrap:normal; word-break:normal; line-height:21px">fileHandle</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">=</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="color:#66066; word-wrap:normal; word-break:normal; line-height:21px">NSFileHandle</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileHandleForReadingAtPa<wbr>th</wbr></span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#8800; word-wrap:normal; word-break:normal; line-height:21px">//打开一个文件准备读取</span></pre> <pre style="white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="word-wrap:normal; word-break:normal; line-height:21px">fileHandle</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">=</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="color:#66066; word-wrap:normal; word-break:normal; line-height:21px">NSFileHandle</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileHandleForWritingAtPa<wbr>th</wbr></span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span></pre> <pre style="white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="word-wrap:normal; word-break:normal; line-height:21px">fileHandle</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">=</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="color:#66066; word-wrap:normal; word-break:normal; line-height:21px">NSFileHandle</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileHandleForUpdatingAtP<wbr>ath</wbr></span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">path</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span></pre> <pre style="white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="word-wrap:normal; word-break:normal; line-height:21px">fileData</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">=</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileHandle availableData</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#8800; word-wrap:normal; word-break:normal; line-height:21px">// 从设备或者通道返回可用的数据</span></pre> <pre style="white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="word-wrap:normal; word-break:normal; line-height:21px">fileData</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">=</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileHandle readDataToEndOfFile</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span></pre> <pre style="white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileHandlewriteData</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">:</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileData</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#8800; word-wrap:normal; word-break:normal; line-height:21px">//将NSData数据写入文件</span></pre> <pre style="white-space:normal; word-wrap:break-word; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">[</span><span style="word-wrap:normal; word-break:normal; line-height:21px">fileHandle closeFile</span><span style="color:#66660; word-wrap:normal; word-break:normal; line-height:21px">];</span><span style="word-wrap:normal; word-break:normal; line-height:21px"></span><span style="color:#8800; word-wrap:normal; word-break:normal; line-height:21px">//关闭文件</span><span style="color:#8800; word-wrap:normal; word-break:normal; line-height:21px">... ...</span></pre> <strong><span style="font-size:18px; word-wrap:normal; word-break:normal; line-height:32px">注:</span></strong>NSFileHandle类没有提供创建文件的功能,所以必须使用NSFileManager来创建文件</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值