ios文件读写
来源:互联网 发布:图文网站源码 编辑:程序博客网 时间:2024/04/27 17:02
在开发的过程中,经常碰见文件读写的时候,这里我就简单记录一些在ios开发里面的文件读写api,为了以后用着方便。
ios开发里面,文件的目录是固定的,可以用 NSHomeDirectory()方法读取项目文件的目录,
NSString* path = NSHomeDirectory();
这样得到的应用程序的主目录,基本是不会变的,完整的模拟器路径可能是这样的:/Users/cadamson/Libary/Application Support/iPhone Simulator/User/Applications/5c73EBC6-...-...-...-,最后一部分就是一个随机生成的应用程序ID,这个id每次构建应用时都会重新声成。当然,由于iphone的安全模型不允许访问到任何此目录之上的目录层次,所以这个目录的名称以及它上面目录的名称其实根本无关紧要
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
// 传递 0 代表是找在Documents 目录下的文件。
NSString *documentDirectory = [paths objectAtIndex:0];
// DBNAME 是要查找的文件名字,文件全名
NSString *filePath = [documentDirectory stringByAppendingPathComponent:DBNAME];
// 用这个方法来判断当前的文件是否存在,如果不存在,就创建一个文件
if ( ![fileManager fileExistsAtPath:path]) {
[fileManager createFileAtPath:path contents:nil attributes:nil];
}
<3>读取文件数据:
//分别用NSData 和NSString,NSMutableDictionary来读取文件内容
NSData* fileData = [NSData dataWithContentsOfFile:DBNAME];
NSString* myString = [NSString stringWithContentsOfFile:DBNAME usedEncoding:NULL error:NULL];
NSMutableDictionary* dict = [[NSMutableDictionary alloc]initWithContentsOfFile:fileName];
<4>把数据写入文件
NSString* fileName = [[filePath objectAtIndex:0]stringByAppendingPathComponent:DBNAME];
[fileData writeToFile:fileName atomically:YES]
- ios中读写文件
- ios文件读写
- IOS 上读写文件
- iOS文件读写
- ios关于文件读写
- IOS-读写文件大全
- ios读写文件
- ios文件读写
- IOS 读写文件
- ios文件读写操作
- ios中读写文件
- iOS 文件读写
- ios读写文件
- IOS 读写plist文件
- ios 文件读写
- ios plist文件读写
- iOS 文件读写
- ios文件读写
- 年底招聘“六招”
- 纯JSP用户登录系统
- jdk内置工具(二)
- [leetcode] BST与链表的相互转换
- 单独编译某个工程
- ios文件读写
- Spark的standalone源码分析(一)
- Linux内核代码华人贡献统计
- 教务系统数据库设计 (一)
- 来北京的日子---二三事
- iOS注册系统通知
- 语句块操作
- 睿辰欣创网络科技的面试经历
- linux内核内存管理中的pagevec结构体