ios开发手册中文版

浏览23819
更新时间2022-04-11

IOS文件处理


简介

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

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

文件处理中使用的方法

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


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

检查文件是否存在

   NSFileManager *fileManager = [NSFileManager defaultManager];   //Get documents directory   NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains   (NSDocumentDirectory, NSUserDomainMask, YES);   NSString *documentsDirectoryPath = [directoryPaths objectAtIndex:0];   if ([fileManager fileExistsAtPath:@""]==YES) {        NSLog(@"File exists");    }

比较两个文件的内容

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

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

  if ([fileManager isWritableFileAtPath:@"FilePath"]) {      NSLog(@"isWritable");   }   if ([fileManager isReadableFileAtPath:@"FilePath"]) {      NSLog(@"isReadable");   }   if ( [fileManager isExecutableFileAtPath:@"FilePath"]){      NSLog(@"is Executable");   }

移动文件

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

复制文件

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

删除文件

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

读取文件

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

写入文件

  [fileManager createFileAtPath:@"" contents:data attributes:nil];

相关视频

更多

免费

微信小程序--企业微网站
初级微信小程序--企业微网站

5.4万次学习

收藏

免费

微信小程序开发基础教程
初级微信小程序开发基础教程

6.8万次学习

收藏

免费

微信小程序基础API篇
高级微信小程序基础API篇

3.5万次学习

收藏

免费

微信小程序--微商城
高级微信小程序--微商城

3.9万次学习

收藏

精品课程

更多
前端入门_HTML5
前端入门_HTML5

共29课时

65万人学习

CSS视频教程-玉女心经版
CSS视频教程-玉女心经版

共25课时

41.3万人学习

JavaScript极速入门_玉女心经系列
JavaScript极速入门_玉女心经系列

共43课时

76.7万人学习

独孤九贱(1)_HTML5视频教程
独孤九贱(1)_HTML5视频教程

共25课时

64.2万人学习

独孤九贱(2)_CSS视频教程
独孤九贱(2)_CSS视频教程

共22课时

24.4万人学习

独孤九贱(3)_JavaScript视频教程
独孤九贱(3)_JavaScript视频教程

共28课时

36.3万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时

133.4万人学习

热门下载

更多
phpStudy 2018最新版
phpStudy 2018最新版

集成PHP7,16种组合,超全大合集

下载

VC9 32位
VC9 32位

VC9 32位 phpstudy集成安装环境运行库

下载

VC11 32位
VC11 32位

VC11 32位 phpstudy集成安装环境运行库​

下载

php程序员工具箱完整版
php程序员工具箱完整版

程序员工具箱 v1.0 php集成环境

下载

VC14 32位
VC14 32位

VC14 32位 phpstudy安装环境运行库

下载

SublimeText3汉化版
SublimeText3汉化版

中文版,非常好用

下载

Notepad++ Windows版
Notepad++ Windows版

Notepad++ 8.9.7 Windows版官方安装包,适合 Windows 用户进行代码编辑、文本处理、语法高亮和插件扩展配置。

下载