IOS 檔案管理 2,ios檔案管理

來源:互聯網
上載者:User

IOS 檔案管理 2,ios檔案管理
IOS開發-檔案管理(二)

五、Plist檔案

String方式添加              

NSString *path = [NSHomeDirectory( )  stringByAppendingPathComponent:@"Array.plist"];                    

NSString *content = @"abcd";            

[contect writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

Array方式添加        

NSString *path = [NSHomeDirectory( )  stringByAppendingPathComponent:@"Array.plist"];     

[NSArray *array = [[NSArray alloc] initWithObjects:@"123", @"798",@"000",nil];       [array writeToFile:path atomically:YES];

Dictionary方式添加          

NSString *path = [NSHomeDirectory( )  stringByAppendingPathComponent:@"Dic.plist"];                        

NSDictionary *dic = [NSDictionary alloc] initWithObjects:@"first",@"second",@"third"forKeys:@"123",@"456",@"798"];                                                                       [dic writeToFile:path atomically:YES];

  • 數組、字典只能將BOOL、NSNumber、NSString、NSData、NSDate、NSArray、NSDictionary寫入屬性列表plist檔案

六、讀取檔案類和常用方法

  • NSFileHandle類主要對檔案內容進行讀取和寫入操作

  • NSFileManager類主要對檔案的操作(刪除、修改、移動、複製等等)

常用處理方法

+ (id)fileHandleForReadingAtPath:(NSString *)path  開啟一個檔案準備讀取     

+ (id)fileHandleForWritingAtPath:(NSString *)path  開啟一個檔案準備寫入   

+ (id)fileHandleForUpdatingAtPath:(NSString *)path  開啟一個檔案準備更新  

-  (NSData *)availableData; 從裝置或通道返回可用的資料            

-  (NSData *)readDataToEndOfFile; 從當前的節點讀取到檔案的末尾               

-  (NSData *)readDataOfLength:(NSUInteger)length; 從當前節點開始讀取指定的長度資料                           

-  (void)writeData:(NSData *)data; 寫入資料         

-  (unsigned long long)offsetInFile;  擷取當前檔案的位移量            

-  (void)seekToFileOffset:(unsigned long long)offset; 跳到指定檔案的位移量     

-  (unsigned long long)seekToEndOfFile; 跳到檔案末尾        

-  (void)truncateFileAtOffset:(unsigned long long)offset; 將檔案的長度設為offset位元組

-  (void)closeFile;  關閉檔案

向檔案追加資料

NSString *homePath  = NSHomeDirectory( );        

NSString *sourcePath = [homePath stringByAppendingPathConmpone:@"testfile.text"];                                            

NSFileHandle *fielHandle = [NSFileHandle fileHandleForUpdatingAtPath:sourcePath];                                                        

[fileHandle seekToEndOfFile];  將節點跳到檔案的末尾          

NSString *str = @"追加的資料"                   

NSData* stringData  = [str dataUsingEncoding:NSUTF8StringEncoding];          

[fileHandle writeData:stringData]; 追加寫入資料       

[fileHandle closeFile];

定位元據                    

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]; 擷取資料長度       

[fileHandle seekToFileOffset;length/2]; 位移量檔案的一半           

NSData *data = [fileHandle readDataToEndOfFile];                

[fileHandle closeFile];

複製檔案                           

NSFileHandle *infile, *outfile; 輸入檔案、輸出檔案          

NSData *buffer; 讀取的緩衝資料                    

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]; 建立讀取源路徑檔案

if (infile == nil)                          

{                                          

return NO;                      

}                           

outfile = [NSFileHandle fileHandleForReadingAtPath:outPath]; 建立病開啟要輸出的檔案                                                                                                                

if (outfile == nil)                            

{                                                               

return NO;                                                    

}                                             

[outfile truncateFileAtOffset:0]; 將輸出檔案的長度設為0         

buffer = [infile readDataToEndOfFile];  讀取資料           

[outfile writeData:buffer];  寫入輸入                        

[infile closeFile];        關閉寫入、輸入檔案               

[outfile closeFile];

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.