標籤:blog class tar int strong http
Objective-C檔案和目錄操作,IOS檔案操作,NSFileManager使用檔案操作:
objective-c通過使用NSFileManager類來管理和操作檔案、目錄,NSFileManager,檔案或目錄是使用檔案的路徑名的唯一標示。每個路徑名都是一個NSString對象。
NSFileManager對象通過defaultManager方法來建立執行個體
列如:
NSFileManager *fm = [NSFileManager defaultManager];
刪除某個檔案
[fm removeItemAtPath:@"filename" error:NULL];
error:參數是一個指向NSError對象的指標,能夠提供錯誤的資訊。如果指定為NULL的話就會使用預設的行為,傳回值是BOOL類型的方法,操作成功返回YES反之返回NO
判斷檔案是否被刪除
if([fm removeItemAtPath:@"filename" error:NULL]==NO){
NSLog(@"檔案刪除失敗");
return 1;
}
NSFileManager常用的檔案方法:
-(NSData*)contentsAtPath:path 從一個檔案中讀取資料
-(BOLL)createFileAtPath:path contents:(NSData*)data attributes: attr 向一個檔案寫入資料
-(BOOL)removeItemAtPath:path error:err 刪除一個檔案
-(BOOL)moveItemAtPath:from toPath:to error:err 重新命名或移動一個檔案(to 不能是已存在的)
-(BOOL)copyItemAtPath:from toPath:to error:err 複製檔案(to 不能是已存在的)
-(BOOL)contentsEqualAtPath:path1 andPath:path2 比較這兩個檔案的內容
-(BOOL)fileExistsAtPath:path 測試檔案是否存在
-(BOOL)isReadableFileAtPath:path 測試檔案是否存在,並且是否能執行讀操作
-(BOOL)isWritableFileAtPath:path 測試檔案是否存在,並且是否能執行寫操作
-(NSDictionary*)attributesOfItemAtPath:path error:err 擷取檔案的屬性
屬性字典允許你指定要建立的檔案的許可權,如果將該參數指定為nil,該檔案會被設定為預設許可權。
1、通過一段程式來對檔案進行操作:
[cpp] view plaincopyprint?
- //
- // main.m
- // NSFileManager_01
- //
- // Created by swinglife on 13-11-10.
- // Copyright (c) 2013年 swinglife. All rights reserved.
- //
-
- #import <Foundation/Foundation.h>
-
- int main(int argc, const char * argv[])
- {
-
- @autoreleasepool {
- //檔案名稱
- NSString *fileName = @"testFile";
- NSString *fileContent = @"這是檔案內容!!!!";
- NSData *fileData = [fileContent dataUsingEncoding:NSUTF8StringEncoding];
-
- //建立NSFileManager執行個體
- NSFileManager *fm = [NSFileManager defaultManager];
-
- //建立檔案
- [fm createFileAtPath:fileName contents:fileData attributes:nil];
-
- //判斷檔案是否存在 不存在就結束程式
- if([fm fileExistsAtPath:fileName]==NO){
- NSLog(@"檔案不存在");
- return 1;
- }
-
- //拷貝檔案
- if([fm copyItemAtPath:fileName toPath:@"newFile" error:NULL]==NO){
- NSLog(@"複製失敗");
- return 2;
- }
-
- //測試兩個檔案是否相同
- if([fm contentsEqualAtPath:fileName andPath:@"newFile"]==NO){
- NSLog(@"檔案不相同");
- return 3;
- }
-
- //重新命名newFile
- [fm moveItemAtPath:@"newFile" toPath:@"newFile2" error:NULL];
-
- //擷取newFile2的大小
- NSDictionary *fileAttr = [fm attributesOfItemAtPath:@"newFile2" error:NULL];
- if(fileAttr!=nil){
- NSLog(@"檔案大小:%llu bytes",[[fileAttr objectForKey:NSFileSize] unsignedLongLongValue]);
- }
-
- //刪除檔案
- [fm removeItemAtPath:fileName error:NULL];
-
- //顯示newFile2的內容
- NSString *data = [NSString stringWithContentsOfFile:@"newFile2" encoding:NSUTF8StringEncoding error:NULL];
- NSLog(@"%@",data);
-
-
- }
- return 0;
- }
NSFileManager常用的目錄方法
-(NSString*)currentDirectoryPath 擷取目前的目錄
-(BOOL)changeCurrentDirectoryPath:path 更改目前的目錄
-(BOOL)copyItemAtPath:from toPath:to error:err 複製目錄結構
-(BOOL)createDirectoryAtPath:path withIntermediateDirectories:(BOOL)flag attributes:attr 建立一個新目錄
-(BOOL)fileExistsAtPath:path isDirectory:(BOOL*)flag 測試檔案是不是目錄(flag中儲存結果)
-(NSArray*)contentsOfDirectoryAtPath:path error:err 列出目錄內容
-(NSDirectoryEnumerator*)enumeratorAtPath:path 枚舉目錄的內容
-(BOOL)removeItemAtPath:path error:err 刪除空目錄
-(BOOL)moveItemAtPath:from toPath:to error:err 重新命名或移動一個目錄
2、通過一段程式來對目錄進行操作:
[cpp] view plaincopyprint?
- //
- // main.m
- // NSFileManager_02
- //
- // Created by swinglife on 13-11-10.
- // Copyright (c) 2013年 swinglife. All rights reserved.
- //
-
- #import <Foundation/Foundation.h>
-
- int main(int argc, const char * argv[])
- {
-
- @autoreleasepool {
- //檔案目錄
- NSString *dirName = @"testDir";
-
- //建立NSFileManager執行個體
- NSFileManager *fm = [NSFileManager defaultManager];
-
- //擷取目前的目錄
- NSString *path = [fm currentDirectoryPath];
- NSLog(@"Path:%@",path);
-
- //建立新目錄
- [fm createDirectoryAtPath:dirName withIntermediateDirectories:YES attributes:nil error:NULL];
-
- //重新命名新的目錄
- [fm moveItemAtPath:dirName toPath:@"newDir" error:NULL];
-
- //更改目前的目錄到新的目錄
- [fm changeCurrentDirectoryPath:@"newDir"];
-
- //擷取當前工作目錄
- path = [fm currentDirectoryPath];
- NSLog(@"Path:%@",path);
-
- }
- return 0;
- }