Objective-C 学习笔记 06 - 通过NSString和NSData将数据写入文件

新建Command Line Tool工程Stringz,通过NSString的类方法创建对象,然后将新创建的对象写入文件

#import 

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        NSMutableString *str = [[NSMutableString alloc] init];
        for (int i = 0; i < 10; i++)
        {
            [str appendString:@"Aaron is cool!\n"];
        }
        
        [str writeToFile:@"/tmp/cool.txt" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
        NSLog(@"done writing /tmp/cool.txt");
        
    }
    return 0;
}
执行程序,将在/tmp目录下生成cool.txt文件。

Objective-C 学习笔记 06 - 通过NSString和NSData将数据写入文件_第1张图片

NSError

将数据写入文件时,可能遇到错误,使用NSError可以接受错误信息

#import 

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        NSMutableString *str = [[NSMutableString alloc] init];
        for (int i = 0; i < 10; i++)
        {
            [str appendString:@"Aaron is cool!\n"];
        }
        
        NSError *error = nil;
        BOOL success = [str writeToFile:@"/tmp1/cool.txt" atomically:YES encoding:NSUTF8StringEncoding error:&error];
        
        if (success)
        {
            NSLog(@"done writing /tmp/cool.txt");
        }
        else
        {
            NSLog(@"writing /tmp/cool.txt failed: %@", [error localizedDescription]);
        }
        
    }
    return 0;
}
执行程序将看到错误信息:

writing /tmp/cool.txt failed: The folder “cool.txt” doesn’t exist.
通过NSString读取文件

        NSError *error = nil;
        NSString *str = [[NSString alloc] initWithContentsOfFile:@"/etc/resolv.conf" encoding:NSASCIIStringEncoding error:&error];
        
        if (!str)
        {
            NSLog(@"read failed: %@", [error localizedDescription]);
        }
        else
        {
            NSLog(@"resolv.conf looks like this %@", str);
        }
程序执行结果如下:

Objective-C 学习笔记 06 - 通过NSString和NSData将数据写入文件_第2张图片

NSData

NSData对象代表内存中的某块缓冲区,可以保存相应自己的的数据。例如,从网上下载的数据,然后将下载的数据写入文件。

        NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/img/bdlogo.png"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        NSError *error = nil;
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:&error];
        
        if (!data)
        {
            NSLog(@"fetch failed: %@", [error localizedDescription]);
            return 1;
        }
        
        NSLog(@"The file is %lu bytes", [data length]);
        
        BOOL written = [data writeToFile:@"/tmp/baidu.png" options:0 error:&error];
        
        if (!written)
        {
            NSLog(@"write failed: %@", [error localizedDescription]);
        }
        
        NSLog(@"Success!");
程序执行结果如下:

The file is 5331 bytes
Success!
baidu.png将被下载到tmp目录中:


从文件读取数据并存入NSData对象

        NSData *readData = [NSData dataWithContentsOfFile:@"/tmp/baidu.png"];
        NSLog(@"The file read from disk has %lu bytes", [readData length]);
代码执行结果:

The file read from disk has 5331 bytes






你可能感兴趣的:(Objective-C)