objective-c 使用文件(二)

使用路径:NSPathUtilities.h

 

NSPathUtilities.h包含了NSString的函数和分类扩展,它允许你操作路径名。

 

下面是一个例子:

  1. #import <Foundation/NSArray.h>  
  2. #import <Foundation/NSString.h>  
  3. #import <Foundation/NSFileManager.h>  
  4. #import <Foundation/NSAutoreleasePool.h>  
  5. #import <Foundation/NSPathUtilities.h>  
  6.   
  7.   
  8. int main(int argc, const char *argv[])  
  9. {  
  10.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  11.     NSFileManager *fm;  
  12.     NSString *fName = @"path.m";  
  13.     NSString *path,*tempdir,*extension,*homedir,*fullpath;  
  14.     NSString *upath = @"~stevekochan/progs/../ch16/./path.m";  
  15.     NSArray *components;  
  16.   
  17.   
  18.     fm = [NSFileManager defaultManager];  
  19.   
  20.     //Get the temporary working directory  
  21.   
  22.     tempdir = NSTemporaryDirectory();  
  23.     NSLog(@"Temporary Directory is %@",tempdir);  
  24.   
  25.     //Extract the base directory from current directory  
  26.     path  = [fm currentDirectoryPath];  
  27.     NSLog(@"Base dir is %@",[path lastPathComponent]);  
  28.   
  29.     //Create a full path to the file fName in current directory  
  30.     fullpath = [path stringByAppendingPathComponent:fName];  
  31.     NSLog(@"fullpath to %@ is %@",fName,fullpath);  
  32.   
  33.     //Get the file name extension  
  34.     extension = [fullpath pathExtension];  
  35.     NSLog(@"extension for %@ is %@", fullpath,extension);  
  36.   
  37.     //Get user's home directory  
  38.     homedir = NSHomeDirectory();  
  39.     NSLog(@"Your home directory is %@",homedir);  
  40.   
  41.     //Divide a path into its components  
  42.     components = [homedir pathComponents];  
  43.   
  44.     for(int i = 0; i < [components count]; i++)  
  45.     {  
  46.         NSLog(@"%@",[components objectAtIndex: i]);  
  47.     }  
  48.   
  49.     //"Standardize" a path  
  50.     NSLog(@"%@ => %@",upath,[upath stringByStandardizingPath]);  
  51.   
  52.     [pool drain];  
  53.     return 0;  
  54. }  

 

 

 

 

复制文件和使用NSProcessInfo类

 

使用例子:

  1. #import <Foundation/NSArray.h>  
  2. #import <Foundation/NSString.h>  
  3. #import <Foundation/NSFileManager.h>  
  4. #import <Foundation/NSAutoreleasePool.h>  
  5. #import <Foundation/NSPathUtilities.h>  
  6. #import <Foundation/NSProcessInfo.h>  
  7.   
  8. //Implement a basic copy utility  
  9. int main(int argc, const char *argv[])  
  10. {  
  11.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  12.     NSFileManager *fm;  
  13.     NSString *source,*dest;  
  14.     BOOL isDir;  
  15.     NSProcessInfo *proc = [NSProcessInfo processInfo];  
  16.     NSArray *args = [proc arguments];  
  17.   
  18.     fm = [NSFileManager defaultManager];  
  19.   
  20.     //Check for two arguments on the command line  
  21.     if([args count] != 3)  
  22.     {  
  23.         NSLog(@"Usage: %@ src dest ",[proc processName]);  
  24.         return 1;  
  25.     }  
  26.   
  27.     source = [args objectAtIndex:1];  
  28.     dest = [args objectAtIndex:2];  
  29.   
  30.     //Make sure the source file can be read  
  31.     if([fm isReadableFileAtPath: source] == NO)  
  32.     {  
  33.         NSLog(@"Can't read %@",source);  
  34.         return 2;  
  35.     }  
  36.   
  37.     //See if the destination file is a directory  
  38.     //if it is , add the source to the end of the destination  
  39.   
  40.     [fm fileExistsAtPath: dest isDirectory: &isDir];  
  41.   
  42.     if(isDir == YES)  
  43.     {  
  44.         dest = [dest stringByAppendingPathComponent:[source lastPathComponent]];  
  45.     }  
  46.   
  47.     //Remove the destination file if it already exists  
  48.     [fm removeFileAtPath:dest handler:nil];  
  49.   
  50.     //Okay,time to perform the copy  
  51.     if([fm copyPath: source toPath: dest handler:nil] == NO)  
  52.     {  
  53.         NSLog(@"Copy failed");  
  54.         return 3;  
  55.     }  
  56.   
  57.     NSLog(@"Copy of %@ to %@ succeeded!",source,dest);  
  58.   
  59.     [pool drain];  
  60.     return 0;  
  61. }  

 

 

 

 

基本文件操作:NSFileHandle

 

利用NSFileHandle类提供的方法,请允许我更有效地使用文件。一般而言,我们处理文件时都要经历以下三个步骤:

1.打开文件,并获取一个NSFileHandle对象,以便在后面的I/O操作中引用该文件。

2.对打开的文件执行I/O操作。

3.关闭文件。

 

下面的实例打开开始创建的原始testfile文件,读取它的内容,并将其复制到名为testout的文件中。

代码如下:

  1. #import <Foundation/NSObject.h>  
  2. #import <Foundation/NSString.h>  
  3. #import <Foundation/NSFileManager.h>  
  4. #import <Foundation/NSFileHandle.h>  
  5. #import <Foundation/NSAutoreleasePool.h>  
  6. #import <Foundation/NSData.h>  
  7.   
  8. //Implement a basic copy utility  
  9. int main(int argc, const char *argv[])  
  10. {  
  11.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  12.     NSFileHandle *inFile,*outFile;  
  13.     NSData *buffer;  
  14.   
  15.     //Open the file testfile.txt for reading  
  16.     inFile = [NSFileHandle fileHandleForReadingAtPath: @"testfile.txt"];  
  17.     if(inFile == nil)  
  18.     {  
  19.         NSLog(@"Open of testfile for reading failed");  
  20.         return 1;  
  21.     }  
  22.   
  23.     //Create output file first if necessary  
  24.     [[NSFileManager defaultManager] createFileAtPath: @"testout.txt" contents:nil attributes:nil];  
  25.   
  26.     //Now open outfile for writing  
  27.     outFile = [NSFileHandle fileHandleForWritingAtPath: @"testout.txt"];  
  28.   
  29.     if(outFile == nil)  
  30.     {  
  31.         NSLog(@"Open of testout for writing failed");  
  32.         return 2;  
  33.     }  
  34.   
  35.     //Truncate the output file since it may contain data  
  36.     [outFile truncateFileAtOffset:0];  
  37.   
  38.     //Read the data from inFile and write it to outFile  
  39.     buffer = [inFile readDataToEndOfFile];  
  40.   
  41.     [outFile writeData:buffer];  
  42.   
  43.     //Close the two files  
  44.     [inFile closeFile];  
  45.     [outFile closeFile];  
  46.   
  47.     //Verify the file's contents  
  48.     NSLog(@"%@",[NSString stringWithContentsOfFile:@"testout.txt"  
  49.           encoding: NSUTF8StringEncoding error:nil]);  
  50.   
  51.     [pool drain];  
  52.     return 0;  
  53. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值