我现在是想把客户输入的内容存储到一个文件中,然后再进行读取。但是,多条数据以字符窜的形式保存在同一个文件中……用行与逗号分隔的……如何可以进行删除与分条读取。例如:
//这样有三行数据,其实就是关于一个日期的事件保存
2012-02-02,一,xxx
2012-02-03,二,yyy
2012-02-04,三,zzz
//我现在能做到存储,但是要如何分开读取与操作呢?
//不用数据库,

解决方案 »

  1.   

    //分别用NSData 和NSString,NSMutableDictionary来读取文件内容
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filepath = [documentsDirectory stringByAppendingPathComponent:@"读取的文件名"];
    NSArray* fileData = [[NSArray alloc] initWithContentsOfFile:filepath];NSString* str0 = [array objectAtIndex:0];
    NSString* str1 = [array objectAtIndex:1];
    NSString* str2 = [array objectAtIndex:2];
    //删除第二个数据
    NSArray* newArray = [NSArray arrayWithObjects:
                             @"str0",@"\n\r",@"str2",nil];<4>把数据写入文件
     NSString* fileName = [[filePath objectAtIndex:0]stringByAppendingPathComponent:DBNAME];[newArray writeToFile:fileName atomically:NO];代码类似处理吧,应该可以实现.