iOS 教程
- 第1节:IOS 简介
- 第2节:IOS 环境搭建
- 第3节:IOS Objective C 基础知识
- 第4节:IOS 创建第一款iPhone应用程序
- 第5节:IOS 操作(action)和输出口(Outlet)
- 第6节:IOS 委托(Delegates)
- 第7节:IOS UI元素
- 第8节:IOS 加速度传感器(accelerometer)
- 第9节:IOS 通用应用程序
- 第10节:IOS 相机管理
- 第11节:IOS 定位操作
- 第12节:IOS SQLite数据库
- 第13节:IOS 发送电子邮件
- 第14节:IOS 音频和视频(Audio & Video)
- 第15节:IOS 文件处理
- 第16节:IOS 地图开发
- 第17节:IOS 应用内购买
- 第18节:IOS 整合iAD
- 第19节:IOS GameKit
- 第20节:IOS 故事板(Storyboards)
- 第21节:IOS 自动布局
- 第22节:IOS Twitter和Facebook
- 第23节:IOS 内存管理
- 第24节:IOS 应用程序调试
IOS 文件处理
简介
文件处理不能直观的通过应用程序来解释,我们可以从以下实例来了解IOS的文件处理。
IOS中对文件的操作. 因为应用是在沙箱(sandbox)中的,在文件读写权限上受到限制,只能在几个目录下读写文件。
文件处理中使用的方法
下面列出了用于访问和操作文件的方法的列表。
以下实例你必须替换FilePath1、FilePath和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];