美文网首页
文件读取的 四种方式

文件读取的 四种方式

作者: 爱喝农药de清凉 | 来源:发表于2017-08-18 10:29 被阅读26次

//第一种方法: NSFileManager实例方法读取数据
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
NSString* thepath = [paths lastObject];
thepath = [thepath stringByAppendingPathComponent:@"fd_list.txt"];
NSLog(@"桌面目录:%@", thepath);
NSFileManager* fm = [NSFileManager defaultManager];
NSData* data = [[NSData alloc] init];
data = [fm contentsAtPath:thepath];
NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

    //第二种方法: NSData类方法读取数据
    data = [NSData dataWithContentsOfFile:thepath];
    NSLog(@"NSData类方法读取的内容是:%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    
    
    //第三种方法: NSString类方法读取内容
    NSString* content = [NSString stringWithContentsOfFile:thepath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"NSString类方法读取的内容是:\n%@",content);
    
    
    //第四种方法: NSFileHandle实例方法读取内容
    NSFileHandle* fh = [NSFileHandle fileHandleForReadingAtPath:thepath];
    data = [fh readDataToEndOfFile];
    NSLog(@"NSFileHandle实例读取的内容是:\n%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
return 0;

JSON:
NSString * jsonPath = [[NSBundle mainBundle]pathForResource:@"json" ofType:@"json"];
NSData * jsonData = [[NSData alloc]initWithContentsOfFile:jsonPath];

NSMutableDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];

NSArray * list = jsonDic[@"list"];

相关文章

  • Spring boot 读取properties文件的四种方式

    Spring boot 读取properties文件的四种方式 方式一使用@Value注解在application...

  • IPC机制

    安卓四种进程间通信方式: 文件共享(文件并发读取不安全,SharedPreferences也是一个XML文件,但是...

  • 文件读取的 四种方式

    //第一种方法: NSFileManager实例方法读取数据NSArray* paths = NSSearchPa...

  • python 文件操作

    fp=open("文件路径","方式") 文件读取 文件写入 文件关闭 文件读取写入方式

  • Python四种逐行读取文件内容的方法

    Python四种逐行读取文件内容的方法 下面四种Python逐行读取文件内容的方法, 分析了各种方法的优缺点及应用...

  • 算法技术面

    . R语言的文件读取:csv文件的读取方式(read.csv),txt文件的读取方式(read.table) 2....

  • python3读写文件的方式:

    一、读取文本格式的文件:open 读取内容的方式: 写文件方式: 例: 二、读取csv格式的文件: 例:

  • golang 读取文件的四种方式

    参考 Go实战--golang中读写文件的几种方式 读文件 读取的文件放在file/test:也就是file包下的...

  • numpy总结

    1、文件读取 np.genfromtxt(文件路径,delimiter=分割方式,dtype=读取方式) 2、nu...

  • node基本模块之fs

    异步方式 读取文本文件 读取二进制文件

网友评论

      本文标题:文件读取的 四种方式

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