做自(zì)由與創造的先行者

iOS文(wén)件處理(lǐ)

iOS開(kāi)發手冊

IOS文(wén)件處理(lǐ)

簡介

文(wén)件處理(lǐ)不能(néng)直觀的通過應用(yòng)程序來(lái)解釋,我們可以從(cóng)以下(xià)實例來(lái)了(le)解IOS的文(wén)件處理(lǐ)。

IOS中對(duì)文(wén)件的操作(zuò). 因爲應用(yòng)是在沙箱(sandbox)中的,在文(wén)件讀寫權限上(shàng)受到(dào)限制,隻能(néng)在幾個目錄下(xià)讀寫文(wén)件。

文(wén)件處理(lǐ)中使用(yòng)的方法

下(xià)面列出了(le)用(yòng)于訪問和(hé)操作(zuò)文(wén)件的方法的列表。

以下(xià)實例你(nǐ)必須替換FilePath1、FilePath和(hé)FilePath字符串爲完整的文(wén)件路徑,以獲得所需的操作(zuò)。

檢查文(wén)件是否存在

NSFileManager *fileManager = [NSFileManager defaultManager];

//Get documents directory

NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains

(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectoryPath = [directoryPaths objectAtIndex:0];

if ([fileManager fileExistsAtPath:@""]==YES) {

NSLog(@"File exists");

}

比較兩個文(wén)件的内容

if ([fileManager contentsEqualAtPath:@"FilePath1" andPath:@" FilePath2"]) {

NSLog(@"Same content");

}

檢查是否可寫、可讀、可執行文(wén)件

if ([fileManager isWritableFileAtPath:@"FilePath"]) {

NSLog(@"isWritable");

}

if ([fileManager isReadableFileAtPath:@"FilePath"]) {

NSLog(@"isReadable");

}

if ( [fileManager isExecutableFileAtPath:@"FilePath"]){

NSLog(@"is Executable");

}

移動文(wén)件

if([fileManager moveItemAtPath:@"FilePath1"

toPath:@"FilePath2" error:NULL]){

NSLog(@"Moved successfully");

}

複制文(wén)件

if ([fileManager copyItemAtPath:@"FilePath1"

toPath:@"FilePath2" error:NULL]) {

NSLog(@"Copied successfully");

}

删除文(wén)件

if ([fileManager removeItemAtPath:@"FilePath" error:NULL]) {

NSLog(@"Removed successfully");

}

讀取文(wén)件

NSData *data = [fileManager contentsAtPath:@"Path"];

寫入文(wén)件

[fileManager createFileAtPath:@"" contents:data attributes:nil];

網站(zhàn)建設開(kāi)發|APP設計(jì)開(kāi)發|小(xiǎo)程序建設開(kāi)發
上(shàng)一篇:iOS相機管理(lǐ)