【iOS_Development】文件操作
来源:互联网 发布:太阳花女王 知乎 编辑:程序博客网 时间:2024/06/05 17:26
- NSFileManager:是用来管理文件系统的,它可以用来进行常见的文件\文件夹操作
- 获取NSFileManager示例
[NSFileManager defaultManager]
增删改查
1. 创建文件夹
- (void)createFolder { // 获取documentsPath NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; // 文件夹路径 NSString *folderPath = [documentsPath stringByAppendingPathComponent:@"selfFolder"]; NSError *error; BOOL temp = [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error]; if (temp) { NSLog(@"文件夹创建成功:%@", folderPath); } else { NSLog(@"文件夹创建失败\n失败原因:%@", error); }}
2. 创建文件
- (void)createFile { // 获取documentsPath NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; // 设置文件夹路径 NSString *folderPath = [documentsPath stringByAppendingPathComponent:@"selfFolder"]; NSString *testPath = [folderPath stringByAppendingPathComponent:@"selfFile.txt"]; BOOL temp = [[NSFileManager defaultManager] createFileAtPath:testPath contents:nil attributes:nil]; if (temp) { NSLog(@"文件创建成功:%@", testPath); } else { NSLog(@"文件创建失败"); }}
3. 向文件中写入数据
- (void)writeDataToFile { // 获取documentsPath NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; // 文件夹路径 NSString *folderPath = [documentsPath stringByAppendingPathComponent:@"selfFolder"]; // 文件路径 NSString *filePath = [folderPath stringByAppendingPathComponent:@"selfFile.txt"]; NSString *content = @"hello world"; NSError *error; BOOL temp = [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]; if (temp) { NSLog(@"文件写入成功:%@", filePath); } else { NSLog(@"文件写入失败\n失败原因:%@", error); }}
4. 删除文件
- (void)deleteFile { // 获取documentsPath NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; // 文件夹路径 NSString *folderPath = [documentsPath stringByAppendingPathComponent:@"selfFolder"]; // 文件路径 NSString *filePath = [folderPath stringByAppendingPathComponent:@"selfFile.txt"]; NSError *error; BOOL temp = [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error]; if (temp) { NSLog(@"文件删除成功"); } else { NSLog(@"文件删除失败\n失败原因:%@", error); }}
5. 从文件中读取数据
- (void)readFile { // 获取documentsPath NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; // 文件夹路径 NSString *folderPath = [documentsPath stringByAppendingPathComponent:@"selfFolder"]; // 文件路径 NSString *filePath = [folderPath stringByAppendingPathComponent:@"selfFile.txt"];// NSData *data = [NSData dataWithContentsOfFile:filePath]; NSError *error; NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error]; if (!error) { NSLog(@"文件读取成功:%@", content); } else { NSLog(@"文件写入失败\n失败原因:%@", error); }}
常用工具方法
1. 判断文件是否存在
[[NSFileManager defaultManager] fileExistsAtPath:filePath]
2. 判断是否为一个目录
[[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDir];
3. 判断文件是否可读
[[NSFileManager defaultManager] isReadableFileAtPath:filePath];
4. 是否可写
[[NSFileManager defaultManager] isWritableFileAtPath:filePath];
5. 是否可删除
[[NSFileManager defaultManager] isDeletableFileAtPath:filePath];
6. 获取文件属性
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
7. copy文件
[[NSFileManager defaultManager] copyItemAtPath:path1 toPath:path2 error:nil];
8. 移动文件
[[NSFileManager defaultManager] moveItemAtPath:createDirPath toPath:targetPath error:nil];
0 0
- 【iOS_Development】文件操作
- 【iOS_Development】文件操作
- 【iOS_Development】校验工具类
- 【iOS_Development】isKindOfClass && isMemberOfClass
- 【iOS_Development】frame VS bounds
- 【iOS_Development】广告图的添加
- 【iOS_Development】消息转发机制简述
- 【iOS_Development】delegate的使用优化
- 【iOS_Development】添加/隐藏UITabBar右上角的小红点
- 【iOS_Development】iOS沙盒目录简述
- iOS_Development~ 添加 / 隐藏 UITabBar 右上角的小红点
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- An ffmpeg and SDL Tutorial 06
- Hibernate + Oracle实现主键自增长
- css不常见属性
- Java数组总结之自制简单数组工具类
- workbench导出的Schema文件,在saiku中运行出错。
- 【iOS_Development】文件操作
- hdu 4635 Strongly connected 【图论-强连通分量-缩点-完全图】
- An ffmpeg and SDL Tutorial 08
- django学习
- 第八届蓝桥杯JavaB组省赛
- 如何在iframe内部弹出浮层,浮层遮罩整个浏览器
- CNTK使用BS创建网络模型方法总结---免费视频教程
- 支持向量机
- Android视频直播02——直播不得知道的基础