IOS - 檔處理

簡介

檔處理不能直觀地與應用程式進行說明,因此,用於處理檔的主要方法解釋如下。請注意,應用程式包只有讀取許可權,我們不會修改檔。無論如何,我們可以修改應用程式的檔目錄。

檔處理的方法

下麵列出了用於訪問和操作檔的方法的列表中。下麵我們就來替換,FilePath2 FILEPATH 和 FilePath1字串到我們所需的完整的檔路徑,以獲得所需的動作

檢查檔是否存在一個路徑中

   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];

接下來是什麼?

我們已經成功地瞭解各種檔訪問和操縱技術,現在可以做各種操作檔和使用。


上一篇: iOS - 音頻和視頻 下一篇: IOS - 訪問地圖