iOS文件处理

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];

若文章对你有帮助,可以点赞或打赏支持我们。发布者:lyh会员,转载请注明出处:http://61.174.243.28:13541/AY-knowledg-hub/ios%e6%96%87%e4%bb%b6%e5%a4%84%e7%90%86/

(0)
lyhlyh会员认证作者
上一篇 2023年 4月 1日 下午8:51
下一篇 2023年 4月 1日 下午8:57

相关推荐

  • strings

    文章目录strings补充说明语法选项实例 strings 在对象文件或二进制文件中查找可打印的字符串 补充说明 strings命令 在对象文件或二进制文件中查找可打印的字符串。字…

    入门教程 2024年 3月 11日
  • expr

    文章目录expr补充说明语法选项参数实例 expr 一款表达式计算工具 补充说明 expr命令 是一款表达式计算工具,使用它完成表达式的求值操作。 expr的常用运算符: 加法运算…

    入门教程 2023年 12月 14日
  • iotop

    文章目录iotop补充说明安装语法选项实例 iotop 用来监视磁盘I/O使用状况的工具 补充说明 iotop命令 是一个用来监视磁盘I/O使用状况的top类工具。iotop具有与…

    入门教程 2023年 12月 19日
  • ag

    文章目录ag补充说明语法选项实例 ag ack 的升级版,C语言编写,更快更人性化 补充说明 摘自 https://github.com/ggreer/the_silver_sea…

    入门教程 2023年 12月 6日
  • groupmod

    文章目录groupmod补充说明语法选项参数 groupmod 更改群组识别码或名称 补充说明 groupmod命令 更改群组识别码或名称。需要更改群组的识别码或名称时,可用gro…

    入门教程 2023年 12月 14日
  • declare

    文章目录declare语法主要用途选项参数返回值例子讨论注意 declare 声明变量,设置或显示变量的值和属性。 语法 declare [-aAfFgilnrtux] [-p] …

    入门教程 2023年 12月 7日
  • mailstat

    文章目录mailstat补充说明语法选项参数 mailstat 显示到达的邮件状态 补充说明 mailstat命令 用来显示到达的邮件状态。 语法 mailstat(选项)(参数)…

    入门教程 2024年 1月 3日
  • dpkg-preconfigure

    文章目录dpkg-preconfigure补充说明语法选项参数实例 dpkg-preconfigure Debian Linux中软件包安装之前询问问题 补充说明 dpkg-pre…

    入门教程 2023年 12月 7日
  • Java 循环结构 – for, while 及 do…while

    顺序结构的程序语句只能被执行一次。 如果您想要同样的操作执行多次,就需要使用循环结构。 Java中有三种主要的循环结构: while 循环 do…while 循环 for 循环 在…

    入门教程 2023年 3月 4日
  • ip6tables

    文章目录ip6tables补充说明语法选项实例 ip6tables linux中防火墙软件 补充说明 ip6tables命令 和iptables一样,都是linux中防火墙软件,不…

    入门教程 2023年 12月 19日
Translate »