1. class
Apple provides us two basic classes for handling file operations. They are:
NSFileManager, NSFileHandle
2. functions
Here I want to describe the functions of them group by the class.
2.1 NSFileManager
(1)Check if a File exists at a path
fileExistsAtPath
NSfileManager *fileManager = [NSFileManager defaultManager];
NSArray *documentDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentPath = [documentDirectory objectAtIndex:0];
if ([fileManager fileExistAtPath:documentPath]) {
//do something
}
(2) Comparing two file contents
contentsEqualAtPath:(NSString )path1 andPath:(NSString )path2
As described by apple api, it returns YES if file or directory specified in path1 has the same contents as that specified in path2, otherwise NO.
(3) Check if Writable, Readable, and Executable
***isWritableFileAtPath:
isReadableFileAtPath:
isExecutableFileAtPath:*
(4) Move File
moveItemAtPath:
(5) Copy File
copyItemAtPath:(NSString )srcPath toPath:(NSString )dstPath error:(NSError )error**
(6) Remove File
removeItemAtPath:(NSString )path error:(NSError )error***
(7) Read File
- (nullable) contentsAtPath:(NSString )path*
(8) Write File
createFileAtPath:(NSString )path contents:(nullable NSData )data attributes:(nullable NSDictionary
2.2 NSFileHandle
As described by apple document: NSFileHandle class is an object-oriented wrapper for a file descriptor. You use file handle objects to access data associated with files, sockets, pipes, and devices. For files, you can read, write, and seek within the file. For sockets, pipes, and devices, you can use a file handle object to monitor the device and process data asynchronously.
(1)Read File
1.1 readInBackgroundAndNotifyForModes:
Reads from the file or communications channel in the background and posts a notification when finished.
1.2 readInBackgroundAndNotify
Reads from the file or communications channel in the background and posts a notification when finished.
This method performs an asynchronous availableData operation on a file or communications channel and posts an NSFileHandleReadCompletionNotification notification on the current thread when that operation is complete.
1.3 readToEndOfFileInBackgroundAndNotifyForModes
Comparing to the function of readInBackgroundAndNotifyForModes, this method reads to the end of file from the file or communications channel in the background and posts a notification when finished.
1.4 readToEndOfFileInBackgroundAndNotify
This function has similar meaning as the upper one.
1.5 - (NSData *_Nonnull)readDataOfLength:(NSUInteger) length
Synchronously reads data up to the specified number of bytes. length stands for the number of bytes to read from the receiver.
(2)Write Data
writeData:(NSData *_Nonnull)data
this function synchronously writes the specified data to the receiver.
Important:
this function may fail and can’t be catched by using @try @catch block when device has no enough free space to store the data to be writeen to.
So here we need check the free space of device such as disk.
(3) Get Free space size of disk
- (uint64_t) freeDiskSpace {
uint64_t freeSpaceSize = 0;
__autoreleasing NSError *error = nil;
NSDictionary *attributeDictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
if (attributeDictionary) {
NSNumber *freeFileSizeInBytes = [attributeDictionary objectForKey:NSFileSystemFreeSize];
freeSpaceSize = [freeFileSizeInBytes unsignedLongLongValue];
}
return freeSpaceSize;
}