objective-c 使用文件(一)

 

objective-c 使用文件(一)

分類: objective-c 1570人閱讀 評論(0) 收藏 舉報

  Foundation 框架請允許你利用文件系統對文件或目錄執行基本操作。這些基本操作是由NSFileManager類提供的,

這個類的方法具有如下功能:

  • 創建 一個新文件
  • 從現有文件中讀取數據
  • 將數據寫入文件中
  • 重新命名文件
  • 刪除文件
  • 測試文件是否存在
  • 確定文件的大小及其他屬性
  • 複製文件
  • 測試兩個文件的內容是否相同

上述多數據操作也可以直接對目錄進行。例如,可以創建目錄,讀取其中的內容,或者刪除目錄。

 

管理文件和目錄

每個文件方法都是對NSFileManager對象的調用,而NSFileManager對象 是通過向類發送一條defaultManager消息創建的,

如下所示:

 

NSFileManager *fm

 ...

fm = [NSFileManager defaultManager];

 

例如,要從當前目錄刪除名爲todolist的文件,首先要創建一個NSFileManager對象(如前面所示),然後調用removeFileAtPath方法,

代碼如下:

[fm removeFileAtPath: @"todolist" handler:nil];

可以測試返回結果,以確保文件刪除:

if([fm removeFileAtPath:@"todolist" handler:nil] == NO)

{

    NSLog(@"Couldn't remove file todolist");

    return 1;

}

 

下面是一個基本文件操作的例子:

  1. //Basic ifle operations  
  2. //Assumes the existence of a file called "testfile"  
  3. //in ther current working directory  
  4. #import <Foundation/NSObject.h>  
  5. #import <Foundation/NSString.h>  
  6. #import <Foundation/NSFileManager.h>  
  7. #import <Foundation/NSAutoreleasePool.h>  
  8. #import <Foundation/NSDictionary.h>  
  9.   
  10. int main(int argc, const char *argv[])  
  11. {  
  12.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  13.     NSString *fName = @"testfile.txt";  
  14.     NSFileManager *fm;  
  15.     NSDictionary *attr;  
  16.   
  17.     //Need to create an instance of the file manager  
  18.     fm = [NSFileManager defaultManager];  
  19.   
  20.     //Let's make sure our test file exists first  
  21.     if([fm fileExistsAtPath: fName] == NO)  
  22.     {  
  23.         NSLog(@"File doesn't exist'");  
  24.         return 1;  
  25.     }  
  26.   
  27.     //Now let's make a copy  
  28.   
  29.     if([fm copyPath: fName toPath: @"newfile.txt" handler:nil] == NO)  
  30.     {  
  31.         NSLog(@"File copy failed");  
  32.         return 2;  
  33.     }  
  34.   
  35.     //Let's test to see if the two files are identical  
  36.     if([fm contentsEqualAtPath: fName andPath: @"newfile.txt"] == NO)  
  37.     {  
  38.         NSLog(@"Files are not equal!");  
  39.         return 3;  
  40.     }  
  41.   
  42.     //Now let's rename the copy  
  43.   
  44.     if([fm movePath: @"newfile.txt" toPath: @"newfile2.txt" handler:nil] == NO)  
  45.     {  
  46.         NSLog(@"File rename failed!");  
  47.         return 4;  
  48.     }  
  49.   
  50.     //Get the size of newfile2  
  51.   
  52.     if((attr = [fm fileAttributesAtPath: @"newfile2.txt" traverseLink:NO]) == nil)  
  53.     {  
  54.         NSLog(@"Couldn't get file attributes!");  
  55.         return 5;  
  56.     }  
  57.   
  58.     NSLog(@"File size is %i bytes",[[attr objectForKey: NSFileSize] intValue]);  
  59.   
  60.     //And finally, let's delete the original file  
  61.     if([fm removeFileAtPath: fName handler:nil] == NO)  
  62.     {  
  63.         NSLog(@"File removal failed!");  
  64.         return 6;  
  65.     }  
  66.   
  67.     NSLog(@"All operations were successful!");  
  68.   
  69.     //Display the contents of the newly-created file  
  70.   
  71.     NSLog(@"%@",[NSString stringWithContentsOfFile:@"newfile2.txt" encoding:NSUTF8StringEncoding error:nil]);  
  72.   
  73.     [pool drain];  
  74.     return 0;  
  75. }  

 

 

 

使用NSData類

 

使用文件時,需要頻繁地將數據讀入一個臨時存儲區,它通常稱爲緩衝區。當收集數據,以便隨後將這些數據輸出到文件中時,

通常也使用存儲區。Foundation的NSData類提供了一種簡單的方式,它用來設置緩衝 區、將文件的內容讀入緩衝區,或將

緩衝區的內容寫到一個文件。

 

