#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;
}
本文详细介绍了如何使用Objective-C进行文件操作,包括文件是否存在检查、复制、比较、重命名和删除,以及获取文件属性,如大小等。
1万+

被折叠的 条评论
为什么被折叠?



