Categories
objective-c

objective-c 流编程

文件输入流

NSMutableData *data = [NSMutableData new];
        NSInputStream *inputStream = [NSInputStream inputStreamWithFileAtPath:@"/Users/dengfang/111.txt"];
        [inputStream open];
         NSLog(@"数据读取中...");
        while(1){
            uint8_t buf[1024];
            NSInteger readLength = [inputStream read:buf maxLength:1024];
            if (readLength > 0) {
                [data appendBytes:buf length:readLength];
            } else {
                NSLog(@"未读取到数据");
                break;
            }
        }
        NSLog(@"%@" ,[data base64EncodedStringWithOptions:nil]);
    }

文件输出流

NSOutputStream *ouputStream = [NSOutputStream outputStreamToFileAtPath:@"/Users/dengfang/222.txt" append:false];
[ouputStream open];
[ouputStream write:data.bytes maxLength:data.length];
[ouputStream close];

Leave a Reply