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的函数和分类扩展,它允许你操作路径名。


牛逼 作者 :

 


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