之前没有对NSFileManager进行系统的总结,在使用的时候不太熟练。
这次总结一下
获取应用沙盒根路径:
-(void)dirHome{
NSString *dirHome=NSHomeDirectory();
NSLog(@"app_home: %@",dirHome);
}
获取Documents目录路径:
//获取Documents目录
-(NSString *)dirDoc{
//[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"app_home_doc: %@",documentsDirectory);
return documentsDirectory;
}
获取Library目录路径:
//获取Library目录
-(void)dirLib{
//[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
NSLog(@"app_home_lib: %@",libraryDirectory);
}
获取Cache目录路径:
//获取Cache目录
-(void)dirCache{
NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cacPath objectAtIndex:0];
NSLog(@"app_home_lib_cache: %@",cachePath);
}
获取Tmp目录路径:
//获取Tmp目录
-(void)dirTmp{
//[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
NSString *tmpDirectory = NSTemporaryDirectory();
NSLog(@"app_home_tmp: %@",tmpDirectory);
}
创建文件夹:
//创建文件夹
-(void *)createDir{
NSString *documentsPath =[self dirDoc];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
// 创建目录
BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
if (res) {
NSLog(@"文件夹创建成功");
}else
NSLog(@"文件夹创建失败");
}
创建文件:
//创建文件
-(void *)createFile{
NSString *documentsPath =[self dirDoc];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil];
if (res) {
NSLog(@"文件创建成功: %@" ,testPath);
}else
NSLog(@"文件创建失败");
}
写数据到文件
-(void)writeFile{
NSString *documentsPath =[self dirDoc];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
NSString *content=@"测试写入内容!";
BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (res) {
NSLog(@"文件写入成功");
}else
NSLog(@"文件写入失败");
}
读文件属性
//读文件
-(void)readFile{
NSString *documentsPath =[self dirDoc];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
// NSData *data = [NSData dataWithContentsOfFile:testPath];
// NSLog(@"文件读取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"文件读取成功: %@",content);
}
文件属性
-(void)fileAttriutes{
NSString *documentsPath =[self dirDoc];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];
NSArray *keys;
id key, value;
keys = [fileAttributes allKeys];
int count = [keys count];
for (int i = 0; i < count; i++)
{
key = [keys objectAtIndex: i];
value = [fileAttributes objectForKey: key];
NSLog (@"Key: %@ for value: %@", key, value);
}
}
删除文件
//删除文件
-(void)deleteFile{
NSString *documentsPath =[self dirDoc];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
BOOL res=[fileManager removeItemAtPath:testPath error:nil];
if (res) {
NSLog(@"文件删除成功");
}else
NSLog(@"文件删除失败");
NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO");
}
下面是详细解析,但没有实例
//file
文件操作
NSFileManager
常见的NSFileManager文件的方法:
-(BOOL)contentsAtPath:path 从文件中读取数据
-(BOOL)createFileAtPath:path contents:(BOOL)data attributes:attr 向一个文件写入数据
-(BOOL)removeFileAtPath: path handler: handler 删除一个文件
-(BOOL)movePath: from toPath: to handler: handler 重命名或移动一个文件(to可能已经存在)
-(BOOL)copyPath:from toPath:to handler: handler 复制文件 (to不能存在)
-(BOOL)contentsEqualAtPath:path1 andPath:path2 比较两个文件的内容
-(BOOL)fileExistsAtPath:path 测试文件是否存在
-(BOOL)isReadablefileAtPath:path 测试文件是否存在,且是否能执行读操作
-(BOOL)isWritablefileAtPath:path 测试文件是否存在,且是否能执行写操作
-(NSDictionary *)fileAttributesAtPath:path traverseLink:(BOOL)flag 获取文件的属性
-(BOOL)changeFileAttributes:attr atPath:path 更改文件的属性
NSFileManager对象的创建 :
NSFileManager *fm;
fm = [NSFileManager defaultManager];
NSDictionary *attr =[fm fileAttributesAtPath: fname traverseLink: NO] ; //文件属性
NSLog(@”file size is:%i bytes “,[[attr objectForKey:NSFileSize] intValue]);
NSData *data =[fm contentsAtPath:@”filename”];//文件内容
常见的NSFileManager目录的方法:
-(NSString *)currentDirectoryPath 获取当前目录
-(BOOL)changeCurrentDirectoryPath:path 更改当前目录
-(BOOL)copyPath:from toPath:to handler:handler 复制目录结构,to不能已经存在
-(BOOL)createDirectoryAtPath:path attributes:attr 创建目录
-(BOOL)fileExistsAtPath:path isDirectory:(BOOL *)flag 测试文件是否为目录 (flag存储结构yes/no)
-(NSArray *)contentsOfDirectoryAtPath:path 列出目录的内容
-(NSDirectoryEnumerator *)enumeratorAtPath:path 枚举目录的内容
-(BOOL)removeFileAtPath:path handler:handler 删除空目录
-(BOOL)movePath:from toPath:to handler:handler 重命名或移动一个目录,to不能是已经存在的
path= [fm currentDirectoryPath] ;
NSArray *dirarray;
NSDirectoryEnumerator *direnu;
direnu = [fm enumeratorAtPath:path];
NSLog(@”contents of %@\n”,path);
BOOL flag;
while((path = [direnu nextObject])!=nil)
{
NSLog(@”%@ “,path);
[fm fileExistsAtPath:path isDirectory:&flag];
if(flag == YES)
[direnu skipDescendents]; //跳过子目录
}
path= [fm currentDirectoryPath] ;
dirarray = [fm contentsOfDirectoryAtPath:path];
NSLog(@”%@ “,dirarray);
常用路径工具函数
NSString * NSUserName(); 返回当前用户的登录名
NSString * NSFullUserName(); 返回当前用户的完整用户名
NSString * NSHomeDirectory(); 返回当前用户主目录的路径
NSString * NSHomeDirectoryForUser(); 返回用户user的主目录
NSString * NSTemporaryDirectory(); 返回可用于创建临时文件的路径目录
常用路径工具方法
-(NSString *) pathWithComponents:components 根据components中元素构造有效路径
-(NSArray *)pathComponents 析构路径,获取路径的各个部分
-(NSString *)lastPathComponent 提取路径的最后一个组成部分
-(NSString *)pathExtension 路径扩展名
-(NSString *)stringByAppendingPathComponent:path 将path添加到现有路径末尾
-(NSString *)stringByAppendingPathExtension:ext 将拓展名添加的路径最后一个组成部分
-(NSString *)stringByDeletingPathComponent 删除路径的最后一个部分
-(NSString *)stringByDeletingPathExtension 删除路径的最后一个部分 的扩展名
-(NSString *)stringByExpandingTildeInPath 将路径中的代字符扩展成用户主目录(~)或指定用户主目录(~user)
-(NSString *)stringByResolvingSymlinksInPath 尝试解析路径中的符号链接
-(NSString *)stringByStandardizingPath 通过尝试解析~、..、.、和符号链接来标准化路径
使用路径NSPathUtilities.h
tempdir = NSTemporaryDirectory(); 临时文件的目录名
path = [fm currentDirectoryPath];
[path lastPathComponent]; 从路径中提取最后一个文件名
fullpath = [path stringByAppendingPathComponent:fname];将文件名附加到路劲的末尾
extenson = [fullpath pathExtension]; 路径名的文件扩展名
homedir = NSHomeDirectory();用户的主目录
component = [homedir pathComponents]; 路径的每个部分
NSProcessInfo类:允许你设置或检索正在运行的应用程序的各种类型信息
(NSProcessInfo *)processInfo 返回当前进程的信息
-(NSArray*)arguments 以NSString对象数字的形式返回当前进程的参数
-(NSDictionary *)environment 返回变量/值对词典。描述当前的环境变量
-(int)processIdentity 返回进程标识
-(NSString *)processName 返回进程名称
-(NSString *)globallyUniqueString 每次调用该方法都会返回不同的单值字符串,可以用这个字符串生成单值临时文件名
-(NSString *)hostname 返回主机系统的名称
-(unsigned int)operatingSystem 返回表示操作系统的数字
-(NSString *)operatingSystemName 返回操作系统名称
-(NSString *)operatingSystemVersionString 返回操作系统当前版本
-(void)setProcessName:(NSString *)name 将当前进程名称设置为name
过滤数组中的文件类型 : [fileList pathsMatchingExtensions:[NSArrayarrayWithObject:@”jpg”]];