oc 常用文件操作

 

  • 寫文件
NSString* str=@"12345";
NSError *err=nil;
//path是路徑
[str writeToFile:path atomically:YES encoding:NSUnicodeStringEncoding error: &err];
//可以打印錯誤
NSLog(@"%@",err);

 

NSArray NSDictionary與其類似 都用writeToFile來寫

 

  • 讀文件
//讀plist 用dictionary讀, path是路徑
NSDictionary* dic=[[NSDictionary alloc] initWithContentsOfFile:path];

 

  • 文件管理 NSFileManager
NSFileManager *fm = [NSFileManager defaultManager];
//創建文件
NSString* path=@"/Users/1.csv";
[fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
//判斷路徑下文件是否存在
if([fm fileExistsAtPath:path] == YES){
        NSLog(@"存在");
 }
//刪除文件
[fm removeItemAtPath:path error:nil]; 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章