objective-c之文件处理

来源:互联网 发布:朱峰 知乎 编辑:程序博客网 时间:2024/06/06 04:31
////  main.m//  文件处理////  Created by wu jianhua on 16-8-3.//  Copyright (c) 2016年 wujianhua. All rights reserved.//#import <Foundation/Foundation.h>//http://www.yiibai.com/objective_c/objective_c_file_handling.htmlint main(int argc, const char * argv[]){    char* path[1024]={0};    printf("currentDir=%s",getcwd(path, 1024));        @autoreleasepool {                      NSFileManager *fileManager = [NSFileManager defaultManager];        //Get documents directory        NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);                NSString *documentsDirectoryPath = [directoryPaths objectAtIndex:0];        NSLog(@"documentsDirectoryPath=%@",documentsDirectoryPath);                        documentsDirectoryPath=@"/Users/wujianhua/Desktop/test/hehe.txt";                if ([fileManager fileExistsAtPath:documentsDirectoryPath]==YES) {            NSLog(@"File exists");        }                if ([fileManager isWritableFileAtPath:documentsDirectoryPath]) {            NSLog(@"isWritable");        }        if ([fileManager isReadableFileAtPath:documentsDirectoryPath]) {            NSLog(@"isReadable");        }        if ( [fileManager isExecutableFileAtPath:documentsDirectoryPath]){            NSLog(@"is Executable");        }                if ([fileManager contentsEqualAtPath:@"/Users/wujianhua/Desktop/test/hehe.txt" andPath:@" /Users/wujianhua/Desktop/test/hehe.txt"]) {            NSLog(@"Same content");        }else{            NSLog(@"no Same content");        }                //复制文件        if ([fileManager copyItemAtPath:@"/Users/wujianhua/Desktop/test/hehe.txt"                                 toPath:@"/Users/wujianhua/Desktop/test/hehe2.txt"  error:NULL]) {            NSLog(@"Copied successfully");        }                //移动文件        if([fileManager moveItemAtPath:@"/Users/wujianhua/Desktop/test/hehe2.txt"                                toPath:@"/Users/wujianhua/Desktop/hehe2.txt" error:NULL]){            NSLog(@"Moved successfully");        }               //删除文件       // if ([fileManager removeItemAtPath:@"/Users/wujianhua/Desktop/hehe2.txt" error:NULL]) {            NSLog(@"Removed successfully");       // }        //读取文件                NSData *data = [fileManager contentsAtPath:@"/Users/wujianhua/Desktop/hehe.txt"];        //写文件        [fileManager createFileAtPath:@"/Users/wujianhua/Desktop/hehe_out.txt" contents:data attributes:nil];                                            }    return 0;}

0 0
原创粉丝点击