• 数据追加写入沙盒路径,而不是覆盖之前的数据


    临近春节了,这段时间比较忙,各种赶项目,没啥时间写博客。

    /**

     *  @brief 追加写入数据到沙盒路径

     *

     *  @param string   要写入的字符串

     *  @param fileName 把数据写入文件的文件名

     */

    +(void)writefile:(NSString *)string fileName:(NSString *)fileName

    {

        NSLog(@"fileName==%@",fileName);

        NSArray *paths  = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

        NSString *homePath = [paths objectAtIndex:0];

        

        NSString *filePath = [homePath stringByAppendingPathComponent:fileName];

        

        NSFileManager *fileManager = [NSFileManagerdefaultManager];

        

        if(![fileManager fileExistsAtPath:filePath]) //如果不存在

        {

            NSLog(@"-------文件不存在,写入文件----------");

            NSError *error;

            if([string writeToFile:filePath atomically:YESencoding:NSUTF8StringEncodingerror:&error])

            {

                NSLog(@"------写入文件------success");

            }

            else

            {

                 NSLog(@"------写入文件------fail,error==%@",error);

            }

        }

        else//追加写入文件,而不是覆盖原来的文件

        {

            NSLog(@"-------文件存在,追加文件----------");

            NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];

            

            [fileHandle seekToEndOfFile];  //将节点跳到文件的末尾

            

            

            NSData* stringData  = [string dataUsingEncoding:NSUTF8StringEncoding];

            

            [fileHandle writeData:stringData]; //追加写入数据

            

            [fileHandle closeFile];

        }

    }

  • 相关阅读:
    oracle如何查询哪个表数据量大
    SecureRandom生成随机数超慢 导致tomcat启动时间过长的解决办法
    smartctl----硬盘状态监控
    Oracle数据库的状态查询
    jdbc连接数据库使用sid和service_name的区别
    V$INSTANCE 字段说明
    V$PROCESS和V$SESSION,以及使用这两个视图能做什么
    NetOps Defined
    POI 海量数据
    HTML5 CSS3 诱人的实例: 3D立方体旋转动画
  • 原文地址:https://www.cnblogs.com/lfgtechblog/p/5173171.html
Copyright © 2020-2023  润新知