NSFileHandle

利用NSFilehandle類提供的方法,允許更有效地使用文件。
一般而言,處理文件時都要經(jīng)歷以下三個步驟:
1.打開文件,并獲取一個NSFileHandle對象,以便在后面的I/O操作中引用該文件
2.對打開的文件執(zhí)行I/O操作(讀取、寫入、更新)
3.關(guān)閉文件

注意:NSFileHandle類主要對文件內(nèi)容進行讀取和寫入操作,可以使用NSFileHandle做文件的斷點續(xù)傳。
NSFileHandle 此類主要是對文件內(nèi)容進行讀取和寫入操作
NSFileMange 此類主要是對文件進行的操作以及文件信息的獲取

常用處理方法

+ (id)fileHandleForReadingAtPath:(NSString *)path //打開一個文件準備讀取
+ (id)fileHandleForWritingAtPath:(NSString *)path //打開一個文件準備寫入
+ (id)fileHandleForUpdatingAtPath:(NSString *)path //打開一個文件準備更新
- (NSData *)availableData; //從設(shè)備或通道返回可用的數(shù)據(jù)
- (NSData *)readDataToEndOfFile; //從當前的節(jié)點讀取到文件的末尾
- (NSData *)readDataOfLength:(NSUInteger)length; // 從當前節(jié)點開始讀取指定的長度數(shù)據(jù)
- (void)writeData:(NSData *)data; //寫入數(shù)據(jù)
- (unsigned long long)offsetInFile; //獲取當前文件的偏移量
- (void)seekToFileOffset:(unsigned long long)offset; //跳到指定文件的偏移量
- (unsigned long long)seekToEndOfFile; //跳到文件末尾
- (void)truncateFileAtOffset:(unsigned long long)offset; //將文件的長度設(shè)為offset字節(jié)
- (void)closeFile; 關(guān)閉文件

基本用法一 追加數(shù)據(jù)

 NSString *homePath  = NSHomeDirectory( );
 NSString *sourcePath = [homePath stringByAppendingPathConmpone:@"testfile.text"];                
 NSFileHandle *fielHandle = [NSFileHandle fileHandleForUpdatingAtPath:sourcePath];                       
 [fileHandle seekToEndOfFile];  將節(jié)點跳到文件的末尾          
 NSString *str = @"追加的數(shù)據(jù)"                   
 NSData* stringData  = [str  dataUsingEncoding:NSUTF8StringEncoding];          
 [fileHandle writeData:stringData]; 追加寫入數(shù)據(jù)       
 [fileHandle closeFile];

基本用法一 定位數(shù)據(jù)

 NSFileManager *fm = [NSFileManager defaultManager];              
 NSString *content = @"abcdef";                      
 [fm createFileAtPath:path contents:[content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];                                                   
 NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];      
 NSUInteger length = [fileHandle availabelData] length]; 獲取數(shù)據(jù)長度       
 [fileHandle seekToFileOffset;length/2]; 偏移量文件的一半           
 NSData *data = [fileHandle readDataToEndOfFile];                
 [fileHandle closeFile];

基本用法一 復(fù)制文件

NSFileHandle *infile, *outfile;   //輸入文件、輸出文件          
NSData *buffer;    //讀取的緩沖數(shù)據(jù)                    
NSFileManager *fileManager = [NSFileManager defaultManager];   
NSString *homePath = NSHomeDirectory( );              
NSString *sourcePath = [homePath  stringByAppendingPathComponent:@"testfile.txt"];   // 源文件路徑                                          
NSString *outPath = [homePath stringByAppendingPathComponent:@"outfile.txt"];   //輸出文件路徑                               
BOOL sucess  = [fileManager createFileAtPath:outPath contents:nil attributes:nil];  
 if (!success)          
  {                                                      
     return N0;                                                                                                   
  }                 
infile = [NSFileHandle fileHandleForReadingAtPath:sourcePath];   //創(chuàng)建讀取源路徑文件
 if (infile == nil)                          
  {                                          
    return NO;                      
  }                           
outfile = [NSFileHandle fileHandleForReadingAtPath:outPath];   //創(chuàng)建并打開要輸出的文件                                                                                                                
  if (outfile == nil)                            
  {                                                               
      return NO;                                                    
  }                                             
[outfile truncateFileAtOffset:0]; //將輸出文件的長度設(shè)為0         
buffer = [infile readDataToEndOfFile];  //讀取數(shù)據(jù)           
[outfile writeData:buffer];  //寫入輸入                        
[infile closeFile];        //關(guān)閉寫入、輸入文件               
[outfile closeFile];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容