nsfilemanger_skybeauty_新浪博客

本文详细介绍了如何使用Objective-C进行文件操作,包括文件是否存在检查、复制、比较、重命名和删除,以及获取文件属性,如大小等。

#import


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

{


    @autoreleasepool {

        

        // insert code here...

        NSFileManager * fm = [NSFileManager defaultManager];

        NSString * string = @"/Users/kanoubairin/Desktop/test";

        NSDictionary * dictionary =[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:2],@"apple",[NSNumber numberWithInt:11],@"orange",[NSNumber numberWithFloat:12.3],@"banana",[NSNumber numberWithDouble:10.56],@"pear", nil];

       NSDictionary * s;

       for(NSDictionary * s in dictionary)

        {

           NSLog(@"%@,%@",s,[dictionary objectForKey:s]);

        }

       if([fm fileExistsAtPath:string]==NO)

        {

            NSLog(@"file doesn't exist!");

           return 1;

        }

       if([fm copyPath:string toPath:@"newfile" handler:nil]==NO)

        {

            NSLog(@"file copy failed");

           return 2;

        }

       if([fm contentsEqualAtPath:string andPath:@"newfile"]==NO)

        {

            NSLog(@"files are not equal!");

           return 3;

        }

       if ([fm movePath:@"newfile" toPath:@"newfile2"handler:nil]==NO)

        {

            NSLog(@"file rename failed!");

           return 4;

        }

        if ((s=[fm fileAttributesAtPath:@"newfile2" traverseLink:NO])==nil)

        {

            

            NSLog(@"couldn't get file attributes!");

           return 5;

        }

        NSLog(@"file size is %i bytes",[[s objectForKey:NSFileSize] intValue]);

       if ([fm removeFileAtPath:string handler:nil]==NO)

        {

            NSLog(@"file remove failed!!");

           return 6;

        }

        NSLog(@"All operations were sucessful");

        NSLog(@"%@",[NSString stringWithContentsOfFile:@"newfile2" encoding:NSUTF8StringEncoding error:nil]);

        

        

    }

   return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值