- iOS教程
- iOS - 主页
- iOS - 入门
- iOS - 环境设置
- iOS - Objective-C 基础知识
- iOS - 第一个 iPhone 应用程序
- iOS - 操作和出口
- iOS - 代表
- iOS - UI 元素
- iOS - 加速度计
- iOS - 通用应用程序
- iOS - 相机管理
- iOS - 位置处理
- iOS-SQLite 数据库
- iOS - 发送电子邮件
- iOS - 音频和视频
- iOS - 文件处理
- iOS - 访问地图
- iOS - 应用内购买
- iOS - iAd 集成
- iOS - 游戏套件
- iOS - 故事板
- iOS - 自动布局
- iOS - 推特和脸书
- iOS - 内存管理
- iOS - 应用程序调试
- iOS 有用资源
- iOS - 快速指南
- iOS - 有用的资源
- iOS - 讨论
iOS - 文件处理
文件处理无法用应用程序直观地解释,因此下面解释用于处理文件的关键方法。请注意,应用程序包仅具有读取权限,我们无法修改文件。您无论如何都可以修改应用程序的文档目录。
文件处理中使用的方法
下面讨论用于访问和操作文件的方法。这里我们必须将 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];