文件處理不能直觀地與應(yīng)用程序進(jìn)行說(shuō)明,因此,用于處理文件的主要方法解釋如下。請(qǐng)注意,應(yīng)用程序包只有讀取權(quán)限,我們不會(huì)修改文件。無(wú)論如何,我們可以修改應(yīng)用程序的文件目錄。
下面列出了用于訪問和操作文件的方法的列表中。下面我們就來(lái)替換,F(xiàn)ilePath2 FILEPATH 和 FilePath1字符串到我們所需的完整的文件路徑,以獲得所需的動(dòng)作
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"); }
if ([fileManager contentsEqualAtPath:@"FilePath1" andPath:@" FilePath2"]) { NSLog(@"Same content"); }
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];
我們已經(jīng)成功地了解各種文件訪問和操縱技術(shù),現(xiàn)在可以做各種操作文件和使用。