zoukankan      html  css  js  c++  java
  • 【旧事重提】iOS中文件读写

    ---恢复内容开始---

    1. 读取文件  (本地 UTF-8编码的文本文件

        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"song" ofType:@"json"];

        NSString *str =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

        //NSData *data = [NSData dataWithContentsOfFile:filePath];

        //NSString *textFile  = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        id jsonObj = [str jsonObject];

        NSArray *item = [jsonObj objectForKey:@"data"];

        NSLog(@"arr %lu %@", (unsigned long)[item count], item);

     

    2. 写入文件

    //Document 目录

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

    NSString *documentsDirectory = [paths objectAtIndex:0];需要的路径

    NSString *fileD = [documentSDirectory stringByAppendingPathComponent:@"xxx.txt"];

     

     NSString* fileName = [[filePath objectAtIndex:0] stringByAppendingPathComponent: DBNAME];

       [fileData writeToFile:fileName atomically:YES]

    ---恢复内容结束---

  • 相关阅读:
    symbol
    es6的对象新增的方法
    关于一个有趣的知识
    我为什么要记笔记?
    学习 yjango 博士的学习方法后的总结
    关于写博客的好处
    这是一片博客的测试
    【牛客19】(思路)
    【HDOJ】find your present (2)(思路题)
    【素数】Eratosthenes筛选
  • 原文地址:https://www.cnblogs.com/lee4519/p/4279088.html
Copyright © 2011-2022 走看看