Objective-C 文件处理
来源:互联网 发布:windows激活密匙在哪 编辑:程序博客网 时间:2024/06/11 13:46
文件处理在 NSFileManager类的帮助下可实现大多数文件操作功能。
文件处理的方法
下面列出的方法用于访问和操作文件列表。在这里,我们必须替换FilePath1, FilePath2 和 FilePath 为我们所需的完整的文件路径字符串,以获得所需的动作。
检查一个路径文件是否存在
NSFileManager *fileManager = [NSFileManager defaultManager]; //Get documents directory NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectoryPath = [directoryPaths objectAtIndex:0]; if ([fileManager fileExistsAtPath:@""]==YES) { NSLog(@"File exists"); }
比较两个文件的内容
if ([fileManager contentsEqualAtPath:@"FilePath1" andPath:@" FilePath2"]) { NSLog(@"Same content"); }
检查是否可写,可读和可执行
if ([fileManager isWritableFileAtPath:@"FilePath"]) { NSLog(@"isWritable"); } if ([fileManager isReadableFileAtPath:@"FilePath"]) { NSLog(@"isReadable"); } if ( [fileManager isExecutableFileAtPath:@"FilePath"]){ NSLog(@"is Executable"); }
移动文件
if([fileManager moveItemAtPath:@"FilePath1" toPath:@"FilePath2" error:NULL]){ NSLog(@"Moved successfully"); }
复制文件
if ([fileManager copyItemAtPath:@"FilePath1" toPath:@"FilePath2" error:NULL]) { NSLog(@"Copied successfully"); }
删除文件
if ([fileManager removeItemAtPath:@"FilePath" error:NULL]) { NSLog(@"Removed successfully"); }
读取文件
NSData *data = [fileManager contentsAtPath:@"Path"];
写文件
[fileManager createFileAtPath:@"" contents:data attributes:nil];
0 0
- Objective-C 文件处理
- objective-c之文件处理
- Objective-C中对文件的处理
- Objective-c异常处理
- Objective-C字符串处理
- Objective-C字符串处理
- Objective-C字符串处理
- Objective-C字符串处理
- objective -c 异步处理
- objective-c 异常处理
- Objective-C字符串处理
- Objective-C字符串处理
- Objective-C字符串处理
- Objective-c 字符串处理
- Objective-C错误处理
- Objective - C 断言处理
- Objective-C:错误处理
- objective-c 使用文件
- 基于Spring MVC(REST API)做单元测试(mockito)
- html2image 乱码
- uva 1648 Business Center 简单数学
- 系统端口详解
- iOS显示启动页时隐藏状态栏
- Objective-C 文件处理
- js序列化ajax参数对象
- 序列图
- Ubuntu14.04安装与安装后的软件的安装
- binder 分析(2)_client端程序
- 简述Android单位dp、sp、dip、px、等的区别和使用
- Andorid之ListView优化
- 如何直接下载WGS84的无偏移无水印的谷歌卫星地图数据
- 简单配置Apache服务器