美文网首页iOS
NSData和NSMutableData

NSData和NSMutableData

作者: 习惯了_就好 | 来源:发表于2019-02-23 11:04 被阅读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(@"写入失败");
            }
    
    

    相关文章

      网友评论

        本文标题:NSData和NSMutableData

        本文链接:https://www.haomeiwen.com/subject/jtguyqtx.html