RSS

Working with Files

15 Sep

This post covers the Objective C platform class used for basic I/O.

In this post we shall learn the following:

  • Create a new file.
  • Read from an existing file.
  • Delete a file.
  • Test for an existing file.
  • Make a copy of a file.

Managing files using NSFileManager: The NSFileManager class enables you to perform many generic file-system operations and insulates an application from the underlying file system.

In Cocoa applications, a file manager object is usually your first interaction with the file system. You use this object to locate, create, copy, and move files and directories. You also use this object to get information about files and directories, such as its size, modification date, and BSD permissions. You can also use a file manager object to change the values of many file and directory attributes.

The NSFileManager class supports both the NSURL and NSString classes as ways to specify the location of a file or directory. The use of the NSURL class is generally preferred for specifying file-system items because they can convert path information to a more efficient representation internally.

If you are moving, copying, linking, or removing files or directories, you can use a delegate in conjunction with a file manager object to manage those operations. The delegate’s role is to affirm the operation and to decide whether to proceed when errors occur.

Common NSFileManager Methods:

Method

Description

– (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error

 

Move the file or directory at the specified path to a new location.
– (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error

 

Copies the item at the specified path to a new location.
– (BOOL)fileExistsAtPath:(NSString *)path

 

Returns a Boolean value that indicates whether a file or directory exists at a specified path.
– (BOOL)isWritableFileAtPath:(NSString *)path

 

Returns a Boolean value that indicates whether the invoking object appears able to write to a specified path.
– (BOOL)isReadableFileAtPath:(NSString *)path

 

Returns a Boolean value that indicates whether the invoking object appears able to read to a specified path.
– (BOOL)contentsEqualAtPath:(NSString *)path1 andPath:(NSString *)path2

 

Returns a Boolean value that indicates whether the file or directories in specified paths have the same conetents.

 

Creating a new File:  Given below is the snap shot of the code that will help you to create a new file along with the code explanation.

Code Explanation: The above code will create a file in the document folder with the name MyFile.txt and with the content “Welcome to Quagnitia Systems have a great Day.” The NSString class has a function called as writeToFile which accepts the location as a parameter for where you want to create the file and it returns you a boolean value so with the help of the finalResult variable I am storing the result of that method and then applying a for loop to check whether the file is create in the specified location or not.

Check for existing file: Given below is the snap shot to check whether file is present or not

 

Code Explanation:  With the help of the NSFileManager instance method named fileExistsAtPath you can check for the file existence in a particular location, in the above code I am checking for file existence in the documents folder for a file named MyFile.txt.

Read from an existing file:  Given below is the snap shot of the code to read the data from an existing file.

Code Explanation:  There is no such function in the NSFileManager to read data from the file so I came up with a very basic approach that is followed by all the developers when they want to read data from a particular file. So in the above code what I am doing is first I am checking for the file existence which I want to read and then if the file exists then I am collecting the information about the file inside the NSData variable called fileData and then with the help of the string function I am reading the data in the string format and printing the result into the console.

Delete a file: Given below is the snap shot of the code to delete a file from a particular location

Code Explanation: In the above code I am using the standard function of the NSFileManager class to remove the file from a particular location.

Create a copy of the file: Given below is the snap shot of the code for creating the copy of a file.

Code Explanation: In the above code I am creating the copy of the file named MyFile.txt with a new name MyNewFile.txt. First I am checking for the file existence in the specified location and then with the help of the instance method of the NSFileManager class i.e copyItemAtPath:toPath I am copying the files now this function returns a Boolean result indicating whether the file is copied successfully or not.

I hope that you have understood how to work with files and in case if you are having any queries then feel free to ask your queries via comments or you can mail me at iphone.learning@quagnitia.com, you can find the PDF document of this post here, until then Happy iCoding and have a great Day.

Advertisements
 
Leave a comment

Posted by on September 15, 2011 in Beginners

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: