使用路径:NSPathUtilities.h
NSPathUtilities.h包含了NSString的函数和分类扩展,它允许你操作路径名。
下面是一个例子:
- #import <Foundation/NSArray.h>
- #import <Foundation/NSString.h>
- #import <Foundation/NSFileManager.h>
- #import <Foundation/NSAutoreleasePool.h>
- #import <Foundation/NSPathUtilities.h>
- int main(int argc, const char *argv[])
- {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- NSFileManager *fm;
- NSString *fName = @"path.m";
- NSString *path,*tempdir,*extension,*homedir,*fullpath;
- NSString *upath = @"~stevekochan/progs/../ch16/./path.m";
- NSArray *components;
- fm = [NSFileManager defaultManager];
- //Get the temporary working directory
- tempdir = NSTemporaryDirectory();
- NSLog(@"Temporary Directory is %@",tempdir);
- //Extract the base directory from current directory
- path = [fm currentDirectoryPath];
- NSLog(@"Base dir is %@",[path lastPathComponent]);
- //Create a full path to the file fName in current directory
- fullpath = [path stringByAppendingPathComponent:fName];
- NSLog(@"fullpath to %@ is %@",fName,fullpath);
- //Get the file name extension
- extension = [fullpath pathExtension];
- NSLog(@"extension for %@ is %@", fullpath,extension);
- //Get user's home directory
- homedir = NSHomeDirectory();
- NSLog(@"Your home directory is %@",homedir);
- //Divide a path into its components
- components = [homedir pathComponents];
- for(int i = 0; i < [components count]; i++)
- {
- NSLog(@"%@",[components objectAtIndex: i]);
- }
- //"Standardize" a path
- NSLog(@"%@ => %@",upath,[upath stringByStandardizingPath]);
- [pool drain];
- return 0;
- }
复制文件和使用NSProcessInfo类
使用例子:
- #import <Foundation/NSArray.h>
- #import <Foundation/NSString.h>
- #import <Foundation/NSFileManager.h>
- #import <Foundation/NSAutoreleasePool.h>
- #import <Foundation/NSPathUtilities.h>
- #import <Foundation/NSProcessInfo.h>
- //Implement a basic copy utility
- int main(int argc, const char *argv[])
- {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- NSFileManager *fm;
- NSString *source,*dest;
- BOOL isDir;
- NSProcessInfo *proc = [NSProcessInfo processInfo];
- NSArray *args = [proc arguments];
- fm = [NSFileManager defaultManager];
- //Check for two arguments on the command line
- if([args count] != 3)
- {
- NSLog(@"Usage: %@ src dest ",[proc processName]);
- return 1;
- }
- source = [args objectAtIndex:1];
- dest = [args objectAtIndex:2];
- //Make sure the source file can be read
- if([fm isReadableFileAtPath: source] == NO)
- {
- NSLog(@"Can't read %@",source);
- return 2;
- }
- //See if the destination file is a directory
- //if it is , add the source to the end of the destination
- [fm fileExistsAtPath: dest isDirectory: &isDir];
- if(isDir == YES)
- {
- dest = [dest stringByAppendingPathComponent:[source lastPathComponent]];
- }
- //Remove the destination file if it already exists
- [fm removeFileAtPath:dest handler:nil];
- //Okay,time to perform the copy
- if([fm copyPath: source toPath: dest handler:nil] == NO)
- {
- NSLog(@"Copy failed");
- return 3;
- }
- NSLog(@"Copy of %@ to %@ succeeded!",source,dest);
- [pool drain];
- return 0;
- }
基本文件操作:NSFileHandle
利用NSFileHandle类提供的方法,请允许我更有效地使用文件。一般而言,我们处理文件时都要经历以下三个步骤:
1.打开文件,并获取一个NSFileHandle对象,以便在后面的I/O操作中引用该文件。
2.对打开的文件执行I/O操作。
3.关闭文件。
下面的实例打开开始创建的原始testfile文件,读取它的内容,并将其复制到名为testout的文件中。
代码如下:
- #import <Foundation/NSObject.h>
- #import <Foundation/NSString.h>
- #import <Foundation/NSFileManager.h>
- #import <Foundation/NSFileHandle.h>
- #import <Foundation/NSAutoreleasePool.h>
- #import <Foundation/NSData.h>
- //Implement a basic copy utility
- int main(int argc, const char *argv[])
- {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- NSFileHandle *inFile,*outFile;
- NSData *buffer;
- //Open the file testfile.txt for reading
- inFile = [NSFileHandle fileHandleForReadingAtPath: @"testfile.txt"];
- if(inFile == nil)
- {
- NSLog(@"Open of testfile for reading failed");
- return 1;
- }
- //Create output file first if necessary
- [[NSFileManager defaultManager] createFileAtPath: @"testout.txt" contents:nil attributes:nil];
- //Now open outfile for writing
- outFile = [NSFileHandle fileHandleForWritingAtPath: @"testout.txt"];
- if(outFile == nil)
- {
- NSLog(@"Open of testout for writing failed");
- return 2;
- }
- //Truncate the output file since it may contain data
- [outFile truncateFileAtOffset:0];
- //Read the data from inFile and write it to outFile
- buffer = [inFile readDataToEndOfFile];
- [outFile writeData:buffer];
- //Close the two files
- [inFile closeFile];
- [outFile closeFile];
- //Verify the file's contents
- NSLog(@"%@",[NSString stringWithContentsOfFile:@"testout.txt"
- encoding: NSUTF8StringEncoding error:nil]);
- [pool drain];
- return 0;
- }