NSFileHandle的使用

来源:互联网 发布:咖啡机 知乎 编辑:程序博客网 时间:2024/05/15 23:48
 NSFileHandle通过了处理文件 IO 的相关方法,通常来说,使用NSFileHandle的基本步骤如下:
 
    1)创建一个NSFileHandle,该NSFileHandle将会打开指定的文件.
    2)对打开的文件执行 IO 操作.
    3)关闭文件.

NSFileHandle常用属性和方法:

//返回该文件所有可用的数据.@property (readonly, copy) NSData *availableData;//读取该文件中包含的所有数据.- (NSData *)readDataToEndOfFile;//读取该文件中length字节的数据./* 注意: 该方法每次返回的 length 字节的数据包装成NSData对象,如果已经到了文件的结尾处,程序将会返回一个不包含任何数据的 NSData对象,因此,程序可通过该方法返回的NSData的 length 方法是否返回0来判断是否已经读到文件结尾处. */- (NSData *)readDataOfLength:(NSUInteger)length;//将制定的数据写入到该文件中- (void)writeData:(NSData *)data;//获取该NSFileHandle中文件指针的位置.文件指针记录了当前读写文件的位置.也就是说,文件指针所在的位置,//也就是接下来NSFileHandle要读写文件的位置.@property (readonly) unsigned long long offsetInFile;//将该NSFileHandle中文件指针移动到文件的结尾处.- (unsigned long long)seekToEndOfFile;//将该NSFileHandle中文件指针移动到指定的位置.- (void)seekToFileOffset:(unsigned long long)offset;//将文件长度"截断"为offset字节.如果该offset参数小于文件本身的大小,多余的部分被截断.如果该offset参数大于文件//本身大小,文件末尾将会被填充空数据.- (void)truncateFileAtOffset:(unsigned long long)offset;//关闭底层文件.- (void)closeFile;//打开标准输入设备(通常是键盘)对应的NSFileHandle.+ (NSFileHandle *)fileHandleWithStandardInput;//打开标准输出设备(通常是键盘)对应的NSFileHandle.+ (NSFileHandle *)fileHandleWithStandardOutput;//打开标准错误输出设备对应的NSFileHandle.+ (NSFileHandle *)fileHandleWithStandardError;//打开空设备对应的NSFileHandle.+ (NSFileHandle *)fileHandleWithNullDevice;//根据指定的路径打开一份准备读取文件内容的文件,并返回该文件对应的NSFileHandle.+ (nullable instancetype)fileHandleForReadingAtPath:(NSString *)path;//根据指定的路径打开一份准备写入文件内容的文件,并返回该文件对应的NSFileHandle.+ (nullable instancetype)fileHandleForWritingAtPath:(NSString *)path;//根据指定的路径打开一份既可以读取又可以准备写入文件内容的文件,并返回该文件对应的NSFileHandle.+ (nullable instancetype)fileHandleForUpdatingAtPath:(NSString *)path;//根据指定URL的打开一份准备读取文件内容的文件,并返回该文件对应的NSFileHandle.+ (nullable instancetype)fileHandleForReadingFromURL:(NSURL *)url error:(NSError **)error;//根据指定的URL打开一份准备写入文件内容的文件,并返回该文件对应的NSFileHandle.+ (nullable instancetype)fileHandleForWritingToURL:(NSURL *)url error:(NSError **)error;//根据指定的URL打开一份既可以读取又可以准备写入文件内容的文件,并返回该文件对应的NSFileHandle.+ (nullable instancetype)fileHandleForUpdatingURL:(NSURL *)url error:(NSError **)error;

注意:

      NSFileHandle并没有提供创建文件的功能,必须使用  NSFileManager来创建对象,当文件不存在时,如果使用 fileHandleForWritingAtPath和fileHandleForUpdatingAtPath来打开文件,这两个方法会返回nil.如果文件存在,这两个方法打开文件后,文件记录指针都位于文件开始处(及offset为0).

     除此之外,NSFileHandle还提供了一系列用于后台操作(及异步)读取和写入文件的方法,这些方法大多数以InBackgroundAndNotify结尾,这些方法读写内容时将不会阻塞前台线程,程序将在后台完成对文件的读写.当读写完成后,这些方法会像前台发送一个通知.

下面一起使用一下NSFileHandle读写文件内容:

/** *下面的代码用于打开已经创建好的文件testfile文件,读取它的内容,并将其复制到名为testout的文件中。 */-(void)testTwo{        NSFileHandle *inFile, *outFile;    NSData *buffer;        //打开testfile.txt文件用于读取操作    NSString *path=[[NSBundle mainBundle]pathForResource:@"testfile" ofType:@".txt"];    inFile = [NSFileHandle fileHandleForReadingAtPath:path];        if(inFile == nil)    {        NSLog(@"Open of testfile.txt for reading failed!");        return ;    }        //创建一个文件用于写数据(第一次是必要的)    NSString *homePath  = NSHomeDirectory();    NSString *outPath = [homePath stringByAppendingPathComponent:@"outfile.text"];        [[NSFileManager defaultManager] createFileAtPath:outPath contents:nil attributes:nil] ;        //打开testout.txt文件用于写入操作    outFile = [NSFileHandle fileHandleForWritingAtPath:outPath];        if(outFile == nil)    {        NSLog(@"Open of outfile.text for writing failed!");        return ;    }        //清空数据    [outFile truncateFileAtOffset:0];        //从inFile中读取数据,并将其写入到outFile中    buffer = [inFile readDataToEndOfFile];    [outFile writeData:buffer];        //关闭两个文件    [inFile closeFile];    [outFile closeFile];        //验证文件的内容是否写入    NSLog(@"%@",[NSString stringWithContentsOfFile:outPath encoding:NSUTF8StringEncoding error:nil]);        /*     打印输出如下:(以下内容是本地testfile.txt文件中的内容!)     1:One is always on a strange road, watching strange scenery and listening to strange music. Then one day, you will find that the things you try hard to forget are already gone.     一个人总要走陌生的路,看陌生的风景,听陌生的歌,然后在某个不经意的瞬间,你会发现,原本是费尽心机想要忘记的事情真的就那么忘记了。          2.Happiness is not about being immortal nor having food or rights in one's hand. It’s about having each tiny wish come true, or having something to eat when you are hungry or having someone's love when you need love.     幸福,不是长生不老,不是大鱼大肉,不是权倾朝野。幸福是每一个微小的生活愿望达成。当你想吃的时候有得吃,想被爱的时候有人来爱你。            3.Love is a lamp, while friendship is the shadow. When the lamp is off,you will find the shadow everywhere. Friend is who can give you strength at last.     爱情是灯,友情是影子,当灯灭了,你会发现你的周围都是影子。朋友,是在最后可以给你力量的人。          4.I love you not for who you are, but for who I am before you.     我爱你不是因为你是谁,而是我在你面前可以是谁。          5.Love makes man grow up or sink down.     爱情,要么让人成熟,要么让人堕落。          6.If you can hold something up and put it down, it is called weight-lifting; if you can hold something up but can never put it down,it's called burden-bearing. Pitifully, most of people are bearing heavy burdens when they are in love.     举得起放得下的叫举重,举得起放不下的叫负重。可惜,大多数人的爱情,都是负重的。          */}





0 0
原创粉丝点击