2013-07-24 64 views
0

您好,我正在嘗試將文檔目錄中的文本文件複製到位於文檔目錄中的文件夾名稱Temp中。如何從iOS中的文檔目錄中複製文本文件

這裏是我嘗試過的一些代碼,並沒有工作。

self.fileManager = [NSFileManager defaultManager]; 
     NSError *error; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *documentsDirectoryToTemp = [NSString stringWithFormat:@"%@/Temp/",[paths objectAtIndex:0]]; 

     NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",tblGetNameToMove.textLabel.text]]; 

     [self.fileManager copyItemAtPath:txtPath toPath:documentsDirectoryToTemp error:&error]; 

我該怎麼做?

回答

0

這裏是解決方案。我需要添加目錄以及文件和擴展名。

這裏是代碼。

self.fileManager = [NSFileManager defaultManager]; 
     NSError *error; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 

     NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",tblGetNameToMove.textLabel.text]]; 

     NSString *fileName = [NSString stringWithFormat:@"%@/Temp/%@.txt",documentsDirectory,tblGetNameToMove.textLabel.text]; 

     [self.fileManager copyItemAtPath:txtPath toPath:fileName error:&error]; 
1

可能有許多原因:

  1. 首先嚐試打印像documentsDirectoryToTempn和txtPath所有的路徑是否正確與否。

  2. 第二件事情是使利用該錯誤變量:

    如果(誤差) { 的NSLog( 「錯誤而應對:%@」,[錯誤localizedDiscription]); }

    所以你可以跟蹤確切的問題。

相關問題