【问题标题】:How to copy text file from document directory in iOS如何从iOS中的文档目录复制文本文件
【发布时间】:2013-07-23 12:15:15
【问题描述】:

您好,我正在尝试将文本文件从文档目录复制到位于文档目录中的文件夹名称 Temp

这是我尝试过但没有用的一些代码。

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

我该怎么做?

【问题讨论】:

    标签: ios cocoa-touch nsfilemanager


    【解决方案1】:

    这是解决方案。我需要添加目录以及文件名和扩展名。

    这里是代码。

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

    【讨论】:

      【解决方案2】:

      这可能有很多原因:

      1. 首先尝试打印documentsDirectoryToTempn和txtPath等所有路径,无论是否正确。

      2. 第二件事是利用那个错误变量:

        如果(错误) { NSLog("处理时出错:%@",[错误本地化描述]); }

        这样您就可以跟踪确切的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-30
        • 1970-01-01
        • 2013-06-16
        • 1970-01-01
        • 2015-10-20
        • 1970-01-01
        相关资源
        最近更新 更多