首页 > 代码库 > plist文件操作总结

plist文件操作总结

 

plist文件类 (负责文件的读写,删除整个文件)

#import "Plist.h"

@implementation Plist


- (void)writePlist:(NSMutableDictionary*)dictionary

{

   NSArray *array =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDirectory, YES);

   NSString *docPath = [[arrayobjectAtIndex:0]stringByAppendingPathComponent:PLISTNAME];

    [dictionary writeToFile:docPath atomically:YES];

    [array release];

}


- (void)readPlist:(NSMutableDictionary**)dictionary

{

   NSArray *array =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDirectory, YES);

   NSString *docPath = [[arrayobjectAtIndex:0]stringByAppendingPathComponent:PLISTNAME];

    *dictionary = [[NSMutableDictionaryalloc] initWithContentsOfFile:docPath];

}


- (void)deletePlist

{

   NSFileManager *fileManager = [NSFileManagerdefaultManager];

   NSArray *array =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDirectory, YES);

   NSString *docPath = [[arrayobjectAtIndex:0]stringByAppendingPathComponent:PLISTNAME];

    [fileManager removeItemAtPath:docPath error:nil];

}

@end

- (void)viewDidLoad

{

    [superviewDidLoad];


    //文件读写


   NSMutableDictionary *dictionary = [[NSMutableDictionaryalloc] init];

   NSMutableDictionary *dictionary2 = [[NSMutableDictionaryalloc] init];

   NSMutableArray *array = [[NSMutableArrayalloc] init];

   NSMutableDictionary *dictionary1 = [[NSMutableDictionaryalloc] init];

    [dictionary1 setValue:@"001" forKey:@"harewareID"];

    [array addObject:dictionary1];

    [dictionary setValue:array forKey:@"CPU"];   

     self.plist = [[Plistalloc] init];

     //调用写文件

     [self.plistwritePlist:dictionary];

     //读文件

    [self.plistreadPlist:&dictionary2];

   NSMutableArray *array1 = [[NSMutableArrayalloc] init];

    array1 = [dictionary2objectForKey:@"CPU"];

    NSString *str = [[array1 objectAtIndex:0]objectForKey:@"harewareID"];

    NSLog(@"%@",str);



   //文件删除

  

   [self.plistdeletePlist];

   NSMutableDictionary *dictionary3 = [[NSMutableDictionaryalloc] init];

    [self.plistreadPlist:&dictionary3];

   NSMutableArray *array2 = [[NSMutableArrayalloc] init];

    array2 = [dictionary3objectForKey:@"CPU"];

    NSString *str2 = [[array2 objectAtIndex:0]objectForKey:@"harewareID"];

    NSLog(@"%@",str2);


  //文件内容更改,更改一条数据就是把dictionary内key重写。这里重新插入harewareID


  

    NSMutableDictionary *dictionary6 = [[NSMutableDictionaryalloc] init];

   NSMutableDictionary *dictionary5 = [[NSMutableDictionaryalloc] init];

   NSMutableArray *array5 = [[NSMutableArrayalloc] init];

   NSMutableDictionary *dictionary7 = [[NSMutableDictionaryalloc] init];

    [dictionary7 setValue:@"002" forKey:@"harewareID"];

    [array5 addObject:dictionary7];

    [dictionary6 setValue:array5 forKey:@"CPU"];

    [self.plistwritePlist:dictionary6];

    [self.plistreadPlist:&dictionary5];

   NSMutableArray *array4 = [[NSMutableArrayalloc] init];

    array4 = [dictionary5objectForKey:@"CPU"];

    NSString *str4 = [[array4 objectAtIndex:0]objectForKey:@"harewareID"];

    NSLog(@"%@",str4);

plist文件操作总结