NSFileManager类与文件操作
Posted 亚洲小炫风
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NSFileManager类与文件操作相关的知识,希望对你有一定的参考价值。
用于对文件与目录进行的多种操作的(创建,复制,重命名,删除等)
可以通过defaultManager类方法创建
-[NSFileManager defaultManager]
一些文件操作的概念:
-当前目录:.
-上级目录:..
-根目录 :/
-home目录 ~
-绝对路径:从根目录开始的路径
-相对路径:从当前目录开始的路径
常用的目录操作
1.获取当前目录:currentDirectoryPath
2.概念当前目录:changerCurrentDireactoryPath:path
3.创建目录:createDireactoryAtPath:path
4.复制目录(文件):copyItemAtPath:from toPath:to
5.移动目录(文件):moveItemAtPath:from toPath:to
6.删除目录(文件):removeItemAtPath:path
NSFileManager *fileManager=[NSFileManager new];
NSLog(@"=====>path:%@", fileManager.currentDirectoryPath);
[fileManager changeCurrentDirectoryPath:@".."];
NSLog(@"=====>path:%@", fileManager.currentDirectoryPath);
//创建目录
NSError *error=nil;
NSString *dirPath=[NSHomeDirectory() stringByAppendingFormat:@"hello"];
[fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:NO attributes:nil error:&error];
if(error==nil)
[fileManager changeCurrentDirectoryPath:dirPath];
NSLog(@"=====>path:%@", fileManager.currentDirectoryPath);
else
NSLog(@"==========>error:%@",error);
//删除目录
if([fileManager removeItemAtPath:dirPath error:nil])
NSLog(@"==========>删除成功");
[fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:NO attributes:nil error:nil];
[fileManager createDirectoryAtPath:[NSHomeDirectory() stringByAppendingFormat:@"test"] withIntermediateDirectories:NO attributes:nil error:nil];
NSDirectoryEnumerator<NSString *> *paths= [fileManager enumeratorAtPath:NSHomeDirectory()];
NSString *path=nil;
while(path=[paths nextObject])
NSLog(@"=====>文件:%@",path);
NSArray<NSString *> *path2= [fileManager contentsOfDirectoryAtPath:NSHomeDirectory() error:nil];
for(NSString *path in path2)
NSLog(@"=====>file:%@",path);
常用的文件操作
1.创建文件:crateFileAtPath:path contents:data attributes:attr
2.判断文件是否存在:filesExistsAtPath:path
3.判断两个文件内容是否一样:contentsEqualAtPath:andPath:
4.复制目录(文件):copyItemAtPath:from toPath:to
5.移动目录(文件):moveItemAtPath:from toPath:to
6.删除目录(文件):removeItemAtPath:path
7.获取目录(文件)属性
NSDictionary *attr=[fm attributesOfItemAtPath:path];
NSFileManager *manager=[NSFileManager defaultManager];
NSString *content=@"hello world";
NSData *data=[NSData dataWithBytes:[content UTF8String] length:content.length];
NSString *path=[NSHomeDirectory() stringByAppendingFormat:@"/test.txt"];
if([manager createFileAtPath:path contents:data attributes:nil])
NSLog(@"创建成功");
if([manager fileExistsAtPath:path])
NSLog(@"=====>path:%@ 已经存在",path);
NSArray<NSString *> *paths= [manager contentsOfDirectoryAtPath:NSHomeDirectory() error:nil];
for(NSString *path in paths)
NSLog(@"====>path:%@",path);
文件移动和属性
NSFileManager *manager=[NSFileManager defaultManager];
NSString *content=@"hello world";
NSData *data=[NSData dataWithBytes:[content UTF8String] length:content.length];
NSString *path=[NSHomeDirectory() stringByAppendingFormat:@"/test.txt"];
if([manager createFileAtPath:path contents:data attributes:nil])
NSLog(@"创建成功");
if([manager fileExistsAtPath:path])
NSLog(@"=====>path:%@ 已经存在",path);
//移动文件:
NSString *newPath=[NSHomeDirectory() stringByAppendingFormat:@"/test2.txt"];
if([manager moveItemAtPath:path toPath:newPath error:nil])
NSLog(@"=====>移动文件成功");
NSArray<NSString *> *paths= [manager contentsOfDirectoryAtPath:NSHomeDirectory() error:nil];
for(NSString *path in paths)
NSLog(@"====>path:%@",path);
//获取文件属性
NSDictionary<NSFileAttributeKey, id> *attributes= [manager attributesOfItemAtPath:newPath error:nil];
NSLog(@"=======>attributes:%@",attributes);
/**
=======>attributes:
NSFileCreationDate = "2021-06-25 12:32:14 +0000";
NSFileExtensionHidden = 0;
NSFileGroupOwnerAccountID = 20;
NSFileGroupOwnerAccountName = staff;
NSFileModificationDate = "2021-06-25 12:32:14 +0000";
NSFileOwnerAccountID = 501;
NSFilePosixPermissions = 420;
NSFileReferenceCount = 1;
NSFileSize = 11;
NSFileSystemFileNumber = 13287847;
NSFileSystemNumber = 16777220;
NSFileType = NSFileTypeRegular;
*/
数据缓冲区管理类
1.分为不可变(NSData)和可变的(NSMutableData)
2.与字符串的转换
NSString *str=@"this is a test file";
NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
NSString *str2=[[NSString allow] initWithData encoding:NSUTF8Encoding];
从文件读取到缓冲区
NSData *data=[fm contentsAtPath:path];
从缓冲区写数据到文件
createFileAtPath:path contents:data attributes:attr
-(void)test
NSFileManager *manager= [NSFileManager defaultManager];
NSString *path=[NSHomeDirectory() stringByAppendingFormat:@"/test2.txt"];
NSString *str=@"我爱你\\n 你也爱我";
NSData *content=[NSData dataWithBytes:[str UTF8String] length:str.length];
if([manager createFileAtPath:path contents:content attributes:nil])
NSLog(@"====>文件创建成功");
//1. 通过创建NSFileHandle对象
NSFileHandle *handle=[NSFileHandle fileHandleForReadingAtPath:path];
//NSData *buffer=[handle readDataToEndOfFile];
NSData *buffer=[handle readDataOfLength:str.length];
NSString *fileStr=[[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding];
NSLog(@"=======>读取str:%@",fileStr);
//3.
[handle closeFile];
获取常用的特殊目录函数
1.返回当前用户的登录名
-NSString *NSUserName()
2.返回当前用户的完整用户名
-NSString *NSFullUserName()
3.返回当前用户主目录的路径
-NSString *NSHomeDirectory()
4.返回用户user的主目录
-NSString *NSHomeDirectoryForUser()
5.返回可用于创建临时文件的路径目录
-NSString *NSTemporayDirectory();
以上是关于NSFileManager类与文件操作的主要内容,如果未能解决你的问题,请参考以下文章