10.18 - NSFileHandle文件内容处理类的使用

//OC文件内容处理练习,用到了NSFileHandle类,用来读取文件内容,设置文件读写指针等。

- (void)nsFileHandleTest

{

    NSString *documentPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

    NSLog(@"%@", documentPath);

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *testPath = [documentPath stringByAppendingPathComponent:@"creatFile.txt"];

   

    //1.NSFileHandle不能创建文件,因此在打开一个文件时要确保文件存在,不存在需要先用NSFileManager对象来创建一个文件。

    if(![fileManager fileExistsAtPath:testPath]){

        if(![fileManager createFileAtPath:testPath contents:[@"hello world!" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil]){

            NSLog(@"file create failed !");

            return ;

        }

    }

    //2.获取只写文件处理对象。不能用该对象来读数据。

    NSFileHandle *writeHandle = [NSFileHandle fileHandleForWritingAtPath:testPath];

    //3.获取当前文件写指针。

    NSLog(@"write offset%llu", writeHandle.offsetInFile);

    //4.向文件写数据, 从当前文件写指针指示位置开始写入数据,写入一个数据就覆盖文件中原有的一个数据,文件写指针随之移动。

    NSData *writeData = [@"Hello" dataUsingEncoding:NSUTF8StringEncoding];

    [writeHandle writeData:writeData]; //将文件原有内容“hello world!” 替换成了“Hello world!”

    NSLog(@"write offset: %llu", writeHandle.offsetInFile);

   

    //5.设置文件写指针位置.

    [writeHandle seekToFileOffset:6]; //该方法设置基于文件开始处的偏移量值。

    //[writeHandle seekToEndOfFile]; //该方法设置文件写指针指向文件末尾。

    NSLog(@"write offset: %llu", writeHandle.offsetInFile);

   

    //6.获取只读文件处理对象, 不能用该对象来写数据。

    NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:testPath];

    //获取文件读指针,默认初始化为0,即文件开始位置。也从这里可以看出文件的读指针和写指针是不在一起的,有自己单独的位置。

    NSLog(@"read offset: %llu", readHandle.offsetInFile);

   

    //从文件当前读指针指示位置读取数据,可以指定读取多少长度的数据,若到达文件尾,就停止读取。

    NSData *readData = [readHandle readDataOfLength:15];

    NSLog(@"readData: \n%@", [[NSString alloc] initWithData:readData encoding:NSUTF8StringEncoding]);

    NSLog(@"read offset: %llu", readHandle.offsetInFile);

   

    //7.获取更新文件处理对象,该对象对文件可读可写

    NSFileHandle *updateHandle = [NSFileHandle fileHandleForUpdatingAtPath:testPath];

    NSLog(@"update offset:%llu", updateHandle.offsetInFile);

   

    NSData *update = [updateHandle readDataToEndOfFile];

    NSLog(@"updata: \n%@", [[NSString alloc] initWithData:update encoding:NSUTF8StringEncoding]);

    NSLog(@"update offset:%llu", updateHandle.offsetInFile);

   

    [updateHandle seekToFileOffset:6];

    [updateHandle writeData:writeData];

    NSLog(@"update offset:%llu", updateHandle.offsetInFile);

   

    [readHandle seekToFileOffset:0];

    readData = [readHandle readDataToEndOfFile];

    NSLog(@"read:\n%@", [[NSString alloc] initWithData:readData encoding:NSUTF8StringEncoding]);

   

    //8.获取文件描述符

    NSLog(@"%d", readHandle.fileDescriptor);

    //9.用一个文件描述符来初始化一个文件处理对象。如果参数是一个直接的整数,则返回的文件处理对象就是该文件的一个只读处理对象。如果参数是一个文件处理对象fileHandlefileDescriptor的返回值,那么返回的文件处理对象就相当于该fileHandle。该方法的另一个参数表示是否在返回的文件处理对象销毁时关闭该文件描述符对应的文件。

    NSFileHandle *fileHandle = [[NSFileHandle alloc] initWithFileDescriptor:readHandle.fileDescriptor closeOnDealloc:YES];

    NSLog(@"<%p>,<%p>", readHandle, fileHandle);

   

    //[fileHandle writeData:[@"123456" dataUsingEncoding:NSUTF8StringEncoding]];

    NSLog(@"%llu", fileHandle.offsetInFile);

    [fileHandle seekToFileOffset:0];

   

    NSData *fileData;

    //fileData = [fileHandle readDataToEndOfFile];

    if(fileData){

        NSLog(@"fileData:\n%@", [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding]);

    }

    //10.关闭文件,和C语言文件操作一样,不再访问一个文件时,要将打开的文件关闭。

    [readHandle closeFile];

    [writeHandle closeFile];

    [updateHandle closeFile];

}

posted @ 2014-10-19 23:00  _蚕豆_  阅读(534)  评论(0)    收藏  举报