obj-c編程10:Foundation庫中類的使用(3)[文件管理]
好吧,不管神馬係統都無可避免的要說到文件,目錄,路徑(PATH)管理的內容,下麵我們來看看在F庫中對他們的支持.我簡單看了下,不談其他光從方法命名來說就多少顯得有點複雜,如果和ruby相比就嗬嗬了.
以下代碼功能包括文件的打開,複製,移動,取其內容,文件夾的2種遍曆方法(遞歸和非遞歸):
#import <Foundation/Foundation.h> #define msg(...) NSLog(__VA_ARGS__) int main(int argc, char *argv[]){ @autoreleasepool { NSString *path = @"data.db"; NSFileManager *fm; NSDictionary *attr; fm = [NSFileManager defaultManager]; //check file is existed if([fm fileExistsAtPath: path] == NO){ NSLog(@"file %@ don't exist!",path); return 1; } //copy to new file ,new_file name must give if([fm copyItemAtPath: path toPath: @"new_data.db" error:NULL] == NO){ NSLog(@"file copy failed!"); return 2; } //move file if([fm moveItemAtPath:path toPath:@"fixed_data.db" error:NULL] == NO){ NSLog(@"file rename failed!"); return 3; } //cmp 2 file by contents if([fm contentsEqualAtPath:@"fixed_data.db" andPath:@"new_data.db"] == NO){ NSLog(@"files are not Equal!"); }else{ NSLog(@"file are Equal!"); } //get file size if((attr = [fm attributesOfItemAtPath:@"fixed_data.db" error:NULL]) == nil){ NSLog(@"can't get file attr!"); }else{ msg(@"file size is %llu bytes",\ [[attr objectForKey: NSFileSize] unsignedLongLongValue]); } //at last we remove file if([fm removeItemAtPath:@"new_data.db" error:NULL] == NO){ msg(@"file rm failed!"); } } return 0; }
值得注意的是對於文件拷貝和移動操作,如果目標文件已存在就會失敗.而且這兩種操作都要給出目標文件名哦,切記!
對於多次操作文件內容的情況我們可以用NSData類做緩存來用,下麵是另一種文件copy操作:
#import <Foundation/Foundation.h> #define msg(...) NSLog(__VA_ARGS__) int main(int argc, char *argv[]){ @autoreleasepool { NSFileManager *fm; NSData *data; fm = [NSFileManager defaultManager]; data = [fm contentsAtPath:@"data.db"]; if(data == nil){ NSLog(@"file read failed!"); return 1; } if([fm createFileAtPath:@"cp_data.db" contents:data attributes:nil] == NO){ NSLog(@"can't copy file"); return 2; } } return 0; }
對於目錄的遍曆可以使用enumeratorAtPath或者contentsOfDirectoryAtPath:error:方法(瞧著名字起的...),前者會深入遞歸子文件夾中的文件而後者不會,不過也可以動態阻止其遞歸過程,我們在下麵的代碼終將會看到如何做:
#import <Foundation/Foundation.h> #define msg(...) NSLog(__VA_ARGS__) int main(int argc, char *argv[]){ @autoreleasepool { NSString *path; NSFileManager *fm; NSDirectoryEnumerator *dir_er; NSArray *files; fm = [NSFileManager defaultManager]; path = [fm currentDirectoryPath]; dir_er = [fm enumeratorAtPath:path]; msg(@"PART1 : file list in %@:",path); while((path = [dir_er nextObject]) != nil) msg(@"%@",path); path = [fm currentDirectoryPath]; files = [fm contentsOfDirectoryAtPath:path error:NULL]; msg(@"PART2 : all files in path :%@",files); BOOL flag = NO; dir_er = [fm enumeratorAtPath:path]; msg(@"PART3 : file list in %@:",path); while((path = [dir_er nextObject]) != nil){ msg(@"%@",path); [fm fileExistsAtPath:path isDirectory:&flag]; if(flag == YES) [dir_er skipDescendents]; } } return 0; }
注意第一次枚舉器dir_er枚舉到結尾返回後,再一次枚舉會直接返回,因為已經沒有要枚舉的對象了,我沒有找到類似於其他語言中的rewind方法,所以隻有重新給dir_er賦值鳥.運行結果如下:
wisy@wisy-ThinkPad-X61:~/src/objc_src$ ./f 2014-07-02 13:41:03.220 f[12245] PART1 : file list in /home/wisy/src/objc_src: 2014-07-02 13:41:03.222 f[12245] a 2014-07-02 13:41:03.223 f[12245] class_std 2014-07-02 13:41:03.223 f[12245] class_std/Box.m 2014-07-02 13:41:03.223 f[12245] class_std/test.m 2014-07-02 13:41:03.223 f[12245] class_std/cls_test 2014-07-02 13:41:03.223 f[12245] class_std/Box.h 2014-07-02 13:41:03.223 f[12245] class_std/cls_test.d 2014-07-02 13:41:03.223 f[12245] cp_data.db 2014-07-02 13:41:03.223 f[12245] t 2014-07-02 13:41:03.223 f[12245] dzh.m 2014-07-02 13:41:03.223 f[12245] var.m 2014-07-02 13:41:03.223 f[12245] t.d 2014-07-02 13:41:03.223 f[12245] class.m 2014-07-02 13:41:03.223 f[12245] f.m 2014-07-02 13:41:03.223 f[12245] a.d 2014-07-02 13:41:03.224 f[12245] f 2014-07-02 13:41:03.224 f[12245] normal.m 2014-07-02 13:41:03.224 f[12245] data.db 2014-07-02 13:41:03.224 f[12245] a.m 2014-07-02 13:41:03.224 f[12245] f.d 2014-07-02 13:41:03.224 f[12245] PART2 : all files in path :(a, "class_std", "cp_data.db", t, "dzh.m", "var.m", "t.d", "class.m", "f.m", "a.d", f, "normal.m", "data.db", "a.m", "f.d") 2014-07-02 13:41:03.224 f[12245] PART3 : file list in /home/wisy/src/objc_src: 2014-07-02 13:41:03.224 f[12245] a 2014-07-02 13:41:03.224 f[12245] class_std 2014-07-02 13:41:03.224 f[12245] cp_data.db 2014-07-02 13:41:03.224 f[12245] t 2014-07-02 13:41:03.224 f[12245] dzh.m 2014-07-02 13:41:03.224 f[12245] var.m 2014-07-02 13:41:03.224 f[12245] t.d 2014-07-02 13:41:03.224 f[12245] class.m 2014-07-02 13:41:03.224 f[12245] f.m 2014-07-02 13:41:03.225 f[12245] a.d 2014-07-02 13:41:03.225 f[12245] f 2014-07-02 13:41:03.225 f[12245] normal.m 2014-07-02 13:41:03.225 f[12245] data.db 2014-07-02 13:41:03.225 f[12245] a.m 2014-07-02 13:41:03.225 f[12245] f.d
待續
最後更新:2017-04-03 05:38:54