如何在iPhone中读取文件?

时间:2021-07-05 05:28:36

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?

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

Thank you...

谢谢你!

5 个解决方案

#1


3  

Use NSString's stringWithContentsOfFile: method.

使用NSString stringWithContentsOfFile:方法。

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

#2


2  

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..

不管怎样,谢谢大家. .

#3


1  

Hopefully this is what you're after :

希望这就是你想要的:

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

I usually have it looking in the Applications Document directory.

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

#4


1  

Hope this helps...

希望这有助于……

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

hAPPY cODING...

快乐的编码…

#5


0  

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

我认为这是从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 hope this will work for you!!

我希望这对你有用!

#1


3  

Use NSString's stringWithContentsOfFile: method.

使用NSString stringWithContentsOfFile:方法。

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

#2


2  

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..

不管怎样,谢谢大家. .

#3


1  

Hopefully this is what you're after :

希望这就是你想要的:

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

I usually have it looking in the Applications Document directory.

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

#4


1  

Hope this helps...

希望这有助于……

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

hAPPY cODING...

快乐的编码…

#5


0  

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

我认为这是从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 hope this will work for you!!

我希望这对你有用!