iOS 的目录操作基础
2011-06-02 09:04
281 查看
1、文件管理类:NSFileManager
常用接口:
1.1 fileExistsAtPath 判断文件是否存在
1.2 fileAttributesPath 判断文件夹属性,可读/可写
1.3 copyPath 从一个目录拷贝一个文件到另外一个目录
1.4 movePath 从一个目录转到文件到另外一个目录
1.5 removeFileAtPath 删除指定目录中的文件
2、iOS可操作目录有两种:Documents下与Caches下, 如图:
1、创建目录createDirectoryAtPath:withIntermediateDirectories:attributes:error:
NSFileManager * fileManager = nil;
NSArray *paths = nil;
NSString *documentsDirectory = nil;
NSString * folerName = @"Photos";
NSString * fileName = @"myphoto.png";
NSString * filePath = nil;
UIImage *photoimage = nil;
NSData * imageData = nil;
//Documents:
paths
= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//Caches:
paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:folerName];
fileManager = [[NSFileManager alloc]init];
[fileManager createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:error];
2、创建目录并在目录中存储对象createFileAtPath: contents: attributes
filePath = [documentsDirectory stringByAppendingPathComponent:filename];
if((![fileManager fileExistsAtPath: fullPathToFile]))
{
photoimage = [[UIImage alloc] imageNamed:@"photo.png"];
imageData = UIImagePNGRepresentation(photoimage);
[fileManager createFileAtPath:filePath contents: imageData attributes:nil];
}
[photoimage release];
[fileManager release];
3、删除目录中指定文件
NSString * filePath = [documentsDirectory stringByAppendingPathComponent: fileName];
[filemanager removeItemAtPath: filePath error:NULL];
常用接口:
1.1 fileExistsAtPath 判断文件是否存在
1.2 fileAttributesPath 判断文件夹属性,可读/可写
1.3 copyPath 从一个目录拷贝一个文件到另外一个目录
1.4 movePath 从一个目录转到文件到另外一个目录
1.5 removeFileAtPath 删除指定目录中的文件
2、iOS可操作目录有两种:Documents下与Caches下, 如图:
1、创建目录createDirectoryAtPath:withIntermediateDirectories:attributes:error:
NSFileManager * fileManager = nil;
NSArray *paths = nil;
NSString *documentsDirectory = nil;
NSString * folerName = @"Photos";
NSString * fileName = @"myphoto.png";
NSString * filePath = nil;
UIImage *photoimage = nil;
NSData * imageData = nil;
//Documents:
paths
= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//Caches:
paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:folerName];
fileManager = [[NSFileManager alloc]init];
[fileManager createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:error];
2、创建目录并在目录中存储对象createFileAtPath: contents: attributes
filePath = [documentsDirectory stringByAppendingPathComponent:filename];
if((![fileManager fileExistsAtPath: fullPathToFile]))
{
photoimage = [[UIImage alloc] imageNamed:@"photo.png"];
imageData = UIImagePNGRepresentation(photoimage);
[fileManager createFileAtPath:filePath contents: imageData attributes:nil];
}
[photoimage release];
[fileManager release];
3、删除目录中指定文件
NSString * filePath = [documentsDirectory stringByAppendingPathComponent: fileName];
[filemanager removeItemAtPath: filePath error:NULL];
相关文章推荐
- Java-基础 目录操作 递归创建目录
- python基础操作—文件和目录操作方法
- iOS的沙箱目录和文件操作
- iOS底层基础知识-文件目录结构
- PHP之文件目录基础操作
- 做个知识回顾目录,打算每日更新一下ios的基础知识
- php基础06_文件增删、目录读取及其他操作
- ios多线程操作(一)—— 多线程基础与原理
- Java基础知识系列——目录操作
- IOS的一些文件操作。(沙箱) 在Documents目录下创建文件
- 六十七、基础框架(二十五)文件操作-列举 目录内容
- [IOS]关于文件(文件目录)的读写操作
- 小胖说事49-----IOS沙盒Files目录说明和常用操作
- linux基础命令学习(二)文件和目录操作
- 【ASP.NET 基础】ASP.NET 目录操作
- IOS开发基础—按钮(button)基本操作
- linux基础教程-----目录文件操作(2)
- ios文件操作之目录操作-NSFileManager
- iOS - 操作文件目录的方法
- Linux第2课——Linux技术之目录(文件夹)与文件操作基础