NSFileHandle处理文件IO

NSFileHandle提供了处理文件IO相关方法,通常来说,使用NSFileHandle的基本步骤就是:

1、创建一个NSFileHandle,然后通过其打开指定的文件

2、对打开的文件进行IO操作

3、关闭文件

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //打开一份文件准备读取
    NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:@"/Users/xxt/Desktop/NSFileHandle/NSFileHandle/ViewController.m"];
    NSData *data;

    //读取NSFileHandle中的256字节
    while ([(data = [fh readDataOfLength:512]) length]) {
        NSLog(@"%ld",[data length]);
        //直接将NSData的数据用UTF-8的格式转换字符串
        NSString *content = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"---------输出读取的512字节的内容--------");
        NSLog(@"%@",content);
    }
    [fh closeFile]; //关闭文件
    
    NSFileHandle *fh2 = [NSFileHandle fileHandleForWritingAtPath:@"sbc.txt"];//打开一份文件准备写入
    
    if (!fh2) {
        //创建一个NSFileManager对象
        NSFileManager *fm = [NSFileManager defaultManager];
        //创建一份空的文件
        [fm createFileAtPath:@"sbc.txt" contents:nil attributes:nil];
        fh2 = [NSFileHandle fileHandleForWritingAtPath:@"sbc.txt"];
        
    }
    NSString *mybooks = @"ios课本";
    [fh2 writeData:[mybooks dataUsingEncoding:NSUTF8StringEncoding]];//将指定内容写入底层文件
    [fh2 closeFile];
}


@end

输出为:

2016-02-03 21:23:42.058 NSFileHandle[682:15660] 512
2016-02-03 21:23:42.059 NSFileHandle[682:15660] ---------输出读取的512字节的内容--------
2016-02-03 21:23:42.059 NSFileHandle[682:15660] 

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //打开一份文件准备读取
    NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:@"/Users/xxt/Desktop/NSFileHandle/NSFileHandle/ViewController.m"];
    NSData *data;

    //读取NSFileHandle中的256字节
    while ([(data = [fh readDataOfLength:512]) length]) {
      
2016-02-03 21:23:42.060 NSFileHandle[682:15660] 512
2016-02-03 21:23:42.060 NSFileHandle[682:15660] ---------输出读取的512字节的内容--------
2016-02-03 21:23:42.060 NSFileHandle[682:15660]   NSLog(@"%ld",[data length]);
        //直接将NSData的数据用UTF-8的格式转换字符串
        NSString *content = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"---------输出读取的512字节的内容--------");
        NSLog(@"%@",content);
    }
    [fh closeFile]; //关闭文件
    
    NSFileHandle *fh2 = [NSFileHandle fileHandleForWritingAtPath:@"sbc.txt"];//打开一份文件准备写入
    
    if (!fh2) {
        //创建一个NSFileManager对象
2016-02-03 21:23:42.069 NSFileHandle[682:15660] 421
2016-02-03 21:23:42.070 NSFileHandle[682:15660] ---------输出读取的512字节的内容--------
2016-02-03 21:23:42.070 NSFileHandle[682:15660]         NSFileManager *fm = [NSFileManager defaultManager];
        //创建一份空的文件
        [fm createFileAtPath:@"sbc.txt" contents:nil attributes:nil];
        fh2 = [NSFileHandle fileHandleForWritingAtPath:@"sbc.txt"];
        
    }
    NSString *mybooks = @"ios课本";
    [fh2 writeData:[mybooks dataUsingEncoding:NSUTF8StringEncoding]];//将指定内容写入底层文件
    [fh2 closeFile];
}


@end


你可能感兴趣的:(ios,NSFileHandle,NSFileManager)