课程表

iOS课程

工具箱
速查手册

文件处理

当前位置:免费教程 » 移动开发 » iOS

简介

文件处理不能直观的通过应用程序来解释,我们可以从以下实例来了解IOS的文件处理。

IOS中对文件的操作. 因为应用是在沙箱(sandbox)中的,在文件读写权限上受到限制,只能在几个目录下读写文件。

文件处理中使用的方法

下面列出了用于访问和操作文件的方法的列表。

以下实例你必须替换FilePath1、FilePath和FilePath字符串为完整的文件路径,以获得所需的操作。

检查文件是否存在

  1. NSFileManager *fileManager = [NSFileManager defaultManager];
  2. //Get documents directory
  3. NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains
  4. (NSDocumentDirectory, NSUserDomainMask, YES);
  5. NSString *documentsDirectoryPath = [directoryPaths objectAtIndex:0];
  6. if ([fileManager fileExistsAtPath:@""]==YES) {
  7. NSLog(@"File exists");
  8. }

比较两个文件的内容

  1. if ([fileManager contentsEqualAtPath:@"FilePath1" andPath:@" FilePath2"]) {
  2. NSLog(@"Same content");
  3. }

检查是否可写、可读、可执行文件

  1. if ([fileManager isWritableFileAtPath:@"FilePath"]) {
  2. NSLog(@"isWritable");
  3. }
  4. if ([fileManager isReadableFileAtPath:@"FilePath"]) {
  5. NSLog(@"isReadable");
  6. }
  7. if ( [fileManager isExecutableFileAtPath:@"FilePath"]){
  8. NSLog(@"is Executable");
  9. }

移动文件

  1. if([fileManager moveItemAtPath:@"FilePath1"
  2. toPath:@"FilePath2" error:NULL]){
  3. NSLog(@"Moved successfully");
  4. }

复制文件

  1. if ([fileManager copyItemAtPath:@"FilePath1"
  2. toPath:@"FilePath2" error:NULL]) {
  3. NSLog(@"Copied successfully");
  4. }

删除文件

  1. if ([fileManager removeItemAtPath:@"FilePath" error:NULL]) {
  2. NSLog(@"Removed successfully");
  3. }

读取文件

  1. NSData *data = [fileManager contentsAtPath:@"Path"];

写入文件

  1. [fileManager createFileAtPath:@"" contents:data attributes:nil];
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号