简单实例NSFileHandle的文件读写

新建helo文件,初始内容123456789,经过多次修改,输出相应内容。

//
//  main.m

/* 演示文件句柄的使用:读文件和写文件操作 */

#import <Foundation/Foundation.h>

#define PATH @"/Users/apple/Documents/ios_dev/test_case/TestFileHandle"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        /* 新建文件和初始化文本内容 */
        NSString *path = [NSString stringWithFormat:@"%@/hello.txt", PATH];
        NSData *content = [@"123456789" dataUsingEncoding:NSUTF8StringEncoding];
        NSFileManager *fm = [NSFileManager defaultManager];
        BOOL ret = [fm createFileAtPath:path contents:content attributes:nil];
        if (ret == 0)
        {
            NSLog(@"Create file fail.");
            return 0;
        }
        
        /*
         The 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.
 
         fileHandleForReadingAtPath:Returns a file handle initialized for reading the file, device,
         or named socket at the specified path.
         */
        NSString *str1 = [NSString stringWithFormat:@"%@/hello.txt",PATH];
        NSFileHandle *rfh = [NSFileHandle fileHandleForReadingAtPath:str1];
        
        /* readDataOfLength:Synchronously reads data up to the specified number of bytes. */
        NSData *da1 = [rfh readDataOfLength:2];  /* 句柄位置变动 */
        
        /* readDataToEndOfFile:Synchronously reads the available data up to the end of file or maximum
         number of bytes. */
        NSData *da2 = [rfh readDataToEndOfFile]; /* 从上次句柄处开始读取 */
        
        NSString *str2 = [[NSString alloc] initWithData:da1 encoding:NSUTF8StringEncoding];
        NSLog(@"first read contents:%@", str2);
        NSString *str3 = [[NSString alloc] initWithData:da2 encoding:NSUTF8StringEncoding];
        NSLog(@"seconde read contents:%@", str3);
        
        /* fileHandleForWritingAtPath:Returns a file handle initialized for writing to the file, 
         device, or named socket at the specified path. */
        NSFileHandle *wfh = [NSFileHandle fileHandleForWritingAtPath:path];
        NSData *da3 = [@"BEST&" dataUsingEncoding:NSUTF8StringEncoding];
        
        /* writeData:If the receiver is a file, writing takes place at the file pointer’s current position.
         After it writes the data, the method advances the file pointer by the number of bytes written.*/
        [wfh writeData:da3];
        
        /* seekToEndOfFile:Moves the file pointer to the specified offset within the file represented
         by the receiver. */
        [rfh seekToFileOffset:0];
        
        NSData *da4 = [rfh readDataToEndOfFile];
        NSString *str4 = [[NSString alloc] initWithData:da4 encoding:NSUTF8StringEncoding];
        NSLog(@"read contents after first write:%@", str4);
        
        /* 注意此时wfh的句柄不在文件首位置,而在执行了写入da3后的那个位置,因此不是从文件首或文件末尾写入新的字符串 */
        NSData *da5 = [@"##" dataUsingEncoding:NSUTF8StringEncoding];
        [wfh writeData:da5];
        
        [rfh seekToFileOffset:0];
        da4 = [rfh readDataToEndOfFile];
        str4 = [[NSString alloc] initWithData:da4 encoding:NSUTF8StringEncoding];
        NSLog(@"read contents after second write:%@", str4);
        
        /* 从文件末尾处新增内容 */
        [wfh seekToEndOfFile];
        da5 = [@"hero" dataUsingEncoding:NSUTF8StringEncoding];
        [wfh writeData:da5];
        
        [rfh seekToFileOffset:0];
        da4 = [rfh readDataToEndOfFile];
        str4 = [[NSString alloc] initWithData:da4 encoding:NSUTF8StringEncoding];
        NSLog(@"read contents after third write:%@", str4);
    }
    return 0;
}

输出结果:

2015-12-12 18:41:46.570 TestFileHandle[836:39034] first read contents:12
2015-12-12 18:41:46.571 TestFileHandle[836:39034] seconde read contents:3456789
2015-12-12 18:41:46.571 TestFileHandle[836:39034] read contents after first write:BEST&6789
2015-12-12 18:41:46.572 TestFileHandle[836:39034] read contents after second write:BEST&##89
2015-12-12 18:41:46.572 TestFileHandle[836:39034] read contents after third write:BEST&##89hero


你可能感兴趣的:(简单实例NSFileHandle的文件读写)