1 直接通過將data加入到目標文件的方法
[self.mutableData writeToFile:@"/Users/Apple/Desktop/text.hm" atomically:YES];
2 通過文件輸出流的方式
@property (nonatomic, strong) NSOutputStream *stream;
首先根據給出的路徑初始化輸出流
//創建流
self.stream = [NSOutputStream outputStreamToFileAtPath:@"/Users/Apple/Desktop/text.hm" append:YES];
//打開流
[self.stream open];
//保存數據
[self.stream write:data.bytes maxLength:data.length];
數據寫完了,關閉輸出流
//關閉流
[self.stream close];
第三種方法 通過NSFileHandle的方式,文件句柄
NSString *filePath = @"/Users/Apple/Desktop/111111.hm";
//如果文件不存在,返回的是nil
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
if (fileHandle == nil) {
//如果文件不存在,會自動創建
[data writeToFile:filePath atomically:YES];
}else {
//讓offset指向文件的末尾
[fileHandle seekToEndOfFile];
[fileHandle writeData:data];
//關閉文件
[fileHandle closeFile];
}