zoukankan      html  css  js  c++  java
  • 数据追加写入沙盒路径,而不是覆盖之前的数据

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

    /**

     *  @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];

        }

    }

  • 相关阅读:
    python 3.x报错:No module named 'cookielib'或No module named 'urllib2'
    Xshell实现Windows和使用跳板机跳转的远程Linux互传文件
    Linux scp常用命令
    正则表达式
    [NBUT 1458 Teemo]区间第k大问题,划分树
    [hdu5416 CRB and Tree]树上路径异或和,dfs
    [vijos P1008 篝火晚会]置换
    [hdu5411 CRB and Puzzle]DP,矩阵快速幂
    [hdu4713 Permutation]DP
    [hdu4710 Balls Rearrangement]分段统计
  • 原文地址:https://www.cnblogs.com/lfgtechblog/p/5173171.html
Copyright © 2011-2022 走看看