如何重命名 Documents 目录中的文件? [英] How to rename files from Documents directory?

查看:76
本文介绍了如何重命名 Documents 目录中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am saving my files in Document directory named 1.png, 2.png, 3.png, 4.png, 5.png. If I delete the original file 3.png, how do I rename files 4.png and 5.png to be called 3.png and 4.png respectively?

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

This is the code I am using to write the files in the first place:

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

推荐答案

获取 NSDocuments 目录使用:

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

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

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

How to rename file you can find here:

如何使用 NSFileManager 重命名文件

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

If you do not know the names of the files in documents directory you can use :

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

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

This array contains all filenames you need. So you can rename them all.

这篇关于如何重命名 Documents 目录中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