ios下移動檔案方法匯總

來源:互聯網
上載者:User

ios下移動檔案方法匯總

   這篇文章主要給大家匯總了一下ios下移動檔案方法,從簡單到複雜,十分的實用,有需要的小夥伴可以參考下。

  這段objective c代碼用於移動指定路徑下的檔案

  代碼如下:

  if ([fileManager copyItemAtPath:@"FilePath1"

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

  NSLog(@"Copied successfully");

  }

  方法二:

  使用 NSFileManager:

  讓您的文檔的路徑和您的緩衝路徑。遍曆所有的檔案,並將它們移動使用 NSFileManager

  代碼如下:

  - (void) moveAllDocs {

  NSFileManager *fileManager = [NSFileManager defaultManager];

  NSError *error = nil;

  NSString *sourceDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

  NSString *destinationDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

  NSArray *contents = [fileManager contentsOfDirectoryAtPath:sourceDirectory error:&error];

  for(NSString *sourceFileName in contents) {

  NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:sourceFileName];

  NSString *destFile = [destinationDirectory stringByAppendingPathComponent:sourceFileName];

  if(![fileManager moveItemAtPath:sourceFile toPath:destFile error:&error]) {

  NSLog(@"Error: %@", error);

  }

  }

  }

  方法三:

  FCFileManager 是一個構建在 NSFileManager 之上的 iOS 檔案管理工具,簡化了檔案管理。它提供了許多靜態方法,用於執行最常用的操作用幾行代碼。它的工作原理是預設的檔案目錄,允許使用相對路徑,但它可以在任何其他目錄中輕鬆工作。

  Move file:

  代碼如下:

  [FCFileManager moveItemAtPath:@"test.txt" toPath:@"tests/test.txt"];

  Remove file:

  代碼如下:

  //remove file at the specified path

  [FCFileManager removeItemAtPath:@"test.txt"];

  以上所述上就是本文的全部內容了,希望大家能夠喜歡。

相關文章

聯繫我們

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