天天看点

【ios 汇总 - 操作文件常用方法】 (NSFileManager NSFileHandle NSData)在文件末位写入data seekToEndOfFile创建文件夹 createDirectoryAtPath遍历文件夹下所有文件获取md5 再转字符串按长度添加data  获取bundle 文件路径

在文件末位写入data seekToEndOfFile

                NSFileHandle* fileHandle = [NSFileHandlefileHandleForWritingAtPath:cfg.tmpPath];

                if (fileHandle)

                {

                    [fileHandle seekToEndOfFile];

                    if (item.data.length >0)

                    {

                        [fileHandle writeData:item.data];

                        item.writeTotalLen += item.data.length;

                    }

                    if (addData)

                    {

                        [fileHandle writeData:addData];

                        item.writeTotalLen += addData.length;

                    }

                    [fileHandle closeFile];

                    [item.data resetBytesInRange:NSMakeRange(0, item.data.length)];

                    [item.data setLength:0];

                }

创建文件夹 createDirectoryAtPath

    NSFileManager* fileManager = [NSFileManagerdefaultManager];

    BOOL exists = [fileManager fileExistsAtPath:directory isDirectory:&isDirectory];

bSuccess = [fileManager createDirectoryAtPath:directorywithIntermediateDirectories:YESattributes:nilerror:&err];

遍历文件夹下所有文件

- (NSMutableArray *)showAllLogFileName

{

    NSString *logPath = [NSString stringWithFormat:@"%@/log/", [NSString CachePath]];

    NSFileManager *fm = [NSFileManager defaultManager];

    NSArray *logs = [fm contentsOfDirectoryAtPath:logPath error:nil];

    NSMutableArray *logFiles = [NSMutableArray array];

    if([logs count]){

        [logs sortedArrayUsingSelector:@selector(compare:)];

        for (id objin [logs reverseObjectEnumerator]) {

            [logFiles addObject:obj];

        }

    }

    return logFiles;

}

获取md5 再转字符串

CC_MD5(cStr, (CC_LONG)strlen(cStr), result);

NSString* key = [NSStringstringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],result[8], result[9], result[10], result[11],result[12], result[13], result[14], result[15]];

按长度添加data  

(单位:byte)

[thisItem.data appendBytes:result.data.byteslength:nlen];

获取bundle 文件路径

    resourcePath = [[[[ NSBundle  mainBundle] resourcePath]stringByAppendingPathComponent:@"Bird"]

                    stringByAppendingPathComponent:@"index.html"];

继续阅读