美文网首页
iOS开发生成txt文件

iOS开发生成txt文件

作者: 山野武夫 | 来源:发表于2022-04-19 17:41 被阅读0次

一开始我用的方法一写,但是后来发现在高系统上报错(大概报错内容:NSCocoaErrorDomain:257)。

后来解决了,做个记录。

1、在低于iOS13的系统中。用创建文件夹的形式可以如下:
+ (NSString *)tmpLogPath

{

    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];

    NSString *dirPath = [docPath stringByAppendingPathComponent:@"mylog"];

    NSString *filePath = [dirPath stringByAppendingPathComponent:@"tmpLog.txt"];

    return filePath;

}

+ (void)writeTmpLog:(NSString *)aMsg

{

    NSString *filePath = [[self class] tmpLogPath];

    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

        BOOL isDir = NO;

        BOOL hasDir = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDir];

        if (!hasDir || !isDir) {

            [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:NO attributes:nil error:nil];

        }

    }

    NSError *error;

    NSString *content =[NSString stringWithContentsOfFile:filePath

                                                encoding:NSUTF8StringEncoding

                                                    error:&error];

    NSString *newContent = [NSString stringWithFormat:@"%@\n%@",content,aMsg];

    [newContent writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

}

+ (void)clearTmpLog

{

    NSString *filePath = [[self class] tmpLogPath];

    [@"" writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

}

2、但是在iOS13上,不能这样写。系统会默认创建以为***.txt的文件夹,在写入的时候就出问题了。

会报错:NSCocoaErrorDomain:257 就是
NSFileReadNoPermissionError = 257,/ /读取错误(权限问题)

所以可以这样写:

+ (NSString *)tmpLogPath {

    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];

    NSString *filePath = [documentPath stringByAppendingPathComponent:@"mylog/tmpLog.txt"];

    returnfilePath;

}

+ (void)writeTmpLog:(NSString*)aMsg {

    NSString*fieldPath = [[self class]tmpLogPath];

    NSLog(@"当前文件大小:%llu",[self fileSizeWithPath:fieldPath]);

    NSFileManager *manager = [NSFileManager defaultManager];

    if(![managerfileExistsAtPath:fieldPath]){

        NSError*error;

        [aMsgwriteToFile:fieldPath atomically:YES encoding:NSUTF8StringEncoding error:&error];

        if(error) {

            NSLog(@"写入失败:%@\n",[error localizedDescription]);

        }

    }else{

        NSError*error;

        NSError*writeError;

        NSString *content =[NSString stringWithContentsOfFile:fieldPath

                                                     encoding:NSUTF8StringEncoding

                                                        error:&error];

        if(error) {

            NSLog(@"读取失败:%@\n",[error localizedDescription]);

        }

        NSString*newContent = [NSString stringWithFormat:@"%@\n%@",content,aMsg];

        [newContentwriteToFile:fieldPath atomically:YES encoding:NSUTF8StringEncoding error:&writeError];

        if(writeError) {

            NSLog(@"写入失败:%@\n",[writeErrorlocalizedDescription]);

        }

    }

}

//获取文件大小

+ (unsignedlonglong)fileSizeWithPath:(NSString*)path {

    signedlonglongfileSize =0;

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if([fileManagerfileExistsAtPath:path]) {

        NSError*error =nil;

        NSDictionary*fileDict = [fileManagerattributesOfItemAtPath:patherror:&error];

        if(!error && fileDict) {

            fileSize = [fileDictfileSize];

        }

    }

    returnfileSize;

}

+ (void)clearTmpLog {

    NSError*error;

    NSFileManager *manager = [NSFileManager defaultManager];

    NSString*filePath = [[selfclass]tmpLogPath];

    [managerremoveItemAtPath:filePatherror:&error];

    if(error) {

        NSLog(@"删除失败:%@\n",[error localizedDescription]);

    }

}

相关文章

网友评论

      本文标题:iOS开发生成txt文件

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