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

        }

    }

  • 相关阅读:
    10个用jQuery实现图片幻灯片/画廊效果和源码
    老赵面试题参考答案(二)
    C#的显式接口和隐式接口
    老赵面试题参考答案(三)
    C#中的参数传递:值类型(value type)和引用类型(reference type)
    word转换成html的方法
    老赵面试题参考答案(四)
    五个Metro UI 风格的网页设计
    老赵面试题参考答案(六)
    概要设计怎么写?
  • 原文地址:https://www.cnblogs.com/lfgtechblog/p/5173171.html
Copyright © 2011-2022 走看看