如何在 iPhone 中读取文件?

发布于 2024-10-06 23:07:08 字数 80 浏览 3 评论 0原文

我正在尝试从我的文档目录中以 NSString 的形式读取 .txt 文件。知道如何将文件读入 NSString 吗?

谢谢...

I am trying to read a .txt file from my documents directory in the form of NSString. Any idea how to read the file into NSString?

Thank you...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

狼性发作 2024-10-13 23:07:08

使用 NSString 的 stringWithContentsOfFile: 方法。

NSString *fileContents = [NSString stringWithContentsOfFile:@"some/file.txt"];

Use NSString's stringWithContentsOfFile: method.

NSString *fileContents = [NSString stringWithContentsOfFile:@"some/file.txt"];
寻梦旅人 2024-10-13 23:07:08
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:@"myfile.txt"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:myPathDocs])
    {
        NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:@"myfile" ofType:@"txt"];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager copyItemAtPath:myPathInfo toPath:myPathDocs error:NULL];
    }       

    //Load from File
NSString *myString = [[NSString alloc] initWithContentsOfFile:myPathDocs encoding:NSUTF8StringEncoding error:NULL];

无论如何,这对我有用

,谢谢大家..

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

    NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:@"myfile.txt"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:myPathDocs])
    {
        NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:@"myfile" ofType:@"txt"];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager copyItemAtPath:myPathInfo toPath:myPathDocs error:NULL];
    }       

    //Load from File
NSString *myString = [[NSString alloc] initWithContentsOfFile:myPathDocs encoding:NSUTF8StringEncoding error:NULL];

This worked for me

Anyway, thank you all..

世俗缘 2024-10-13 23:07:08

希望这就是您想要的:

NSString *myString  = [[NSString alloc] initWithContentsOfFile:@"pathToFile"];

我通常在应用程序文档目录中查找它。

Hopefully this is what you're after :

NSString *myString  = [[NSString alloc] initWithContentsOfFile:@"pathToFile"];

I usually have it looking in the Applications Document directory.

爱你不解释 2024-10-13 23:07:08

希望这会有所帮助...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,  YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileContents=[documentsDirectory stringByAppendingPathComponent:@"file.txt"];

编码快乐...

Hope this helps...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,  YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileContents=[documentsDirectory stringByAppendingPathComponent:@"file.txt"];

hAPPY cODING...

流星番茄 2024-10-13 23:07:08

我认为这是从 DocumentDirectory 读取 .txt 文件的最佳方法。

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"txtFile.txt"];
    NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

我希望这对你有用!

I think this is the best way to read .txt file from DocumentDirectory.

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"txtFile.txt"];
    NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

I hope this will work for you!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文