iOS - NSFileManager的读写操作
来源:互联网 发布:乐乎城市青年社区 亦庄 编辑:程序博客网 时间:2024/05/21 16:59
-(void) write { //创建文件管理器 NSFileManager *fileManager = [NSFileManager defaultManager]; //获取路径 //参数NSDocumentDirectory要获取那种路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0];//去处需要的路径 //更改到待操作的目录下 [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]]; //[fileManager removeItemAtPath:@"config" error:nil];//移除本文件管理器下的该项 NSString *path = [documentsDirectory stringByAppendingPathComponent:@"config"];//获取文件路径 //判断文件是否存在 if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {//如果文件不存在则创建 //创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil NSData *d_data=[[NSMutableDictionary alloc] init]; [d_data setValue:@"" forKey:@"userid"];//手机号 [d_data setValue:@"" forKey:@"pwd"];//密码 [d_data setValue:@"0" forKey:@"backup"];//备份类型 [fileManager createFileAtPath:path contents:d_data attributes:nil]; NSString *str = @"a test file name"; BOOL succeed = [str writeToFile: [documentsDirectory stringByAppendingPathComponent:@"test.xml"] atomically: YES encoding: NSUTF8StringEncoding error: nil]; NSLog( @"succeed is %d", succeed ); // yes -> 写成功 no->写失败 [d_data release]; } } - (void)read { //读取数据 NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath: @"test.xml"]; NSData *data = [file readDataToEndOfFile];//得到xml文件 //读取到NSDate中 NSString* aStr; aStr = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; //转换为NSString NSLog( @"aStr is %@", aStr ); [file closeFile]; }
原博客地址:http://blog.csdn.net/iorchid/article/details/6752859
0 0
- iOS - NSFileManager的读写操作
- IOS中NSFileManager的操作
- iPhone - NSFileManager的读写操作(上)
- [绍棠] iOS文件目录和文件操作 及NSFileManager的读写操作
- ios读写文件NSFileManager
- IOS中NSFileManager的操作 [Z]
- iOS文件操作 NSFileManager
- iOS NSFileManager文件操作
- iOS NSFileManager(文件操作)
- ios 文件操作 NSFileManager
- iOS NSFileManager文件操作
- iOS读写文件(NSFileManager)
- IOS中NSFileManager的操作 [Z](文件操作)
- ios学习笔记:文件操作(NSFileManager)iOS的沙盒中
- IOS 文件操作(NSFileManager)
- iOS文件操作(NSFileManager)
- iOS文件相关操作(NSFileManager)
- iOS NSFileManager 文件操作详解
- UItableView 插入删除数据 动画
- 函数指针和函数数组
- Linux硬盘分区,挂载,格式化,加到开机启动项
- 奔跑吧!兄弟!未来的世界属于OCR!属于识别!
- C/C++文件操作详解
- iOS - NSFileManager的读写操作
- hadoop context.write
- asp.net-水印文本框/字符过滤文本框
- Linux权限
- python 函数参数传递和参数匹配
- linux网络编程之socket(十四):基于UDP协议的网络程序
- Facade模式
- HDU 4864 Task
- go Stack 简单实现