您的位置:首页 > 其它

oc write read file

2012-08-27 10:44 267 查看
#import <Foundation/Foundation.h>

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

//write file
NSString *myString = @"Hello World";
NSError *error;
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/file.txt"];
NSLog(@"File Path: %@",path);

if(![myString writeToFile:path atomically:YES encoding: NSUTF8StringEncoding error:&error]){
NSLog(@"Error writing to file: %@",[error localizedDescription]);
//return;
}else{
NSLog(@"String successfully written to file");
}

//read file
NSString *inString =[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

if(!inString){
NSLog(@"Error reading from file % %@",[path lastPathComponent],[error localizedDescription]);
}else{
NSLog(@"%@",inString);
}

NSURL *url = [NSURL fileURLWithPath:path];
NSData *data = [NSData dataWithContentsOfURL:url];
NSLog(@"%d",[data length]);
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: