objective-c 使用檔案(二)

來源:互聯網
上載者:User

使用路徑:NSPathUtilities.h

 

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

 

下面是一個例子:

#import <Foundation/NSArray.h><br />#import <Foundation/NSString.h><br />#import <Foundation/NSFileManager.h><br />#import <Foundation/NSAutoreleasePool.h><br />#import <Foundation/NSPathUtilities.h></p><p>int main(int argc, const char *argv[])<br />{<br /> NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];<br /> NSFileManager *fm;<br /> NSString *fName = @"path.m";<br /> NSString *path,*tempdir,*extension,*homedir,*fullpath;<br /> NSString *upath = @"~stevekochan/progs/../ch16/./path.m";<br /> NSArray *components;</p><p> fm = [NSFileManager defaultManager];</p><p> //Get the temporary working directory</p><p> tempdir = NSTemporaryDirectory();<br /> NSLog(@"Temporary Directory is %@",tempdir);</p><p> //Extract the base directory from current directory<br /> path = [fm currentDirectoryPath];<br /> NSLog(@"Base dir is %@",[path lastPathComponent]);</p><p> //Create a full path to the file fName in current directory<br /> fullpath = [path stringByAppendingPathComponent:fName];<br /> NSLog(@"fullpath to %@ is %@",fName,fullpath);</p><p> //Get the file name extension<br /> extension = [fullpath pathExtension];<br /> NSLog(@"extension for %@ is %@", fullpath,extension);</p><p> //Get user's home directory<br /> homedir = NSHomeDirectory();<br /> NSLog(@"Your home directory is %@",homedir);</p><p> //Divide a path into its components<br /> components = [homedir pathComponents];</p><p> for(int i = 0; i < [components count]; i++)<br /> {<br /> NSLog(@"%@",[components objectAtIndex: i]);<br /> }</p><p> //"Standardize" a path<br /> NSLog(@"%@ => %@",upath,[upath stringByStandardizingPath]);</p><p> [pool drain];<br /> return 0;<br />}</p><p>

 

 

 

 

複製檔案和使用NSProcessInfo類

 

使用例子:

#import <Foundation/NSArray.h><br />#import <Foundation/NSString.h><br />#import <Foundation/NSFileManager.h><br />#import <Foundation/NSAutoreleasePool.h><br />#import <Foundation/NSPathUtilities.h><br />#import <Foundation/NSProcessInfo.h></p><p>//Implement a basic copy utility<br />int main(int argc, const char *argv[])<br />{<br /> NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];<br /> NSFileManager *fm;<br /> NSString *source,*dest;<br /> BOOL isDir;<br /> NSProcessInfo *proc = [NSProcessInfo processInfo];<br /> NSArray *args = [proc arguments];</p><p> fm = [NSFileManager defaultManager];</p><p> //Check for two arguments on the command line<br /> if([args count] != 3)<br /> {<br /> NSLog(@"Usage: %@ src dest ",[proc processName]);<br /> return 1;<br /> }</p><p> source = [args objectAtIndex:1];<br /> dest = [args objectAtIndex:2];</p><p> //Make sure the source file can be read<br /> if([fm isReadableFileAtPath: source] == NO)<br /> {<br /> NSLog(@"Can't read %@",source);<br /> return 2;<br /> }</p><p> //See if the destination file is a directory<br /> //if it is , add the source to the end of the destination</p><p> [fm fileExistsAtPath: dest isDirectory: &isDir];</p><p> if(isDir == YES)<br /> {<br /> dest = [dest stringByAppendingPathComponent:[source lastPathComponent]];<br /> }</p><p> //Remove the destination file if it already exists<br /> [fm removeFileAtPath:dest handler:nil];</p><p> //Okay,time to perform the copy<br /> if([fm copyPath: source toPath: dest handler:nil] == NO)<br /> {<br /> NSLog(@"Copy failed");<br /> return 3;<br /> }</p><p> NSLog(@"Copy of %@ to %@ succeeded!",source,dest);</p><p> [pool drain];<br /> return 0;<br />}</p><p>

 

 

 

 

基本檔案操作:NSFileHandle

 

利用NSFileHandle類提供的方法,請允許我更有效地使用檔案。一般而言,我們處理檔案時都要經曆以下三個步驟:

1.開啟檔案,並擷取一個NSFileHandle對象,以便在後面的I/O操作中引用該檔案。

2.對開啟的檔案執行I/O操作。

3.關閉檔案。

 

下面的執行個體開啟開始建立的原始testfile檔案,讀取它的內容,並將其複製到名為testout的檔案中。

代碼如下:

#import <Foundation/NSObject.h><br />#import <Foundation/NSString.h><br />#import <Foundation/NSFileManager.h><br />#import <Foundation/NSFileHandle.h><br />#import <Foundation/NSAutoreleasePool.h><br />#import <Foundation/NSData.h></p><p>//Implement a basic copy utility<br />int main(int argc, const char *argv[])<br />{<br /> NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];<br /> NSFileHandle *inFile,*outFile;<br /> NSData *buffer;</p><p> //Open the file testfile.txt for reading<br /> inFile = [NSFileHandle fileHandleForReadingAtPath: @"testfile.txt"];<br /> if(inFile == nil)<br /> {<br /> NSLog(@"Open of testfile for reading failed");<br /> return 1;<br /> }</p><p> //Create output file first if necessary<br /> [[NSFileManager defaultManager] createFileAtPath: @"testout.txt" contents:nil attributes:nil];</p><p> //Now open outfile for writing<br /> outFile = [NSFileHandle fileHandleForWritingAtPath: @"testout.txt"];</p><p> if(outFile == nil)<br /> {<br /> NSLog(@"Open of testout for writing failed");<br /> return 2;<br /> }</p><p> //Truncate the output file since it may contain data<br /> [outFile truncateFileAtOffset:0];</p><p> //Read the data from inFile and write it to outFile<br /> buffer = [inFile readDataToEndOfFile];</p><p> [outFile writeData:buffer];</p><p> //Close the two files<br /> [inFile closeFile];<br /> [outFile closeFile];</p><p> //Verify the file's contents<br /> NSLog(@"%@",[NSString stringWithContentsOfFile:@"testout.txt"<br /> encoding: NSUTF8StringEncoding error:nil]);</p><p> [pool drain];<br /> return 0;<br />}<br />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.