iOS文件处理

来源:互联网 发布:会计电算化模拟软件 编辑:程序博客网 时间:2024/06/11 15:07
      //----------------------获取沙盒信息-----------------
       //获取应用程序根目录
       NSString *path1 = NSHomeDirectory();
       
       //获取docment的目录
       NSArray *patharr =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
       //取出数组内容
       NSString *path2 = [patharrobjectAtIndex:0];
       
       NSLog(@"path1:%@\n,patharr = %@\n,path2=%@\n",path1,patharr,path2);
       

       NSArray *patharr =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
       //取出数组内容
       NSString *path2 = [patharrobjectAtIndex:0];

       NSString *filePath = [path2stringByAppendingPathComponent:@"helloword.txt"];

       NSLog(@"filePath=%@",filePath);


       
       
       
       
       
       //-------------------文件操作--------------------
       //通过NSFileManager 创建文件
       //---- 初始化
       NSFileManager *fm  =[NSFileManager defaultManager];
       



       
       //---- 创建保存的路径
       NSArray *patharr =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
       //取出数组内容
       NSString *path = [patharrobjectAtIndex:0];
       //---- path :/Users/liwei/Documents
       //---- 创建文件保存的路径
       NSString *filePath = [pathstringByAppendingPathComponent:@"helloword.txt"];
       
       //---- filepath:/Users/liwei/Documents/helloword.txt
       
       
       NSString *text = @"我喜欢凤姐5.6,121212";
       //---- 定义data
       NSData *data = [textdataUsingEncoding:NSUTF8StringEncoding];
       //---- 写入文件
       BOOL isOK=[fm createFileAtPath:filePathcontents:data attributes:nil];   
       
       if (isOK) {
          NSLog(@"文件创建成功!");
       }else{
       
          NSLog(@"失败了!");
       }
       
       //-------------创建文件方法2:简单版
       
       NSString *str = @"缺课的人喜欢凤姐!";
       [strwriteToFile:@"/Users/liwei/Desktop/fengjie.txt" atomically:YESencoding:NSUTF8StringEncoding error:nil];
       
       

      //--------------------------创建目录----------------

       //withIntermediateDirectories
       // YES 如果文件夹不存在,则创建, 如果存在表示可以覆盖
       // NO  如果文件夹不存在,则创建,如果存在不可以覆盖
       NSString *dirPath =@"/Users/liwei/Desktop/test";
       BOOL isOK = [fm createDirectoryAtPath:dirPathwithIntermediateDirectories:NO attributes:nil error:nil];
       
       if (isOK) {
          NSLog(@"创建成功!");
       }else{
          NSLog(@"创建失败!");
       
       }
       
       //------------ 读取文件 -------------
       //三种方法读取文件,假设读取文件内容为NSString
       //1、通过NSData 读取文件
       //  1)读取文件到NSData
       NSString*filePath=@"/Users/liwei/Desktop/fengjie.txt";
       NSData *data2 = [NSDatadataWithContentsOfFile:filePath];
       //  2)将NSData转换为NSString
       NSString *contentStr1 = [[NSString alloc]initWithData:data2 encoding:NSUTF8StringEncoding];
      NSLog(@"fileContent------:%@",contentStr1);
       
       
       
       
       
       //2、根据系统路径,指定文件名的文件读取
       //  1)产生文件路径
       NSArray *patharr =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
       //取出数组内容
       NSString *path = [patharrobjectAtIndex:0];
       //---- path :/Users/liwei/Documents
       //---- 创建文件保存的路径
       NSString *filePath2 = [pathstringByAppendingPathComponent:@"helloword.txt"];
       NSLog(@"filePath2 : %@",filePath2);
       //  2)读取到NSData
       NSData *data3 = [NSDatadataWithContentsOfFile:filePath2];
       // 3)转换到NSString 
       NSString *contentStr3 = [[NSString alloc]initWithData:data3 encoding:NSUTF8StringEncoding];
       
       NSLog(@"contentStr3 : %@",contentStr3);
       
       //3、字符串使用文件进行初始化stringWithContentsOfFile:path
       //定义字符串的同时就用文件初始化
       
       NSString *contentStr4 = [NSStringstringWithContentsOfFile:@"/Users/liwei/Desktop/test.txt"encoding:NSUTF8StringEncoding error:nil];
       
       NSLog(@"contentStr4   %@",contentStr4);
       

       
       
       // ----------------- 移动文件 -------------

       NSString *formPath =@"/Users/liwei/Desktop/test2.txt";
       NSString *moveToPath =@"/Users/liwei/Desktop/test3/test.txt";
       //创建目录 /Users/liwei/Desktop/test3
       [fm createDirectoryAtPath:[moveToPathstringByDeletingLastPathComponent] withIntermediateDirectories:YESattributes:nil error:nil];
       
       NSError *err;
       //开始移动文件,并且返回移动结果 YES or NO
       BOOL isOK = [fm moveItemAtPath:formPathtoPath:moveToPath error:&err];
       if(isOK){
       
          NSLog(@"移动文件成功!");
       }else{
       
          NSLog(@"移动失败!");
       
       }

       
       
       //---------------文件复制------------------

       NSString *formPath =@"/Users/liwei/Desktop/test3/test.txt";
       NSString *copyToPath =@"/Users/liwei/Desktop/备份/test3.txt";
       //创建目录 /Users/liwei/Desktop/备份
       [fm createDirectoryAtPath:[copyToPathstringByDeletingLastPathComponent] withIntermediateDirectories:YESattributes:nil error:nil];
       
       NSError *err;
       //开始复制文件,并且返回移动结果 YES or NO
       BOOL isOK = [fm copyItemAtPath:formPathtoPath:copyToPath error:&err];
       if(isOK){
          
          NSLog(@"复制文件成功!");
       }else{
          
          NSLog(@"复制文件失败!");
          
       }
       

       //-----------------删除文件--------------

       NSString *deletePath =@"/Users/liwei/Desktop/备份/test3.txt";
       //判断要删除的文件是否存在
       if ([fm fileExistsAtPath:deletePath]) {
          
          NSLog(@"文件存在!");
          //如果存在,则删除文件
           if ([fmremoveItemAtPath:deletePath error:nil]) {
              NSLog(@"文件删除成功!");
          }else{
              NSLog(@"文件删除失败!");
           }
          
       }else{
          //提示要删除的文件不存在
          NSLog(@"您要删除的文件不存在!");
       }
       
       
       
       //----------------目录列表--------------
       //设定要读取的目录
       NSString *userDirPath =@"/Users/liwei/Desktop";//NSHomeDirectory();
       
       //定义枚举对象
       NSDirectoryEnumerator *dirEnum = [fmenumeratorAtPath:userDirPath];
       //定义变量存储路径
       NSString *dirPath =nil;
       
       //遍历目录,打印所有的文件或者子目录名称
       while ((dirPath = [dirEnum nextObject])!=nil){
          NSLog(@"list dir: --- %@",dirPath);
       }
       
       
    }
    return 0;