---
title: "文件数据存储查看"
author: "higher141125"
date: "2013-06-25T14:08:33Z"
tags: []
categories: ["互联网"]
slug: "untitled"
draft: false
lastmod: "2013-06-25T14:08:32Z"
id: 1893969
url: "https://higher141125.iteye.com/blog/1893969"
---

1.文件内容查看

```
-(void)loadFile{
    NSString *filePath=@"/Users/qing/Library/haha.txt";
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
        NSData *data=[[NSMutableData alloc]initWithContentsOfFile:filePath];
        NSString * str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"content=%@",str);
    }else{
        NSLog(@"NO");
    }
}
```

 2.文件存档

```
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

[archiver encodeObject:fourLines forKey:kDataKey];

[archiver finishEncoding];

[data writeToFile:[self dataFilePath] atomically:YES];
```

3.获取XML文件

```
NSString *filePath = [[NSBundle mainBundle] pathForResouse:@"re" ofType:@"xml"];
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
```

 

获取属性列表 

```
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ViewControllers" ofType:@"plist"]];
```

 

4.用户首选项NSuserDefaults

```
 NSUserDefaults *Defaults=
    [NSUserDefaults standardUserDefaults];
    
    [Defaults setObject:@"helloc ddddyou22" forKey:@"hello"];
  //会有缓存数据，要clean
   NSLog(@"%@",[Defaults objectForKey:@"hello"]);
```