Changes

Jump to: navigation, search

Working with Files in Objective-C

32 bytes added, 16:46, 20 November 2009
no edit summary
== Checking if a File Exists ==
The NSFileManager class contains an instance method named ''fileExistsAtPath'' that checks whether a specified file already exists. The method takes as an argument an NSString object containing the path to file and returns a boolean YES or No NO value indicating the presence or otherwise of that file:
<pre>
== Comparing the Contents of Two Files ==
The contents of two files can be compared for equality using the ''contentsEqualAtPath'' method. This methods method takes as arguments the paths to the two files to be compared and returns a boolean YES or NO to indicate whether the file contents match:
<pre>
== Removing a File ==
The ''removeItemAtPath'' method removes the specified file from the file system. The method takes as arguments the pathname of the file to be removed and an optional NSError object. The success of the operation is, as usual, reported in the form of a boolean YES or NO return value:
<pre>
== Creating a Symbolic Link ==
A symbolic link to a particular file may be created using the ''createSymbolicLinkAtPath'' method. This takes arguments the path of the symbolic link, the path to the file to which the link is to refer and an optional NSError object. For example, the following code creates a symbolic link from ''/Users/demo/file1.txt'' that links to the pre-existing file ''/tmp/myfile.txt'':
<pre>
== Reading and Writing Files with NSFileManager ==
The NSFileManager class includes some basic file reading and writing capabilities. these capabilities are somewhat limited when compared to the options provided by the NSFileHandle class, but can be useful for nonetheless.
Firstly, the contents of a file may be read and stored in an NSData object through the use of the ''contentsAtPath'' method:
== Creating an NSFileHandle Object ==
An NSFileHandle object can be created when opening a file for reading, writing or updating (reading and writing). This is achieved using the ''fileHandleForReadingAtPath'', ''fileHandleForWritingAtPath'' and ''fileHandleForUpdatingAtPath'' methods respectively. Having opened a file, it must subsequently be closed when we have finsihed working with it using the ''closeFile'' method. If an attempt to open a file fails, for example because an attempt is made to open a non-existent file for reading, these methods return ''nil''.
For example, the following code excerpt opens a file for reading and writing and then closes it without actually doing anything to the file:
== NSFileHandle File Offsets and Seeking ==
NSFileHandle objects maintain a pointer to the current position in a file. This is referred to as the ''offset''. When a file is first opened the offset is set to 0 (the beginning of the file). This means that any read or write operations we perform using the NSFileHandle methods will take place at offset 0 in the file. To perform operations at different locations in a file (for example to append data to the end of the file) it is first necessary to ''seek'' to the required offset. For example to move the current offset to the end of the file, use the ''seekToEndOfFile'' method. Alternatively, ''seekToFileOffset'' allows you to specifiy the precise location in the file to which the offset is to be positioned. Finally, the current offset may be identified using the ''offsetInFile'' method. in In order to accommodate large files, the offset is stored in the form of an unsigned long long.
The following example opens a file for reading and then performs a number of method calls to move the offset to different positions, outputting the current offset after each move:
== Reading Data from a File ==
Once a file has been opened and assigned a file handle, the contents of that file may be read from the current offset position. The ''readDataOfLength'' methods method reads a specified number of bytes of data from the file starting at the current offset. For example, the following code reads 5 bytes of data from offset 10 in a file. The data read is returned encapsulated in an NSData object:
<pre>
The ''writeData'' method writes the data contained in an NSData object to the file starting at the location of the offset. Note that this does not insert data but rather overwrites any existing data in the file at the corresponding location.
To see this in action we need to begin with a file. Using a text editor, create a file named quickfox.txt, enter the following text before and save it in the /tmp directory:
<tt>The quick brown fox jumped over the lazy dog</tt>
== Truncating a File ==
A file my may be truncated at the specified offset using the ''truncateFileAtOffset'' method. To delete the entire contents of a file, specify an offset of 0 when calling this method:
<pre>

Navigation menu