zoukankan      html  css  js  c++  java
  • 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];
    
     
  • 相关阅读:
    PHP 高精度计算
    PHPWord使用方法
    羽毛球
    大数据(2)
    大数据(1)
    Centos 7 启动错误:XFS_WANT_CORRUPTED_GOTO 修复
    selenium 自动化工具
    python 开发技巧(0)-- 各个系统的python安装
    Yii简单使用阿里云短信教程!
    VMware虚拟机 Ubuntu 实用技巧 (2)桥接模式连接网络与网卡的配置
  • 原文地址:https://www.cnblogs.com/qisedao0215/p/3725306.html
Copyright © 2011-2022 走看看