集合属性列表类(NSArray与NSDictionary)读写文件操作

来源:互联网 发布:索尼xzp挂起网络 编辑:程序博客网 时间:2024/06/08 20:18
本文参照自:
http://blog.csdn.net/zhangmiaoping23/article/details/42644251
http://www.xuebuyuan.com/1686844.html
一、前言
iOS中文件的存储位置主要有以下几类:
Documents:用于存储应用程序中经常需要读取或写入的常规文件。
tmp:用于存储应用程序运行时生成的文件。(随着应用程序的关闭失去了利用价值)
Library:一般存放应用程序的配置文件,比如说plist类型的文件。
NSArray与NSDictionary读写文件操作 - hubingforever - 民主与科学
 
集合属性列表类(NSArray,NSDictionary)具有writeToFile方法用于将属性列表写入文件。NSString和NSData也具有writeToFile这个方法,但是它们只能写字符串和数据块。
二、NSArray读写文件
1、NSArray写文件
示例1-1

int main(int argc, charchar *argv[]) {    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];      NSArray* animals = [NSArray arrayWithObjects:@"dog",@"cat",nil];      NSString * arraypath = @"/Documents/array.txt";      [animals writeToFile:arraypath atomically:YES];      [pool release];      return 0;  }
 

最后写到手机上的plist文件array.txt的内容 如下:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><array>    <string>dog</string>    <string>cat</string></array></plist>

2、NSArray读文件
NSArray读文件读文件就比较简单了,如下所示
示例1-2
NSString * arraypath = @"/Documents/array.txt";  
NSArray* animals = [NSArray arrayWithContentsOfFile:arraypath];  
三、NSDictionary读写文件
1、NSDictionary写文件
示例2-1

int main(int argc, charchar *argv[]) {  
      
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
    NSArray* animals = [NSArray arrayWithObjects:@"dog",@"cat",nil];  
    NSArray* operations = [NSArray arrayWithObjects:@"windows",@"Mac OS X",nil];  
    NSArray* books = [NSArray arrayWithObjects:@"C++",@"C",nil];   
  NSDictionary* Dict = [NSDictionary dictionaryWithObjectsAndKeys:animals,@"animal",operations,@"operation system",books,@"programming",nil];  
    NSString * dictpath = @"/Documents/dict.txt";  
    [Dict writeToFile:dictpath atomically:YES];   
    [pool release];  
    return 0;  
} 

最后写到手机上的plist文件dict.txt文件的内容

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>animal</key>
    <array>
        <string>dog</string>
        <string>cat</string>
    </array>
    <key>operation system</key>
    <array>
        <string>windows</string>
        <string>Mac OS X</string>
    </array>
    <key>programming</key>
    <array>
        <string>C++</string>
        <string>C</string>
    </array>
</dict>
</plist>

可见生成的是plist文件,<plist></plist>的第一个标志指定了类型。 
2、NSDictionary读文件
NSDictionary读取文件也比较简单了,如下所示
示例3-2

NSString * dictpath = @"/Documents/dict.txt";  
NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:dicpath];

四、NSArray和NSDictionary的混合读取
首先创建一个plist文件,名字是student。右键add row 加入行。
NSArray与NSDictionary读写文件操作 - hubingforever - 民主与科学
 选中plist文件,右键 》 open as 》 source code,这样plist文件会以xml格式呈现,修改、删除其中的值。
NSArray与NSDictionary读写文件操作 - hubingforever - 民主与科学
 现在用数组NSArray接受NSArray混有NSDictionary的plist文件数据。

NSString *fielPath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"]; NSArray *students = [NSArray arrayWithContentsOfFile:fielPath]; for (NSDictionary *stu in students) { NSLog(@"num = %@, name = %@", [stu objectForKey:@"num"], [stu objectForKey:@"name"]); }

结果:
NSArray与NSDictionary读写文件操作 - hubingforever - 民主与科学
 
结束!
0 0
原创粉丝点击