【问题标题】:How to rename files from Documents directory?如何重命名文档目录中的文件?
【发布时间】:2013-03-18 21:53:58
【问题描述】:

我将我的文件保存在名为 1.png2.png3.png4.png5.png 的文档目录中。如果删除原始文件3.png,如何将文件4.png5.png分别重命名为3.png4.png

这是我最初用来编写文件的代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[appDel.photo addObject:@"photo"];
NSString *imageName = [NSString stringWithFormat:@"%i.png", [appDel.photo count]];
appDel.numberPhoto = [appDel.photo count];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
                  imageName];
NSData* data = UIImagePNGRepresentation(image); 
[data writeToFile:path atomically:YES];

【问题讨论】:

  • 您是否尝试过编写任何可以为您执行此操作的代码?
  • 这就是重点。我不知道如何获取文件并重命名它(((并且我不知道如何编写此代码
  • 如果您通过代码将文件保存到您的目录,那么您可以通过相同的方法重命名它们。在方法中改写文件名即可。
  • 如何保存文件? plist,SQL,保存方法是什么?

标签: iphone ios objective-c xcode


【解决方案1】:

要获取NSDocuments 目录,请使用:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MyFile.txt"];

如何重命名文件,您可以在此处找到:

How to rename a file using NSFileManager

如果您不知道文档目录中的文件名,您可以使用:

NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];

此数组包含您需要的所有文件名。所以你可以重命名它们。

【讨论】:

  • 是的,我知道我的文件名。谢谢,我会试试这个关于重命名的建议
  • 谢谢,您的建议对我有帮助)=)
猜你喜欢
  • 1970-01-01
  • 2010-09-19
  • 1970-01-01
  • 2015-10-08
  • 2018-12-07
  • 2015-04-23
  • 2012-09-06
  • 2020-08-30
  • 2013-06-12
相关资源
最近更新 更多