检查文件是否已保存到磁盘? [英] Check if file is already saved to disk?
问题描述
我从我的服务器下载文件,将它们保存到设备,并在我的应用程序中将它们显示给用户。我想实现检查以查看该文件是否已存在于设备上,以便我们可以跳过下载并显示,但我无法找到最佳方法。
I am downloading files from my server, saving them to device, and displaying them to the user in my app. I want to implement a check to see if the file already exists on the device so we can skip the download and just display, but I can't figure out the best way to do that.
我为每个文件创建一个唯一的fileName,然后将其转换为NSURL,如下所示:
I create a unique fileName for each file and then convert it to an NSURL like this:
NSString *fileString = [[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:fileName]];
self.fileURL = [[NSURL alloc] initFileURLWithPath:fileString];
然后我写入文件并保存URL以供不久之后使用:
Then I write to file and save the URL for use shortly after:
[data writeToFile:fileString atomically:YES];
self.fileURL = [[NSURL alloc] initFileURLWithPath:fileString];
如何查看该文件或URL是否已存在?
How can I check if that File or URL already exists?
谢谢
推荐答案
NSFileManager有一种方法来检查路径中是否存在文件:
NSFileManager has a method to check if a file exists at a path:
[[NSFileManager defaultManager] fileExistsAtPath: fileString]
这篇关于检查文件是否已保存到磁盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!