复制沙盒文件

作者: Mr_fei | 来源:发表于2017-06-08 18:07 被阅读0次

    /**
    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 = 10
    1024; //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];
    }

    相关文章

      网友评论

        本文标题:复制沙盒文件

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