NSData和NSMutableData
2019-02-23 本文已影响0人
习惯了_就好
/*
NSData不可变data
*/
//获取data
NSData * data = [NSData dataWithContentsOfFile:@"/Users/song/Desktop/测试/abc.txt"];
NSLog(@"data的长度为:%ld",data.length);
//把加载到data中的文件写入到指定位置
BOOL isWriteSuccess = [data writeToFile:@"/Users/song/Desktop/测试/abcnew.txt" atomically:YES];
if (isWriteSuccess) {
NSLog(@"写入成功");
}else{
NSLog(@"写入失败");
}
/*
NSMutableData可变的data
*/
NSMutableData * mutableData = [[NSMutableData alloc] init];
NSString * str1 = @"www.";
NSData * data1 = [str1 dataUsingEncoding:NSUTF8StringEncoding];
[mutableData appendData:data1];
NSString * str2 = @"baidu.";
NSData * data2 = [str2 dataUsingEncoding:NSUTF8StringEncoding];
[mutableData appendData:data2];
NSString * str3 = @"com";
NSData * data3 = [str3 dataUsingEncoding:NSUTF8StringEncoding];
[mutableData appendData:data3];
BOOL isWriteMuTableSuccess = [mutableData writeToFile:@"/Users/song/Desktop/测试/mutableabc.txt" atomically:YES];
if (isWriteMuTableSuccess) {
NSLog(@"写入成功");
}else{
NSLog(@"写入失败");
}