IOS 文件的读写详解
来源:互联网 发布:dnftgp角色数据不更新 编辑:程序博客网 时间:2024/05/16 12:48
在开发的过程中,经常碰见文件读写的时候,这里我就简单记录一些在ios开发里面的文件读写api,为了以后用着方便。
ios开发里面,文件的目录是固定的,可以用 NSHomeDirectory()方法读取项目文件的目录,
NSString* path = NSHomeDirectory();
这样得到的应用程序的主目录,基本是不会变的,完整的模拟器路径可能是这样的:/Users/cadamson/Libary/Application Support/iPhone Simulator/User/Applications/5c73EBC6-...-...-...-,最后一部分就是一个随机生成的应用程序ID,这个id每次构建应用时都会重新声成。当然,由于iphone的安全模型不允许访问到任何此目录之上的目录层次,所以这个目录的名称以及它上面目录的名称其实根本无关紧要
NSFileManager *fileManager = [NSFileManagerdefaultManager];
<>查找文件:NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
// 传递 0 代表是找在Documents 目录下的文件。
NSString *documentDirectory = [directoryPathsobjectAtIndex: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 plist文件的读写
- iOS-plist文件的读写
- iOS plist文件的读写
- IOS Plist文件的读写
- iOS plist 文件的读写
- Android文件的读写详解
- iOS学习之 plist文件的读写
- IOS 应用文件数据的读写分析
- IOS学习之 plist文件的读写
- iOS学习之 plist文件的读写
- iOS学习之 plist文件的读写
- iOS学习之 plist文件的读写
- iOS学习之 plist文件的读写
- IOS 应用文件数据的读写分析
- 把24位和32位BMP位图转为灰色bmp位图
- Access Control
- pat 1082. Read Number in Chinese (25)
- static的用途
- android真机不能调试
- IOS 文件的读写详解
- django-south使用
- Hadoop管理员的十个最佳实践
- Java数据库编程(章节摘要)
- 字符流缓冲区
- dedecms 2级栏目的调用
- UVa 254 - Towers of Hanoi 解题报告(递归)
- Sequoiadb 测试体验系列之五 – Java 开发2
- 百度编辑器ueditor常用设置函数大全