iOS 通过路径对文件写入数据 读取数据
2016-07-21 16:27
465 查看
1获取沙盒不同文件的路径、
-(void)getFilePath{
// 1、获取程序的Home目录
NSString *path =
NSHomeDirectory();
NSLog(@"这是home目录%@",path);
// 2、获取Document目录
NSArray *documentpaths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSLog(@"%@",documentpaths);
NSString *documentpath = [documentpaths
objectAtIndex:0];
NSLog(@"这是Docuemt目录%@",documentpath);
// 3、获取Cache目录
NSArray *cachePath =
NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask,
YES);
NSString *cachePaths = [cachePath
objectAtIndex:0];
NSLog(@"这是Cache目录%@",cachePaths);
// 4、获取Library目录
NSArray *tempPath =
NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask,
YES);
NSString *tempPaths = [tempPath
objectAtIndex:0];
NSLog(@"这是Librayr目录%@",tempPaths);
// 5、获取Tmp目录
NSString *tempDirectory =
NSTemporaryDirectory();
NSLog(@"这是temp目录%@",tempDirectory);
}
2将数据写入所创建的路径中
-(void)writeFileContent{
// 将内容写入文件
NSArray *path =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *paths = [path
objectAtIndex:0];
// NSArray *array = [[NSArray alloc]initWithObjects:@"内容",@"content", nil];
NSArray *array =
@[@"abc",@"def",@"ggg",@10];
NSString *filepath = [paths
stringByAppendingPathComponent:@"textFile.text"];
[array writeToFile:filepath
atomically:YES];
}
3通过路径读取文件的数据、
-(void)readFile{
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [paths
objectAtIndex:0];
NSString *filePath1 = [docDir
stringByAppendingPathComponent:@"textFile.text"];
NSArray *array = [[NSArray
alloc] initWithContentsOfFile:filePath1];
NSLog(@"%@",array);
}
-(void)getFilePath{
// 1、获取程序的Home目录
NSString *path =
NSHomeDirectory();
NSLog(@"这是home目录%@",path);
// 2、获取Document目录
NSArray *documentpaths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSLog(@"%@",documentpaths);
NSString *documentpath = [documentpaths
objectAtIndex:0];
NSLog(@"这是Docuemt目录%@",documentpath);
// 3、获取Cache目录
NSArray *cachePath =
NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask,
YES);
NSString *cachePaths = [cachePath
objectAtIndex:0];
NSLog(@"这是Cache目录%@",cachePaths);
// 4、获取Library目录
NSArray *tempPath =
NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask,
YES);
NSString *tempPaths = [tempPath
objectAtIndex:0];
NSLog(@"这是Librayr目录%@",tempPaths);
// 5、获取Tmp目录
NSString *tempDirectory =
NSTemporaryDirectory();
NSLog(@"这是temp目录%@",tempDirectory);
}
2将数据写入所创建的路径中
-(void)writeFileContent{
// 将内容写入文件
NSArray *path =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *paths = [path
objectAtIndex:0];
// NSArray *array = [[NSArray alloc]initWithObjects:@"内容",@"content", nil];
NSArray *array =
@[@"abc",@"def",@"ggg",@10];
NSString *filepath = [paths
stringByAppendingPathComponent:@"textFile.text"];
[array writeToFile:filepath
atomically:YES];
}
3通过路径读取文件的数据、
-(void)readFile{
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [paths
objectAtIndex:0];
NSString *filePath1 = [docDir
stringByAppendingPathComponent:@"textFile.text"];
NSArray *array = [[NSArray
alloc] initWithContentsOfFile:filePath1];
NSLog(@"%@",array);
}
相关文章推荐
- 微软发布令牌漏洞公告:可绕过 Chromium 沙盒执行任意代码
- 我是运营,我没有假期
- DB2数据库的安装
- C#实现把指定数据写入串口
- “传奇”图象数据存储方式
- 修复mysql数据库
- 浅析SQL数据操作语句
- SQLServer 数据导入导出的几种方法小结
- 简述MySQL分片中快速数据迁移
- MySQL数据备份之mysqldump的使用详解
- C#实现窗体间传递数据实例
- C#中的委托数据类型简介
- SQL Server删除表及删除表中数据的方法
- SqlServer2008误操作数据(delete或者update)后恢复数据的方法
- 给你的数据库文件减肥
- Oracle数据更改后出错的解决方法
- Oracle数据库数据丢失恢复的几种方法总结
- Ajax点击不断加载数据列表
- C#将Sql数据保存到Excel文件中的方法
- C#实例代码之抽奖升级版可以经表格数据导入数据库,抽奖设置,补抽