首页 > 其他 > 详细

【练习】NSFile

时间:2014-04-07 07:45:29      阅读:456      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
//
//  main.m
//  file
//
//  Created by 裴烨烽 on 14-4-6.
//  Copyright (c) 2014年 _______Smart_______. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
       // 创建文件操作对象
        // 当前文件路径
        NSFileManager *fm=[NSFileManager defaultManager]; //文件操作对象
        NSLog(@"当前目录= %@",[fm currentDirectoryPath]);
        
        // 判断文件是否存在
        NSString *fPathName=@"/Foundation/Code/testFile";// 不区分大小写
        if([fm fileExistsAtPath:fPathName]==NO)
        {
            NSLog(@"文件(%@)不存在",fPathName);
        }
    
        // 复制文件
        if([fm copyItemAtPath:fPathName toPath:@"newFile" error:NULL]==NO)
        {
            NSLog(@"复制文件失败!");
        }
        // 判断文件内容是否相等
        if([fm contentsEqualAtPath:fPathName andPath:@"newFile"]==NO)
        {
            NSLog(@"文件不相等");
        }
        // 重命名文件名称
        if([fm moveItemAtPath:@"newFile" toPath:@"newFile2" error:NULL]==NO)
        {
            NSLog(@"文件重命名失败!");
        }
        // 打印文件内容
        
        NSLog(@"Current:%@",[NSString stringWithContentsOfFile:@"newFile2" encoding:NSUTF8StringEncoding error:NULL]);
        
        // 读取文件属性(字典属性
         NSDictionary *attr; // 属性
        if((attr = [fm attributesOfItemAtPath:@"newFile2" error:NULL]) == nil){
        {
            NSLog(@"无法获取文件属性!");
        }
        NSLog(@"文件大小为=%llu bytes",[[attr objectForKey:NSFileSize] unsignedLongLongValue]);
    
            
            // 删除文件
            
            if([fm removeItemAtPath:@"newFile2" error:NULL]==NO)
            {
                NSLog(@"文件删除失败");
            }
            NSLog(@"所有操作成功");
    }
    return 0;
}
}
bubuko.com,布布扣

 

【练习】NSFile,布布扣,bubuko.com

【练习】NSFile

原文:http://www.cnblogs.com/madeininfi/p/3649435.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!