下面是一個使用NSData 的例子:

 

  1. #import <Foundation/NSObject.h>  
  2. #import <Foundation/NSString.h>  
  3. #import <Foundation/NSFileManager.h>  
  4. #import <Foundation/NSAutoreleasePool.h>  
  5. #import <Foundation/NSData.h>  
  6.   
  7. //make a copy of file  
  8. int main(int argc, const char *argv[])  
  9. {  
  10.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  11.     NSFileManager *fm;  
  12.     NSData *fileData;  
  13.   
  14.   
  15.     fm = [NSFileManager defaultManager];  
  16.   
  17.     //Read the file newfile2  
  18.     fileData = [fm contentsAtPath: @"newfile2.txt"];  
  19.   
  20.     if(fileData == nil)  
  21.     {  
  22.         NSLog(@"File read failed!");  
  23.         return 1;  
  24.     }  
  25.   
  26.     //Write the data to newfile3  
  27.     if([fm createFileAtPath: @"newfile3.txt" contents: fileData attributes:nil] == NO)  
  28.     {  
  29.         NSLog(@"Couldn't create the copy!");  
  30.         return 2;  
  31.     }  
  32.   
  33.     NSLog(@"File copy was successful!");  
  34.   
  35.     [pool drain];  
  36.     return 0;  
  37. }  

 

 

 

使用目錄

 

NSFileManager類中,還有很多操作目錄的方法。

下面是一個例子:

  1. #import <Foundation/NSObject.h>  
  2. #import <Foundation/NSString.h>  
  3. #import <Foundation/NSFileManager.h>  
  4. #import <Foundation/NSAutoreleasePool.h>  
  5.   
  6.   
  7. int main(int argc, const char *argv[])  
  8. {  
  9.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  10.     NSFileManager *fm;  
  11.     NSString *dirName = @"testdir";  
  12.     NSString *path;  
  13.   
  14.   
  15.     fm = [NSFileManager defaultManager];  
  16.   
  17.     //Get current directory  
  18.     path = [fm currentDirectoryPath];  
  19.     NSLog(@"Current directory path is %@",path);  
  20.   
  21.     //Create a new directory  
  22.     if([fm createDirectoryAtPath: dirName attributes: nil] == NO)  
  23.     {  
  24.         NSLog(@"Couldn't create directory!");  
  25.         return 1;  
  26.     }  
  27.   
  28.     //Rename the new directory  
  29.     if([fm movePath: dirName toPath: @"newdir" handler: nil] == NO)  
  30.     {  
  31.         NSLog(@"Directory rename failed!");  
  32.         return 2;  
  33.     }  
  34.   
  35.     //Change directory into the new directory  
  36.     if([fm changeCurrentDirectoryPath: @"newdir"] == NO)  
  37.     {  
  38.         NSLog(@"Change directory failed!");  
  39.         return 3;  
  40.     }  
  41.   
  42.     //Now get and display current working directory  
  43.     path = [fm currentDirectoryPath];  
  44.     NSLog(@"Current directory path is %@ ",path);  
  45.   
  46.     NSLog(@"All operations were successful!");  
  47.   
  48.   
  49.     [pool drain];  
  50.     return 0;  
  51. }  

 

 

 

 

枚舉目錄中的內容

 

 

   有時需要枚舉目錄是的內容,就需要用到enumeratorAtPath:方法或者directoryContentsAtPath: 方法,來完成枚舉過程。

如果使用第一種方法,一次可以枚舉指定目錄中的每個文件,默認情況下,如果其中一個文件爲目錄,那麼也會遞歸枚舉它的內容。

在這個過程中,通過向枚舉對象發送一條skipDescendants消息,可以動態的地阻止遞歸過程,從而不再枚舉目錄中的內容。

   對於directoryContentsAtPath:方法,使用這個方法,可心枚舉指定目錄的內容,並在一個數組中返回文件列表。

如果這個目錄中的任何文件本身是個目錄,這個方法並不遞歸枚舉它的內容。

 

下面一個例子是這兩個方法的使用:

  1. #import <Foundation/NSArray.h>  
  2. #import <Foundation/NSString.h>  
  3. #import <Foundation/NSFileManager.h>  
  4. #import <Foundation/NSAutoreleasePool.h>  
  5.   
  6. //enumeratorAtPath:方法和directoryContentsAtPath:方法  
  7. //的區別是:前者遍歷子目錄,後者不遍歷子目錄  
  8. int main(int argc, const char *argv[])  
  9. {  
  10.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  11.     NSFileManager *fm;  
  12.     NSString *path;  
  13.     NSDirectoryEnumerator *dirEnum;  
  14.     NSArray *dirArray;  
  15.   
  16.   
  17.     fm = [NSFileManager defaultManager];  
  18.   
  19.     //Get current directory path  
  20.     path = [fm currentDirectoryPath];  
  21.   
  22.     //Enumerate the directory  
  23.   
  24.     dirEnum = [fm enumeratorAtPath:path];  
  25.   
  26.     NSLog(@"Contents of %@: ",path);  
  27.     while((path = [dirEnum nextObject]) != nil)  
  28.     NSLog(@"%@",path);  
  29.   
  30.     //Another way to enumerate a directory  
  31.     dirArray = [fm directoryContentsAtPath:[fm currentDirectoryPath]];  
  32.     NSLog(@"Contents using directoryContentsAtPath:");  
  33.   
  34.     for(int i = 0; i < [dirArray count];i++)  
  35.     {  
  36.         NSLog(@"%@",[dirArray objectAtIndex: i]);  
  37.     }  
  38.   
  39.     [pool drain];  
  40.     return 0;  
  41. }  


 

objective-c 使用文件(二)

分類: objective-c 513人閱讀 評論(0) 收藏 舉報

使用路徑:NSPathUtilities.h

 

NSPathUtilities.h包含了NSString的函數和分類擴展,它允許你操作路徑名。


牛逼 作者 :

 


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