ios file handling

本文详细介绍了苹果提供的两个基本文件处理类NSFileManager和NSFileHandle的功能,包括检查文件是否存在、比较文件内容、文件读写操作等,并通过示例代码展示了具体用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值