【问题标题】:How to find a file on iPad?如何在 iPad 上查找文件?
【发布时间】:2011-02-02 10:49:01
【问题描述】:

我应该已经使用应用程序在 iPad 模拟器上下载了一个文件。

如何检查此文件是否存在于 iPad 模拟器文件系统中?

是否有一些文件管理器可用于检查文件是否存在及其内容?

【问题讨论】:

    标签: iphone filesystems fileinfo


    【解决方案1】:

    您可以在以下位置找到模拟器

    /用户名/库/应用程序 支持/iPhone模拟器/4.2/

    其中“4.2”是 SDK 版本。 在该文件夹下,您可以在神秘的子文件夹中找到所有应用程序。其中之一是你的。 在那里您可以找到“文档”,这是您应用的个人文件夹。

    【讨论】:

    • 您的评论帮助我找到了我正在寻找的文件。但奇怪的是我没有在你提到的地方找到它。该文件位于我的计算机文件系统的根目录下,换句话说,在 / 下。这怎么可能 ?之后我做了一些测试,擦除文件并再次在 iPad 模拟器中执行应用程序。我可以在根目录下再次看到该文件。
    • 模拟器可以访问你的整个硬盘。您的代码将在真正的 iOS 设备上失败。您磁盘上的根目录是“/”,但对于您的应用程序,它应该是应用程序的文档文件夹,即 NSDocumentDirectory(这对于每个应用程序都是唯一的!)。您可能正在保存到“/myfile.txt”而不是“[App's Document root]/myfile.txt”。
    • /* NSString * theFolder = [NSHomeDirectory() stringByAppendingPathComponent:@""]; NSString * theFileName = [theFolder stringByAppendingPathComponent:@"localFile.txt"];*/ NSString * theFolder = [NSHomeDirectory() stringByAppendingPathComponent:NSDocumentDirectory]; NSString * theFileName = [theFolder stringByAppendingPathComponent:@"localFile.txt"];
    • 再次感谢您的评论。我明白了,你一定是对的。这是我用来设置语言环境文件的代码: NSString * theFolder = [NSHomeDirectory() stringByAppendingPathComponent:@""]; NSString * theFileName = [theFolder stringByAppendingPathComponent:@"localFile.txt"];应该是什么?我试过这个: NSString * theFolder = [NSHomeDirectory() stringByAppendingPathComponent:NSDocumentDirectory]; NSString * theFileName = [theFolder stringByAppendingPathComponent:@"localFile.txt"];但是我可以在我的 XCode 中看到一条警告消息,告诉我它一定不太正确。
    • 获取文档的文件夹(就是你想要的,不是HOME目录): NSString *homeDir = NSHomeDirectory(); NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDir = [arrayPaths objectAtIndex:0];主目录是应用程序的根目录,每次重新安装应用程序时都会更改。 “文档”是您的应用数据的位置,位于主页下方。
    【解决方案2】:

    使用这些方法,您可以验证文件目录中是否存在文件。我没有测试过这个确切的例子,但它应该可以工作。

    例子:

    NSString *filePath = [self dataFilePathInDocuments:@"MyFile.txt"];
    if([self fileExistsAtPath:filePath]) {
        NSLog(@"The file exits at %@", filePath);
    } else {
        NSLog(@"The doesn't exist at %@", filePath);
    }
    

    代码:

    - (NSString *)dataFilePathInDocuments:(NSString*)aFilename {
        NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDirectory = [paths objectAtIndex:0];
    
        return [docDirectory stringByAppendingPathComponent:aFilename];
    }
    
    - (BOOL) fileExistsAtPath:(NSString*)aFilepath {
        NSFileManager *fm = [NSFileManager defaultManager];
        return [fm fileExistsAtPath:aFilepath];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-12
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-21
      • 2019-08-18
      • 1970-01-01
      相关资源
      最近更新 更多