如何重命名 Documents 目录中的文件?
问题描述:
我将文件保存在名为 1.png
、2.png
、3.png
、4 的 Document 目录中.png
, 5.png
.如果我删除了原始文件3.png
,如何将文件4.png
和5.png
重命名为3.png
和 4.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:
如果您不知道文档目录中文件的名称,您可以使用:
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.