18

How can I read a .txt file inside documents directory? Can you show me code?

2 Answers 2

37
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]; 
Sign up to request clarification or add additional context in comments.

1 Comment

As simple and easy as the snippet is, I would recommend adding some context or code comments
1

You can also use NSFileHandle for writing data in file and save to document directory: You can follow: NSFileHandle

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.