数据存储-简单存储

 获取Documents目录
//常量NSDocumentDirectory表明我们正在查找Documents目录的路径,数组位于索引0处的Documents目录
NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//filename将包含theFile.txt文件的完整路径,用于创建读取和写入文件
NSString *filename = [Path stringByAppendingPathComponent:@"theFile.txt"];


/*=======================================================
NSKeyedArchiver 存储方法1
========================================================*/
NSString *str = @"abc";
NSString *astr = @"efg";
NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];
    
//Save
NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filename = [Path stringByAppendingPathComponent:@"test"];
[NSKeyedArchiver archiveRootObject:Array toFile:filename];
    
str = @"a";
astr = @"";
    
//load
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile: filename];
str = [arr objectAtIndex:0];
astr =  [arr objectAtIndex:1];
    
NSLog(@"str:%@",str);
NSLog(@"astr:%@",astr);
   
/*=======================================================
NSUserDefaults 存储方法2
========================================================*/
NSString *str = @"abc";
NSString *astr = @"efg";
NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];
    
//Save
NSUserDefaults *SaveDefaults = [NSUserDefaults standardUserDefaults];
[SaveDefaults setObject:Array forKey:@"SaveKey"];
    
str = @"a";
astr = @"";
    
//load
Array = [SaveDefaults objectForKey:@"SaveKey"];
str = [Array objectAtIndex:0];
astr = [Array objectAtIndex:1];
NSLog(@"str:%@",str);
NSLog(@"astr:%@",astr);

/*=======================================================
writeToFile:
========================================================*/
NSString *str = @"abc";
NSString *astr = @"efg";
NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];
   
//Save
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
    NSLog(@"Documents directory not found!");
}
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"Savedatas.plist"];
[[NSArray arrayWithObjects:Array,nil] writeToFile:appFile atomically:NO];    
     
//load
if([[NSFileManager defaultManager] fileExistsAtPath:appFile])
    self.SaveDataArray = [NSMutableArray arrayWithContentsOfFile:appFile];       
else
    self.SaveDataArray = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Savedatas" ofType:@"plist"]];
NSArray *strArray = [self.SaveDataArray objectAtIndex:0];
   
str = [strArray objectAtIndex:0];
astr = [strArray objectAtIndex:1];

-(BOOL) writeApplicationData:(NSDictionary *)data  writeFileName:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    if (!documentsDirectory) {
        NSLog(@"Documents directory not found!");
        return NO;
    }
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
    return ([data writeToFile:appFile atomically:YES]);
}

-(id) readApplicationData:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
    NSDictionary *myData = [[[NSDictionary alloc] initWithContentsOfFile:appFile] autorelease];
    return myData;
}

/------------------------------------------------/
//  SaveDataInf.m
//  SaveDataUseChar
//
//  Created by Lixf on 10-4-26.
//  Copyright 2010 Lixf. All rights reserved.
//

#import "SaveDataInf.h"


@implementation SaveDataInf
@synthesize SaveData;
@synthesize FontSize, Color, Aligment;

-(id)initDataFromPath{
    NSString *Path = [[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingFormat:@"/Documents/"];
    NSString *LastPath = [Path stringByAppendingFormat:@"Data.inf"];
    if(SaveData != nil)
    {
        [SaveData release];
        SaveData = nil;
    }
    SaveData = [[NSMutableData alloc] initWithContentsOfFile:LastPath];
    unsigned char DataBuffer[10];
    if(SaveData == nil)
    {
        SaveData = [[NSMutableData alloc] initWithLength:1000];
        memset(DataBuffer, 0x0, sizeof(DataBuffer));
        FontSize = 18;
        Color = 0;
        Aligment = 0;
        DataBuffer[0] = FontSize;
        DataBuffer[1] = Color;
        DataBuffer[2] = Aligment;
        [SaveData replaceBytesInRange:NSMakeRange(0, 10) withBytes:DataBuffer];
    }else {
        [SaveData getBytes:DataBuffer range:NSMakeRange(0, 10)];
        FontSize = DataBuffer[0];
        Color = DataBuffer[1];
        Aligment = DataBuffer[2];
    }
    return self;
}

-(BOOL)writeDataToPath{
    NSString *Path = [[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingFormat:@"/Documents/"];
    NSString *LastPath = [Path stringByAppendingFormat:@"Data.inf"];
    BOOL isSuccess = [SaveData writeToFile:LastPath atomically:YES];
    if(isSuccess == YES)
    {
        return YES;
    }else {
        return NO;
    }

}

-(void)setFontSize:(char)size{
    unsigned char DataBuffer[10];
    FontSize = size;
    DataBuffer[0] = size;
    [SaveData replaceBytesInRange:NSMakeRange(0, 1) withBytes:DataBuffer];
}

-(void)setFontColor:(char)color{
    unsigned char DataBuffer[10];
    Color = color;
    DataBuffer[0] = color;
    [SaveData replaceBytesInRange:NSMakeRange(1, 1) withBytes:DataBuffer];
}

-(void)setFontAligment:(char)aligment{
    unsigned char DataBuffer[10];
    Aligment = aligment;
    DataBuffer[0] = aligment;
    [SaveData replaceBytesInRange:NSMakeRange(2, 1) withBytes:DataBuffer];
}

- (void)dealloc {
   if(SaveData != nil)
   {
       [SaveData release];
       SaveData = nil;
   }
    [super dealloc];
}
@end

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值