做(zuò)自(zì)由與創造的先行(xíng)者

iOS文件處理

iOS開(kāi)發手冊

IOS文件處理

簡介

文件處理不能(néng)直觀的通(tōng)過應用程序來解釋,我們可(kě)以從(cóng)以下(xià)實例來了(le)解IOS的文件處理。

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

文件處理中使用的方法

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

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

檢查文件是否存在

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");

}

比較兩個文件的內(nèi)容

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

NSLog(@"Same content");

}

檢查是否可(kě)寫、可(kě)讀、可(kě)執行(xíng)文件

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

NSLog(@"isWritable");

}

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

NSLog(@"isReadable");

}

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

NSLog(@"is Executable");

}

移動文件

if([fileManager moveItemAtPath:@"FilePath1"

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

NSLog(@"Moved successfully");

}

複制文件

if ([fileManager copyItemAtPath:@"FilePath1"

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

NSLog(@"Copied successfully");

}

删除文件

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

NSLog(@"Removed successfully");

}

讀取文件

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

寫入文件

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

網站建設開(kāi)發|APP設計開(kāi)發|小程序建設開(kāi)發
上(shàng)一(yī)篇:iOS相機管理