Reading and Writing Text Files in iPhone OS 3.0

iStock_000007449744XSmall.jpgReading and writing text files are one of the most basic things you need to do in iPhone development. In iPhone OS 3.0 things have changed a little bit here and there with some methods being deprecated.

I just wrote two example methods that you can use to see how you can turn a NSString into a text file and reverse the process.

Write the Contents of your NSString to the Filesystem

All I needed to do is create a NSString that has five lines in it and then I used the writeToFile method to save the contents. This version of writeToFile is like others that I have wrote about but it has a few more parameters.

//Method writes a string to a text file

-(void) writeToTextFile{

     	//get the documents directory:

     	NSArray *paths = NSSearchPathForDirectoriesInDomains

          	(NSDocumentDirectory, NSUserDomainMask, YES);

     	NSString *documentsDirectory = [paths objectAtIndex:0];

     	//make a file name to write the data to using the documents directory:

     	NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt", 

                          							  documentsDirectory];

     	//create content - four lines of text

     	NSString *content = @"One\nTwo\nThree\nFour\nFive";

     	//save content to the documents directory

     	[content writeToFile:fileName

               			 atomically:NO

                 			   encoding:NSStringEncodingConversionAllowLossy 

                    				  error:nil];

}

 

Read Something from the Filesystem and Display It

Of course, once you write something to the filesystem you will want to use it again in the future. Here is a method I wrote as an example of this operation.

//Method retrieves content from documents directory and

//displays it in an alert

-(void) displayContent{

     	//get the documents directory:

     	NSArray *paths = NSSearchPathForDirectoriesInDomains

                     	(NSDocumentDirectory, NSUserDomainMask, YES);

     	NSString *documentsDirectory = [paths objectAtIndex:0];

     	//make a file name to write the data to using the documents directory:

     	NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt", 

                          							  documentsDirectory];

     	NSString *content = [[NSString alloc] initWithContentsOfFile:fileName

                                                      usedEncoding:nil

                                                             error:nil];

     	//use simple alert from my library (see previous post for details)

     	[ASFunctions alert:content];

     	[content release];

}

 

Discussion

That is it – you can do similar things with content like images and almost anything else with the NSData class. There is also a method in NSString (check out the header files in XCode) which advertises similar functionality for URL so it would look like you can do the same thing with your webserver. I had no luck with this. It was trivial to read a text file from my server, but I could not write files using the methods there; I know this can be done, just not with this very simple approaches.

 

One Response to Reading and Writing Text Files in iPhone OS 3.0

  1. Reading and Writing Text Files June 24, 2009 at 2:05 pm #

    [...] in iPhone OS 3.0 June 24, 2009, 2:06 pm Filed under: Uncategorized (My Original Blog Post: http://howtomakeiphoneapps.com/2009/06/reading-and-writing-text-files-in-iphone-os-3-0/) Reading and writing text files are one of the most basic things you need to do in iPhone [...]

Leave a Reply

Affiliate Policy Disclosure

Switch to our mobile site