/**
localfilePath:文件所在的位置,
dirName:复制文件所在的位置
这里的文件可以包括音频,图片,文档等类型
*/
- (void)copyFileFromLocalToLocal:(NSString *)localfilePath otherlocalfilePath:(NSString *)dirName
{
NSInputStream *inputStream = [[NSInputStream alloc] initWithFileAtPath: localfilePath];
NSOutputStream outputStream;
[[NSFileManager defaultManager] createFileAtPath:dirName contents:nil attributes:nil];
outputStream = [NSOutputStream outputStreamToFileAtPath:dirName append:NO];
[outputStream open];
[inputStream open];
NSInteger maxLength = 101024; //buffer size
uint8_t readBuffer [maxLength];
uint32_t readlength;
BOOL endOfStreamReached = NO;
// NOTE: this tight loop will block until stream ends
while (! endOfStreamReached) {
NSInteger bytesRead = [inputStream read: readBuffer maxLength:maxLength];
readlength = bytesRead;
if (bytesRead == 0) {//文件读取到最后
endOfStreamReached = YES;
} else if (bytesRead == -1) {//文件读取错误
endOfStreamReached = YES;
} else {
[outputStream write:readBuffer maxLength:readlength];
}
}
[inputStream close];
[outputStream close];
}
网友评论